How do I correct a directory incorrectly copied into itself?

6,715

First, move the new directory out (with a temp name to avoid conflict), then delete the original directory, then rename the new directory into place. Note: because this will involve deleting things en masse, think it through carefully before just running these commands, and make sure you have a backup first. Also, I've written this with &&s between the commands, so if one fails the rest won't run -- good in a script, but if you're doing this manually it's probably better to just type them sequentially, pay attention, and stop if anything goes wrong.

mv <path>/mydir/mydir <path>/mydir-temp &&
rm -R <path>/mydir &&
mv <path>/mydir-temp <path>/mydir
Share:
6,715

Related videos on Youtube

Peter Boughton
Author by

Peter Boughton

Updated on September 17, 2022

Comments

  • Peter Boughton
    Peter Boughton over 1 year

    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?

    • Admin
      Admin over 13 years
      Something doesn't add up, mv /path/mydir /path/ tells me that "/path/mydir/ and /path/mydir are the same file"., and mv /path/mydir/mydir/* /path/mydir/ works. The only way I can get that "subdirectory of itself" message is if I reverse the command and mv /path/mydir /path/mydir/mydir
    • Peter Boughton
      Peter Boughton over 13 years
      DerfK, I agree it sounds odd, but I've just run it again and will update the question with the commands...
  • Peter Boughton
    Peter Boughton over 13 years
    I'm confused by the recursive delete command here - if the directory is moved why would it need to be deleted?
  • Peter Boughton
    Peter Boughton over 13 years
    Also, I tried a variant of this and get the same problem - see latest edit to question.
  • javaamtho
    javaamtho over 13 years
    Oops, I had the path in the rm command wrong; I've edited the answer to correct it.
  • Peter Boughton
    Peter Boughton over 13 years
    Ah, I see what you're doing now. This would however mean that any files in <path>/mydir that aren't in <path>/mydir/mydir would be lost. I guess sometimes that's useful, but not in this case.
  • javaamtho
    javaamtho over 13 years
    Are you trying to merge the two mydir directories? I don't think the mv command can do that...
  • clerksx
    clerksx over 11 years
    You should never parse ls. Please see mywiki.wooledge.org/ParsingLs.
  • clerksx
    clerksx over 11 years
    Considering the alternative is even shorter (for d in *), and more intuitive, it just seems really strange to do regardless of whether it usually works fine.