OT: finding every file not in a list

Bill Campbell freebsd at celestial.com
Wed Jan 27 20:11:51 UTC 2010

On Wed, Jan 27, 2010, Aryeh M. Friedman wrote:
> I have a list of files that should be in a dir tree and want to remove  
> any files from the tree not in list (i.e. if it is on the list keep it  
> else rm it)... any quick way to do this?

One way to do this that I use quite frequently uses the comm
program to compare lists, something like

cd /path-to-dir1
find . -type f | sort > /tmp/list1
cd /path-to-dir2
find . -type f | sort > /tmp/list2

comm -23 /tmp/list1 /tmp/list2 > /tmp/filesinlist1notinlist2
comm -13 /tmp/list1 /tmp/list2 > /tmp/filesinlist2notinlist1
comm -12 /tmp/list1 /tmp/list2 > /tmp/filescommontolist1andlist2

Then of course one could remove files in list1 not in list2 with:

xargs rm < /tmp/filesinlist1notinlist2

NOTE:  The xargs command will not work if the files contain

INTERNET:   bill at celestial.com  Bill Campbell; Celestial Software LLC
URL: http://www.celestial.com/  PO Box 820; 6641 E. Mercer Way
Voice:          (206) 236-1676  Mercer Island, WA 98040-0820
Fax:            (206) 232-9186  Skype: jwccsllc (206) 855-5792

Criminals love gun control  it makes their jobs safer.

More information about the freebsd-questions mailing list