jant90
jant90

Reputation: 11

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.

Upvotes: 0

Views: 241

Answers (1)

jant90
jant90

Reputation: 11

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

Upvotes: 1

Related Questions