简体   繁体   中英

Shell script to delete files and folders that don't exist in another folder

Using a shell script I wish to delete all files and folders from /folder2/ that do not exist in /folder1/ . Files only need to be matched by name.

I must add that the content of both folders shouldn't necessarily match after this operation because it's possible that /folder1/ contains files that do not in exist in /folder2/ . So after executing the shell script all files and folders found in /folder2/ can also be found in /folder1/ but not vice versa.

The following works for me:
rsync -r --delete --existing --ignore-existing /path/to/folder1/ /path/to/folder2/

rsync will delete all files and folders from folder2 that are not found in folder1 recursively. Also, rsync will skip creating files on the destination. This answer was found here: https://serverfault.com/a/713577

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