From owner-freebsd-questions@FreeBSD.ORG Sat Feb 11 09:16:22 2012 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 0CB5E1065672 for ; Sat, 11 Feb 2012 09:16:22 +0000 (UTC) (envelope-from matthew@FreeBSD.org) Received: from smtp.infracaninophile.co.uk (smtp6.infracaninophile.co.uk [IPv6:2001:8b0:151:1:3cd3:cd67:fafa:3d78]) by mx1.freebsd.org (Postfix) with ESMTP id 81AC58FC08 for ; Sat, 11 Feb 2012 09:16:21 +0000 (UTC) Received: from seedling.black-earth.co.uk (seedling.black-earth.co.uk [IPv6:2001:8b0:151:1:fa1e:dfff:feda:c0bb]) (authenticated bits=0) by smtp.infracaninophile.co.uk (8.14.5/8.14.5) with ESMTP id q1B9GD0O034306 (version=TLSv1/SSLv3 cipher=DHE-RSA-CAMELLIA256-SHA bits=256 verify=NO) for ; Sat, 11 Feb 2012 09:16:13 GMT (envelope-from matthew@FreeBSD.org) X-DKIM: OpenDKIM Filter v2.4.3 smtp.infracaninophile.co.uk q1B9GD0O034306 Authentication-Results: smtp.infracaninophile.co.uk/q1B9GD0O034306; dkim=none (no signature); dkim-adsp=none Message-ID: <4F3631D6.3070109@FreeBSD.org> Date: Sat, 11 Feb 2012 09:16:06 +0000 From: Matthew Seaman User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:10.0) Gecko/20120129 Thunderbird/10.0 MIME-Version: 1.0 To: freebsd-questions References: <1237723287.20120207235924@yandex.ru> <4F31A260.20109@infracaninophile.co.uk> <20120207231716.31aa8bc3@gumby.homeunix.com> <20120209032544.GA58560@ozzmosis.com> <4F3541D6.5080505@infracaninophile.co.uk> <4f368a47.b9IiheSL9TAxtlPw%perryh@pluto.rain.com> In-Reply-To: <4f368a47.b9IiheSL9TAxtlPw%perryh@pluto.rain.com> X-Enigmail-Version: 1.3.5 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="------------enig342F3E1EDFC2B38E58C3EB8B" X-Virus-Scanned: clamav-milter 0.97.3 at lucid-nonsense.infracaninophile.co.uk X-Virus-Status: Clean X-Spam-Status: No, score=-2.8 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_00 autolearn=ham version=3.3.2 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on lucid-nonsense.infracaninophile.co.uk Cc: Subject: Re: 'rm' Can not delete files 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: Sat, 11 Feb 2012 09:16:22 -0000 This is an OpenPGP/MIME signed message (RFC 2440 and 3156) --------------enig342F3E1EDFC2B38E58C3EB8B Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On 11/02/2012 15:33, perryh@pluto.rain.com wrote: > Matthew Seaman wrote: >=20 >>>>> ls -1 | xargs rm >> >>>> but be aware that that wont work for filenames with spaces. >> >> True. Can't do that using ls to generate the list of filenames as >> there is no option to generate a null-separated list amongst ls's >> multitudinous collection. >=20 > It can, however, be done indirectly :) >=20 > $ ls -1 | tr '\012' '\000' | xargs -0 rm Until you run into someone who creates filenames with newlines in them...= Well, actually in that case, ls(1) would usually mangle the name: $ touch 'a > file name with > new lines in it' $ ls a* a?file name with?new lines in it Since it uses '?' to replace any "unprintable" character, that's not bullet proof either. Of course, ls(1) has an option for that: $ ls -B a* a\012file name with\012new lines in it Actually, it has two options for that: $ ls -b a* a\nfile name with\nnew lines in it and before Randal picks me up again on the differences between ls(1) outputting to the terminal rather than into anything else... $ ls a* > ls.out $ hd ls.out=09 00000000 61 0a 66 69 6c 65 20 6e 61 6d 65 20 77 69 74 68 |a.file name with| 00000010 0a 6e 65 77 20 6c 69 6e 65 73 20 69 6e 20 69 74 |.new lines in it| * 00000020 No mangling occurs if you pipe the output into another process. Even so, the conclusion must be that ls(1) is not the best choice for generating a list of file names *for programmatic input* if you have to deal with the full gamut of possible names. find(1) is much better in that case. ls(1) is great for displaying to people in pretty much all circumstances, and it should only be used programmatically if you can /guarantee/ you aren't going run into any of the tricky cases. Cheers, Matthew --=20 Dr Matthew J Seaman MA, D.Phil. PGP: http://www.infracaninophile.co.uk/pgpkey --------------enig342F3E1EDFC2B38E58C3EB8B Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG/MacGPG2 v2.0.16 (Darwin) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iEYEARECAAYFAk82MdwACgkQ8Mjk52CukIxEGwCdGYoel/OAcoSw8kO81kbT7KJF QMsAoIL6o42zTNaNGkYJ+2iqNPWOv9ov =UT6I -----END PGP SIGNATURE----- --------------enig342F3E1EDFC2B38E58C3EB8B--