Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 27 Jan 2010 12:11:50 -0800
From:      Bill Campbell <freebsd@celestial.com>
To:        freebsd-questions@freebsd.org
Subject:   Re: OT: finding every file not in a list
Message-ID:  <20100127201150.GB19557@ayn.mi.celestial.com>
In-Reply-To: <4B6090FC.4070002@gmail.com>
References:  <4B6090FC.4070002@gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
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
whitespace.

Bill
-- 
INTERNET:   bill@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.



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20100127201150.GB19557>