Hi, this is Sandra Henry-Stocker, author of the “Unix as a Second Language” blog on NetworkWorld.
In this Linux tip, we’re going to look at xargs. It's a command that accepts input that is piped to it and then uses that input as arguments to whatever command you specify. Here's a very simple example:
In this example, xargs is running a single command (head -3 file1 file2 file3), so it's not really making anything easier. A better use of xargs is to have it work with a command like find that locates files fitting some criteria and then pipes its output to xargs. Here's an example in which empty files are located and removed:
In this example, we see the command that xargs is running because I added the -t option.
For some commands, you may need to have xargs run once for each argument. To do this, you have add a loop using the -I option.
The -I and the two x characters create the loop (any word or character works) which is needed to run a separate command for each user.
That’s your Linux tip for xargs.
If you have questions or would like to suggest a topic, please add a comment below. And don’t forget to subscribe to the IDG Tech(talk) channel on YouTube.
If you liked this video, please hit the like and share buttons. For more Linux tips, be sure to follow us on Facebook, YouTube and NetworkWorld.com.