How do I correct a directory incorrectly copied into itself?
- by Peter Boughton
Given the following situation...
<path>/mydir1/mydir2
...where mydir2 should have overwritten mydir1, but was instead placed inside, and both directories actually have the same filename. How is that fixed?
Attempting to do mv <path>/mydir/mydir/* <path>/mydir/ or mv <path>/mydir <path>/ results in:
mv: cannot move `<path>/mydir/mydir` to a subdirectory of itself, `<path>/mydir`
This seems stupidly simple, but it's late here and I can't figure it out.
There are seventeen such directories to fix (path differs for each, but same mydir name).
To confirm, the error message can be caused with this:
# cd /path/to/directory
# mv mydir/mydir ./
mv: cannot move `mydir/mydir' to a subdirectory of itself, `./mydir'
Also tried:
# mv mydir/mydir/* mydir/
mv: cannot move `mydir/mydir/otherdir1' to a subdirectory of itself, `mydir/otherdir1'
mv: cannot move `mydir/mydir/otherdir2' to a subdirectory of itself, `mydir/otherdir2'
and...
# mv /path/to/directory/mydir/mydir/otherdir1 /path/to/directory/mydir/
mv: cannot move `/path/to/directory/mydir/mydir/otherdir1' to a subdirectory of itself, `/path/to/directory/mydir/otherdir1'
and using a temporary directory:
# mv mydir/mydir ./mydir-temp
# mv mydir-temp/* mydir/
mv: cannot move `mydir-temp/otherdir1' to a subdirectory of itself, `mydir/otherdir1'
mv: cannot move `mydir-temp/otherdir2' to a subdirectory of itself, `mydir/otherdir2'
I found a similar question "How to recursively move all files (including hidden) in a subfolder into a parent folder in *nix?" which suggested that mv bar/{,.}* . would do this.
But this also gives the same errors, as well as confusingly picking up . and .. from somewhere.
# cd mydir
# mv mydir/{,.}* .
mv: cannot move `mydir/otherdir1' to a subdirectory of itself, `./otherdir1'
mv: cannot move `mydir/otherdir2' to a subdirectory of itself, `./otherdir2'
mv: cannot move `mydir/.' to `./.': Device or resource busy
mv: cannot move `mydir/..' to `./..': Device or resource busy
mv: overwrite `./.file'? y
Another similar question "linux mv command weirdness" suggests that mv doesn't overwrite and a copy is required.
# cd mydir
# cp -rf ./mydir/* ./
cp: overwrite `./otherdir1/file1'? y
cp: overwrite `./otherdir1/file2'? y
cp: overwrite `./otherdir1/file3'?
This appears to be working... except there's a lot of files (and dirs) - I don't want to confirm every one!
Isn't the f there supposed to prevent this?
Ok, so cp was aliased to cp -i (which I found out with type cp), and bypassed by using \cp -rf ./mydir/* ./ which seems to have worked.
Although I've solved the problem of getting dirs/files from one place to another, I'm still curious as to what's going on with the mv stuff - is this really a deliberate feature as suggested by Warner?