help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] hi, doubt about xargs


From: John Kearney
Subject: Re: [Help-bash] hi, doubt about xargs
Date: Mon, 3 Jun 2013 20:25:30 +0200

As I understand it the default behaviour of find is {} expands to only one file name as such

find .  -name '*.txt' -execdir sh -c 'cp "${0}" "${0%.txt}.bak"' {}
is the same thing? or what am I missing something.



This on the other hand does make sense.

find .  -name '*.txt' -print0 | xargs -n10 -p3 sh -c 'for f; do cp "$f" "${f%.txt}.bak"; done' _ {}


?? just curious if I'm missing something.

cheers.


On Mon, Jun 3, 2013 at 3:14 PM, Greg Wooledge <address@hidden> wrote:
On Sun, Jun 02, 2013 at 03:21:35PM +0200, Joaquín Villanova wrote:
> By the way, I've read your wiki and it is just Amazing, I didn't realize
> that it was you / your wiki when you sent me the reply.

Thanks.  There have been several contributors; it's not all my work.

> Just for taking this down definitely. I understand all the command  `find .
> -name '*.txt' -execdir sh -c 'for f; do cp "$f" "${f%.txt}.bak"; done' _ {}
> +'   but for what is `_' used? thanks in advance!

When you run a command like this:

sh -c 'some stuff' a b c ...

"a" becomes $0, "b" becomes $1, "c" becomes $2, and so on.

The "_" in the example will become the script's $0, which is ignored.
The other arguments after that (provided by find) will become the
positional parameters $1, $2 and so on.

"_" can be replaced by nearly anything.  However, don't use "-" because
some versions of sh interpret that like "--" and strip it out.  Thus:

imadev:~$ find .bashrc -exec sh -c 'printf "<%s> " "$@"; echo' - a b {} \;
<b> <.bashrc>

In this case, "-" was stripped out completely, "a" became $0, "b" became
$1, and the filename passed by find became $2.  This behavior is not
consistent across shells, so using - is just a Bad Idea.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]