Sophie (
sophie) wrote in
command_liners2010-11-03 01:32 pm
![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
![[community profile]](https://www.dreamwidth.org/img/silk/identity/community.png)
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:
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
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.
no subject
no subject
"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.
no subject
no subject
no subject
(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.)
no subject
no subject
All the usual Unix filesystems allow anything that isn't / or NUL, as far as I know.
no subject
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
andls -l
- and tryls fil*
, though that will probably fail due to argument quoting!)no subject
/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*
no subject
no subject
no subject
no subject