OT: finding every file not in a list
Chad Perrin
perrin at apotheon.com
Thu Jan 28 10:22:09 UTC 2010
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 ]
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
Url : http://lists.freebsd.org/pipermail/freebsd-questions/attachments/20100128/cf0d366f/attachment.pgp
More information about the freebsd-questions
mailing list