Comments on 8 Practical Examples of Linux Xargs Command for Beginners

The Linux xargs command may not be a hugely popular command line tool, but this doesn't take away the fact that it's extremely useful, especially when combined with other commands like find and grep. If you are new to xargs, and want to understand its usage, you'll be glad to know that's exactly what we'll be doing here.

9 Comment(s)

Add comment

Please register in our forum first to comment.

Comments

By: JebSpringfield

Thanks for this article, I've been trying to understand how xargs works since forever and this is by far the best explanation I've found.

By: Amjad

Thank you very much. Pretty useful.

By: Olubodun

Great article, finally able to figure how to make xargs work with sed

By: eric

Hi! it looks that at least on my system you need a point after find to indicate a directory to search, so

xargs find . -name 

is what works

By: RN

This is a good article.  May I suggest adding the -I argument as one of your sections?  I found it to be essential for getting certain command lines to work, but don't fully understand why.  

By: PavanMedam

Thank You very Much. I was also trying to get and understand xargs applications. This article gave me an idea about xargs

By: l3nn4rt

as stated by <code>man xargs</code>:

<pre><code>

       -L max-lines

              Use at most max-lines nonblank input lines per command line.  Trailing blanks cause an input line to be

              logically continued on the next input line.  Implies -x.

</code></pre>

which means:

<pre><code>

$  xargs -L2

1 2 3

4 5

1 2 3 4 5

 

1 2 3 blank here -->            

4 5           

6 7 8 blank here --> 

9 0 1 2 3 blank here -->

4 5

1 2 3 blank here --> 4 5 6 7 8 blank here --> 9 0 1 2 3 blank here --> 4 5

</code></pre>

so I suggest you to fix the description of -L option.

By: Todd S

Thank you for your insight, you have helped me to resolve a problem

=======================================================

pods=`kubectl get pods | awk '/nginx/ {print $1}'`

for i in $pods; do ls *.png | xargs -n 1 -I{} kubectl cp $i:/usr/share/nginx/html -c <container> ; done

=======================================================

Thank you for your insight.

Todd

By: Scott Beach

If I try to use -d I get an error. My man pages don't list -d. Maybe my version of xargs is different than the one you describe? Mine is BSD August 4, 2015