When to use xargs when laying pipes?

I am new to bash and I am trying to understand a usage xargsthat is still not clear to me. For example:

history | grep ls

Here I am looking for a team lsin my story. I did not use this command xargs, and it worked fine.

find /etc - name "*.txt" | xargs ls -l

I’m this one, I had to use it xargs, but I still can’t understand the difference, and I can’t decide correctly when to use xargsand when not to.

+4
source share
4 answers

, xargs , . grep , . , xargs .

xargs . . , . {}; , :

find /etc -name "*.txt" | xargs -I {} ls -l {}

3 /etc, . , ls -l /etc/*.txt .

, {} .

find /etc -name "*.txt" | xargs -I {} mv {} {}.bak

, , , . , find .

find /etc -print0 -name "*.txt" | xargs -I {} -0 mv {} {}.bak

, xargs, , .

+3

: xargs . xargs, .

(, rm bad_example) stdin ( rm -i is_this_bad_too, read answer). , grep sed, , , .
grep stdin, . ls find . xargs - . man xargs xargs. :

find /etc -name "*.txt" -exec ls -l {} \;
find /etc -name "*.txt" -ls
ls -l $(find /etc -name "*.txt" )
ls /etc/*.txt

, , a nasty filename with spaces.txt /etc.

+2

xargs (1) (, ..), -NUL- .

, find -exec [command] {} +. , xargs -0.

0

GNU Parallel can do the same as xargs, but it does not have broken and accessible "functions".

You can find out GNU Parallel by looking at http://www.gnu.org/software/parallel/man.html#EXAMPLE:-Working-as-xargs--n1.-Argument-appending examples and going through the http: // tutorial www.gnu.org/software/parallel/parallel_tutorial.html

0
source

All Articles