Re: OT: finding every file not in a list
On Wed, Jan 27, 2010 at 02:16:12PM -0500, 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? You should probably use a shell command as recommended by others, but I decided to write a Ruby script to do what you describe. It assumes you have a plaintext file full of to-keep filenames with each filename being an absolute path filename (e.g., /usr/local/bin/xpdf instead of something like xpdf with no absolute path), one such filename per line, with nothing else in the file. The script, which I saved as fkeep.rb, looks like this on the inside: #!/usr/bin/env ruby # syntax: # fkeep.rb filename [path] # # where: # filename is the file containing paths for files to keep # [path] is an optional path to where this program should # start deleting files losers= Dir[#{Dir.getwd}/**/*] keepers = IO.readlines ARGV.shift startpath = ARGV.shift if startpath Dir.chdir startpath end keepers.each {|filepath| losers.delete filepath.chomp } losers.each do |filepath| unless File.directory?(filepath) File.delete filepath end end I've done some cursory testing with this, and it seems to work just fine, but use it only at your own risk. Note that this will not delete directories, because it felt like too much work to make it *safely* delete directories. You will have to delete any empty directories yourself if you use this script as written. -- Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ] pgp5TPqUHsHHX.pgp Description: PGP signature
Re: OT: finding every file not in a list
On Thu, Jan 28, 2010 at 03:13:51AM -0700, Chad Perrin wrote: losers= Dir[#{Dir.getwd}/**/*] keepers = IO.readlines ARGV.shift startpath = ARGV.shift if startpath Dir.chdir startpath end Oops. Speaking of using at your own risk . . . That line that reads `losers= Dir[#{Dir.getwd}/**/*]` should be *after* the conditional block. Thus, the above quoted code should look like this instead: keepers = IO.readlines ARGV.shift startpath = ARGV.shift if startpath Dir.chdir startpath end losers= Dir[#{Dir.getwd}/**/*] . . . otherwise you might end up deleting a bunch of files in the wrong part of the directory hierarchy if you execute the program from somewhere other than where you want files deleted. I guess I shouldn't have gotten fancy and tried to provide a way to execute it from anywhere in the filesystem. Sorry about that. -- Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ] pgpqPGufuu9SM.pgp Description: PGP signature
Re: OT: finding every file not in a list
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? # ls -1F keep old/ # find old old old/a old/a/1 old/a/4 old/b old/b/2 old/b/5 old/c old/c/3 old/c/6 # find new find: new: No such file or directory # cat keep a/4 b/5 c/6 # ( cd old; cat ../keep | cpio -pld ../new ) /tmp/old 0 blocks # ls -1F keep new/ old/ # find new new new/a new/a/4 new/b new/b/5 new/c new/c/6 -- Fuzzy love, -CyberLeo Technical Administrator CyberLeo.Net Webhosting http://www.CyberLeo.Net cyber...@cyberleo.net Furry Peace! - http://.fur.com/peace/ ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: OT: finding every file not in a list
On Jan 28, 2010, at 4:23 PM, CyberLeo Kitsana wrote: 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? # ls -1F keep old/ [...] I think mtree(8) is the proper tool for this job. Especially the -r option: -rRemove any files in the file hierarchy that are not described in the specification. -- David Kelly N4HHE, dke...@hiwaay.net Whom computers would destroy, they must first drive mad. ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: OT: finding every file not in a list
Hi, 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? Perhaps something like this will help: find /dir -type f | \ grep -v `cat excludelist` | \ xargs rm Regards, -- Glen Barber ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: OT: finding every file not in a list
You could probably use rsync with : --exclude-from=FILE read exclude patterns from FILE -jgh On Wed, Jan 27, 2010 at 02:17:37PM -0500, Glen Barber thus spake: Hi, 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? Perhaps something like this will help: find /dir -type f | \ grep -v `cat excludelist` | \ xargs rm Regards, -- Glen Barber ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: OT: finding every file not in a list
Glen Barber wrote: Hi, 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? Perhaps something like this will help: find /dir -type f | \ grep -v `cat excludelist` | \ xargs rm Regards, Note quite since it will see every file after the first as a file to be grepped instead of filtered out... I was playing with the idea of doing a tcsh foreach loop on each file and then using it cut down the output of find... the problem there is it is O(n^2) where is a good solution is O(n) [I need to do this {don't ask the reasons} everytime I build a program I am developing] ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: OT: finding every file not in a list
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: b...@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. ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org