1

How can I use rsync (or another program) to delete identical files between directories? To be identical, location and metadata should match (permissions, ownership, timestamp)

For example, I backup dir X to Y. After time, there are files added and removed in X.

I want to remove from X all files/directories that match identically in Y. Do not touch files in X that are different.

Note: I'm familiar with jdupes, but I'm not trying to delete just any identical files. I want to delete files that also are identical in directory location and filename.

user206746
  • 146
  • 1
  • 6

1 Answers1

1
cd /path/to/X
find -type f -exec ls -l {} \; > /tmp/LIST # Get a list of all files in X
cd /path/to/Y
find -type f -exec ls -l {} \; >> /tmp/LIST # Get a list of all files in Y (combine with list from X)
cd /tmp
sort LIST > SORT # Sort all listed
uniq -d SORT > DUP # Exclude files that aren't listed twice
cd /path/to/X
cat /tmp/DUP | xargs -d '\n' rm # Delete all files listed as duplicate
find -type d -empty -delete # Optional, delete all empty directories

Warning-

This solution compares files using the output of ls -l, so it compares metadata date+time+owner+permissions+filename, and does not compare bytes within the files. Also it is not safe for files with newlines in their name.

Sepero
  • 189
  • 2
  • 5