29

I have folders old and new.

I want to replace all the contents in old/* to new/*. There can be many subdirectories in those folders.

But there can be few files which are not in new/* but are in old/*, so I want them to stay as they are.

How can I do that from a Linux shell?

21

rsync would probably be a better option here. It's as simple as rsync -a subdir/ ./. check this unix.stackexchage answer for better solutions

use -f with the cp command

cp -fR /source/files /dest

suppress cp to overwrite" prompt..

To override cp's alias you can simply enclose it in quotes:

 'cp' -rf ./source/* /destination/

for more information follow these links:

http://www.wallpaperama.com/forums/cp-command-problems-forcing-overwrite-using-cp-command-t5685.html

http://bytes.com/topic/unix/answers/865372-force-cp-overwrite-existing-directory

1
  • Don't use rsync for a one time copy, the overhead of hashing all dir contents is substantial, it only pays when keeping directories synchronized. For a gigantic one time copy, use a tarpipe. – J. M. Becker Jan 13 at 20:35
25

Use rsync. It will synchronize the directories in one direction. So, if you want to update your old folder with everything from new, but keep what's in there, just use:

rsync -avh --dry-run /path/to/new/ /path/to/old/

This will, in a first instance, just output the list of files that would be transferred. In that case: Everything found in new will be copied to old, unless it's already there. Everything in old stays as it is.

If it looks fine to you, remove the --dry-run argument to transmit them for real.

The -avh flags just enable archive mode (which will preserve timestamps, etc.), verbosity and human-readable file-sizes. Nothing will be deleted from the destination unless you specify the --delete flag. Consult man rsync for more information.

0

please Try Once if you want to change the name of a folder theare are no command to rename the files/folders the mv command is used to move the old directory to new directery so rename sucess.

mkdir new sudo mv -fr old/* new/

1
  • Welcome to Super User! Before answering an old question having an accepted answer (look for green ✓) as well as other answers ensure your answer adds something new or is otherwise helpful in relation to them. Here is a guide on How to Answer. There is also tour for the site tour, and help center for the help center. – help-info.de Dec 21 '20 at 14:20

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.