Using the below directory tree as an example, what is the best way to move the contents of directory/folderA to directory.

How to overwrite a file if a file with the same name exists in the destination, for example: directory/folderA/2017/06/info.log and directory/2017/06/info.log.

|-- folderA
|   |-- 2017
|   |   |-- 06
|   |
|   |-- 2016
|   |   |-- 12
|   |   |-- 11
|   |   |-- 10
|-- 2017
|   |-- 04
|   |-- 05
|   |-- 06
|-- 2016
|   |
  • I am looking for the move equivalent of the yes | cp -rf. This is so that the data does not have to be rewritten. The goal is also to preserve files in the destination folders that do not exist in the source directory. – Greg Jul 15 '17 at 3:06

Something like rsync -a --remove-source-files src/ dest ? A simple test shows that anything existing in dest but not in src will be preserved. (Note the slash after src. It is required here.)

Taking preservation a step further, you can add --ignore-existing so files in dest will not be overwritten even if they also exist in src. But then you may be left with files in src since --remove-source-files only applies to files successfully transferred to the destination.

Generally speaking, rsync is a very powerful tool with numerous options. (And, proportionally, a bit of a learning curve...though the case here is pretty straightforward, it seems.) If someone has thought of a copying scenario before there's a decent chance it's covered in rsync.

tar -cv /parent_folder/subfolder/* -f - I tar -xv --overwrite /parent_folder/

use tar to pack whole directory and pipe to unar in parent folder might be the best choice (add -j both before pipe and after if you want filter the tar file through bzip)

—overwrite :overwrite existing file with file extracted from the tar file

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.