sophie: A cartoon-like representation of a girl standing on a hill, with brown hair, blue eyes, a flowery top, and blue skirt. ☀ (Default)
Sophie ([personal profile] sophie) wrote in [community profile] command_liners2010-11-03 01:32 pm
Entry tags:

xargs and apostrophes/spaces

If you've ever tried using xargs with a list of filenames, you've probably at some point come across errors like these:

xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option

ls: cannot access /home/sophie/directory: No such file or directory
ls: cannot access with: No such file or directory
ls: cannot access spaces: No such file or directory
ls: cannot access in/blah.txt: No such file or directory


Both of these errors are due to xargs by default interpreting some characters as special; in the first one, it won't allow apostrophes, and in the second it treats spaces as if they meant separate arguments. And the -0 switch doesn't help at all if you're using newlines.

Both of these errors can be resolved very easily: just add the -d"\n" switch before the command you want xargs to execute. This tells xargs that you don't want xargs to mess with your input at all except to treat newline characters as delimiters. This time, you should find that both apostrophes and spaces are accepted properly.
pauamma: Cartooney crab wearing hot pink and acid green facemask holding drink with straw (Default)

[personal profile] pauamma 2010-11-03 02:58 pm (UTC)(link)
Or (if you're using find), instead of
find ... -print | xargs ...
use
find ... -print0 | xargs -0 ...
pne: A picture of a plush toy, halfway between a duck and a platypus, with a green body and a yellow bill and feet. (Default)

[personal profile] pne 2010-11-03 03:08 pm (UTC)(link)
+1

"find | xargs" (with GNU find and GNU xargs) should always be "find ... -print0 | xargs -0 ..." - I can't think of a case where this would be the wrong thing to do.
karmag: Stylized face based on Dreamwidth logo (Default)

[personal profile] karmag 2010-11-03 06:42 pm (UTC)(link)
Yeah, it doesn't work here.

(Anyone here know a lot about POSIX and portability? If no one else does, perhaps I should read up on it and try to post something.)
pauamma: Cartooney crab wearing hot pink and acid green facemask holding drink with straw (Default)

[personal profile] pauamma 2010-11-03 07:02 pm (UTC)(link)
I have the SUSv3 docs as dead electrons, but I never read them.
jld: (Default)

[personal profile] jld 2010-11-03 03:34 pm (UTC)(link)
But I don't know of a filesystem that allows newlines in filenames

All the usual Unix filesystems allow anything that isn't / or NUL, as far as I know.
pne: A picture of a plush toy, halfway between a duck and a platypus, with a green body and a yellow bill and feet. (Default)

[personal profile] pne 2010-11-03 03:41 pm (UTC)(link)
That was my state of knowledge, too - that those are the only two forbidden characters.

Sophie: try this -

$ touch 'file name
with a newline in it'


and you should have, er, a file name with a newline in it. (Try ls and ls -l - and try ls fil*, though that will probably fail due to argument quoting!)
jadelennox: Senora Sabasa Garcia, by Goya (Default)

[personal profile] jadelennox 2010-11-03 03:02 pm (UTC)(link)
Timely advice. I just 5 min. ago gave up on debugging a piece of crap written by a predecessor of mine:

/usr/bin/find "$@" -name '*.xml' -exec xml sel -N "namespacewenthere" -t -m '/xpathwenthere' -v '@elementhere' -o ' ' -o '{}' '{}' ';' | sed -E 's/([^[:space:]]*) (.*)/xml tr scripts\/badlynamedtransform.xsl -s filesize="`cat "\2" | wc -c`" "\2" > includes\/\1.foxml.xml/' | sh


That had a lot of unnecessary garbage and also crapped out on the hundreds of filenames with spaces. I tried switching it to print0 with xargs -0, but had timing/precedence problems with the backtick. To make matters worse, I'm writing the code on a Debian system that's getting run on somebody else's Snow Leopard system, and our versions of find and xargs aren't compatible. Just 5 min. ago I said "fuck it, I'm doing this in perl".

*sigh*
pauamma: Cartooney crab wearing hot pink and acid green facemask holding drink with straw (Default)

[personal profile] pauamma 2010-11-03 03:23 pm (UTC)(link)
http://www.catb.org/jargon/html/J/job-security.html (But obviously, it can only go so far.)
vlion: cut of the flammarion woodcut, colored (Default)

[personal profile] vlion 2010-11-04 01:52 am (UTC)(link)
Perl. For when you just Want To Get The Job Done.
razputnik: (Default)

[personal profile] razputnik 2010-11-03 03:54 pm (UTC)(link)
Nice, I normally end up writing hideously long bash one liners with xargs because I am a bad person - so this should come in handy :)