简体   繁体   中英

Copy files to directory if they are not in another directory

I have four files file1, file2, file3, file4 .

A directory /All contains all four files, a directory /A contains file1, file2 . Now I want to copy the rest file3, file4 to another directory /B . How can I do this in the command line?

Thank you for the help.

find ./All/ -type f -exec bash -c '[ ! -e "./A/$(basename "$0")" ] && cp "$0" ./B/' '{}' \;

The following solution (in bash ) will support spaces in filenames if you need, although not subdirectories.

diff <(\ls /A) <(\ls /All) | egrep '^> ' | cut -b 3- | xargs -d'\n' -I {}  cp "/All/{}" "/B/{}"

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