Remove files present in one directory but not the other

This will remove all the files and folders that are in /dir1 but not in /dir2

[local_username@localhost /]$ diff -rq /dir1 /dir2 | grep -E "^Only in /dir1*" | sed -n 's/://p' | awk '{print $3"/"$4}' | xargs -I {} rm -r {}

Explanation:

diff - command that does comparisons in Linux
-rq - recursively search (r) and output only files that differ (q)
/dir1 - first directory to compare
/dir2 - compare dir1 to dir2
grep -E "^Only in /dir1*" - shows files that are in dir1 but not in dir2
sed -n 's/://p' - sed is the stream editor command and in this case it removes ':' from the output as diff adds a : at the end of each file found
awk - a program used for formatting data
'{ - start awk language
print - awk print command
$3 - third column of the output from sed which is the directory
/ - add in a forward slash
$4 - forth column of the output from sed which is the file if there is one
}' - end awk language interpretation
xargs -I {} - define variable to represent the path and file name as {}
rm -r {} - remove the file that is only in dir1/ and any folders and there children that are only in dir1/




comments powered by Disqus