Score:1

rsync Delete only Matching files

kz flag

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.

Score:1
my flag
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.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.