Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 28 Jan 2010 03:13:51 -0700
From:      Chad Perrin <perrin@apotheon.com>
To:        freebsd-questions@freebsd.org
Subject:   Re: OT: finding every file not in a list
Message-ID:  <20100128101351.GA27490@guilt.hydra>
In-Reply-To: <4B6090FC.4070002@gmail.com>
References:  <4B6090FC.4070002@gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help

--4Ckj6UjgE2iN1+kY
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

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=20
> any files from the tree not in list (i.e. if it is on the list keep it=20
> 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    =3D Dir["#{Dir.getwd}/**/*"]
    keepers   =3D IO.readlines ARGV.shift
    startpath =3D 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.

--=20
Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ]

--4Ckj6UjgE2iN1+kY
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.14 (FreeBSD)

iEYEARECAAYFAkthY18ACgkQ9mn/Pj01uKUiPgCg5ooM3b1JVrF41bvAD3CerEI3
N9sAn2USq4gC5uCbLJxf1JO8P93PuhWv
=OPcO
-----END PGP SIGNATURE-----

--4Ckj6UjgE2iN1+kY--



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