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)
Comments
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.
Thank you very much. Pretty useful.
Great article, finally able to figure how to make xargs work with sed
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
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.
Thank You very Much. I was also trying to get and understand xargs applications. This article gave me an idea about xargs
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.
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
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