From owner-freebsd-questions@FreeBSD.ORG Thu Jan 28 10:22:09 2010 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 58FE1106568B for ; Thu, 28 Jan 2010 10:22:09 +0000 (UTC) (envelope-from perrin@apotheon.com) Received: from outbound-mail-111.bluehost.com (outbound-mail-111.bluehost.com [69.89.18.7]) by mx1.freebsd.org (Postfix) with SMTP id 278EF8FC23 for ; Thu, 28 Jan 2010 10:22:08 +0000 (UTC) Received: (qmail 27048 invoked by uid 0); 28 Jan 2010 10:22:08 -0000 Received: from unknown (HELO box543.bluehost.com) (74.220.219.143) by outboundproxy3.bluehost.com with SMTP; 28 Jan 2010 10:22:08 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=apotheon.com; h=Date:From:To:Subject:Message-ID:Mail-Followup-To:References:Mime-Version:Content-Type:Content-Disposition:In-Reply-To:User-Agent:X-Identified-User; b=AwiI7mBFELUULRQ8gRRdDujN7MYOjbhKveLN497/3rwrbW/uOxxZyVZdA/TZ2MNMNrx2mIOMIHlDWePfZxSwd3VIq/yaWSN06Ga3QAuMvfksz9Kx5NIjQ9C5kPSH2yuU; Received: from c-24-8-180-234.hsd1.co.comcast.net ([24.8.180.234] helo=kukaburra.hydra) by box543.bluehost.com with esmtpsa (TLSv1:AES256-SHA:256) (Exim 4.69) (envelope-from ) id 1NaRVn-0005cN-Ch for freebsd-questions@freebsd.org; Thu, 28 Jan 2010 03:22:08 -0700 Received: by kukaburra.hydra (sSMTP sendmail emulation); Thu, 28 Jan 2010 03:13:51 -0700 Date: Thu, 28 Jan 2010 03:13:51 -0700 From: Chad Perrin To: freebsd-questions@freebsd.org Message-ID: <20100128101351.GA27490@guilt.hydra> Mail-Followup-To: freebsd-questions@freebsd.org References: <4B6090FC.4070002@gmail.com> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="4Ckj6UjgE2iN1+kY" Content-Disposition: inline In-Reply-To: <4B6090FC.4070002@gmail.com> User-Agent: Mutt/1.4.2.3i X-Identified-User: {2737:box543.bluehost.com:apotheon:apotheon.org} {sentby:smtp auth 24.8.180.234 authed with ren@apotheon.org} Subject: Re: OT: finding every file not in a list X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 28 Jan 2010 10:22:09 -0000 --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 [path] # # where: # 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--