简体   繁体   中英

How do I copy differing content files from one directory to another?

There exists two directories: a/ and b/ .
I'd like to copy all the files(recursively) from a/ into b/ .
However, I only want to copy over an a file if its content is different than the already existing b file. If the corresponding b file does not exist, then you would still copy over the a file.

*by "corresponding file", I mean a files with the same name and relative path from their parent directories.

note: The reason I don't want to overwrite a b file with the same exact contents, is because the b directory is being monitored by another program, and I don't want the file date to change causing the program to do more work than required.

I'm essentially looking for a way to perform a cp -rf a/ b/ while performing a diff check on each file. If the file's are different, perform the copy; otherwise skip the copy.

I see that cp has an update flag:

   -u, --update
          copy only when the SOURCE file is newer than the destination  file  or  when  the
          destination file is missing

but this will not work because I'm not concerned about newer files; I'm concerned about different file contents.

Any shell language will do.
I've been attempting to get this to work by injecting my diff check into a find command:

find a/ ??? -exec cp {} b \;

This doesn't seem like an uncommon thing to do between two directories, so I'm hoping there is an elegant command line solution as aposed to me having to write a python script.

You can achieve this using rsync . Files or directories will be updated only if there is any new update in source folder.

$rsync -av --progress sourcefolder destinationfolder

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM