1

I just want to move and overwrite all the files and folders including any subdirectories and their files to another folder and its subfolder.

I have tried the following commands, but they don't work on subdirectory and its files.

mv -f fromA/* toB/

mv -f fromA/{,.}* toB/

So all the files from folder A are moved to folder B but not A/sub to B/sub.

I get error cannot move 'fromA/subA' to 'toB/subB': File Exists

It works if I go down each folder one by one, but I am sure I am missing something. I have seen most if not all related answers but none of them work here.

2

info mv has this:

   _Note_: 'mv' will only replace empty directories in the destination.
Conflicting populated directories are skipped with a diagnostic.

There is no option to overrule that, so it seems. Either remove B and rename A (back to B), or empty/delete the directories in B by hand first.

| improve this answer | |
1

I recommend using rsync

With rsync, you can specify that you would like the files to be deleted and you can even do a "dry run" to see exactly what would happen if you were to run it for real.

Try

rsync -av --dry-run --delete-before fromA toB

The --delete-before is great for merging files on systems that are low on space. If you are happy with the dry run, remove the --dry-run option.

I recommend looking at the man page to see all of the different "delete" options so you can determine which might be best for you.

If you want to delete all the files in the destination that are not related to the move you can use the --delete-excluded option. Again, use --dry-run to check what will be deleted before running it.

There is a --remove-source-files which will remove the source files during the transfer (like mv would) but it does not remove directories. So you will still have to remove the source directories with rm

| improve this answer | |
0

Based on your statement that you get an error that the file already exists you already have folders and files under 'toB'? If you mainly which to merge the files from 'fromA' to 'toB' then you are probably are best to just copy the files recursively (cp -ar) and then remove 'fromA' afterward.

Otherwise, if 'toB' should initially be empty and you just want to make sure the contents of 'toB' are the same as 'fromA':

$ rm -rf toB
$ mv fromA toB
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.