Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 11 Feb 2012 09:16:06 +0000
From:      Matthew Seaman <matthew@FreeBSD.org>
To:        freebsd-questions <freebsd-questions@FreeBSD.org>
Subject:   Re: 'rm' Can not delete files
Message-ID:  <4F3631D6.3070109@FreeBSD.org>
In-Reply-To: <4f368a47.b9IiheSL9TAxtlPw%perryh@pluto.rain.com>
References:  <1237723287.20120207235924@yandex.ru> <4F31A260.20109@infracaninophile.co.uk> <20120207231716.31aa8bc3@gumby.homeunix.com> <20120209032544.GA58560@ozzmosis.com> <CAE7N2kdmbm_5=c8oNknYQE5HOrvVjtfS4XTGYvxTjEQVbGr-7Q@mail.gmail.com> <4F3541D6.5080505@infracaninophile.co.uk> <4f368a47.b9IiheSL9TAxtlPw%perryh@pluto.rain.com>

Next in thread | Previous in thread | Raw E-Mail | Index | Archive | Help
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 <freebsd-questions@infracaninophile.co.uk> 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--



Want to link to this message? Use this URL: <http://docs.FreeBSD.org/cgi/mid.cgi?4F3631D6.3070109>