简体   繁体   English

Shell脚本删除另一个文件夹中不存在的文件和文件夹

[英]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/ . 我希望使用Shell脚本从/folder2/中删除/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/ . 我必须补充一点,在此操作之后,两个文件夹的内容不一定要匹配,因为/folder1/可能包含/folder2/不存在的文件。 So after executing the shell script all files and folders found in /folder2/ can also be found in /folder1/ but not vice versa. 因此,在执行Shell脚本后,在/folder2/找到的所有文件和文件夹也可以在/folder1/找到,反之则不然。

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. rsync将递归删除folder2中未找到的所有文件和文件夹。 Also, rsync will skip creating files on the destination. 另外,rsync将跳过在目标位置上创建文件。 This answer was found here: https://serverfault.com/a/713577 在这里找到了这个答案: https : //serverfault.com/a/713577

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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