Build a Better Process

Linux xargs Command Summary with Examples

Linux xargs allows you to build and execute commands.
  1. Purpose - Learn what xargs is for an how to find help.
  2. Options - Review a few common options and arguments.
  3. Examples - Walk through code examples with xargs.
  4. Script - Add xargs to our script and run it.
  5. A tip - Finish off with one more insight.
face pic by Paul Alan Davis, CFA
Updated: February 24, 2021
In this tutorial on Linux xargs, 90 of 100, below find a 3-4 minute introductory video, a text-based tutorial and all of the code examples from the video.

Outline Back Tip Next

/ factorpad.com / tech / linux-essentials / xargs-command.html

An ad-free and cookie-free website.

Examples of the Linux xargs Command


Learn to build and execute commands at the Linux command line.

Video Tutorial

Videos can also be accessed from the Linux Essentials Playlist on YouTube.

Linux xargs Command Summary with Examples (3:41)

Video Script

The Command and Why You Need It

Our ninetieth word, or command to memorize is xargs from our category Workflow.

xargs allows you to build and execute commands.

Common Linux xargs Options
-options description
-h, --help Print help screen
-p, --interactive Run in interactive mode
-n, --max-args=ARGS Use maximums of ARGS arguments

Recall from videos (tutorials) #87 through #89, we're building a script to tie together what we've learned here, and now we'll build and execute commands.

Before we start, it helps to think of commands as mini programs and most follow this structure: command -option(s) argument(s).

The xargs command has 20 options and the argument is the name of a command, with options, and if you don't supply a command by default it will use echo and print the results.

Like most commands, help is available with double-dash --help, -p runs in interactive mode and -n limits the arguments.

So why is xargs an important command? Well, we need a way to use output from one command, like find for example, as standard input to another. And now you know how to do that.


Okay, the best way to embed this in your memory is by typing in your own terminal window.

Find this on your Mac using a program called Terminal. On Linux use Terminal or Konsole, and currently Microsoft is adding this functionality to Windows.

Here we go. So let's play with xargs first, then add it to our script.

So by default, xargs uses echo to output what came from another command, like a tail -3 of our .bash_history file.

$ tail -3 ~/.bash_history | xargs xargs --h man xargs exit

And you can see the output there.

Next, let's see how xargs gets arguments from another command, ls files created in videos in the fifties, for example. And then count only the lines.

$ ls -1 video5* | xargs wc -l 146 video54.txt 131 video55.txt 88 video56.txt 51 video57.txt 416 total

That's an example of passing from one to the next.

And then, let's add xargs to a script with nano, from video (tutorial) #20, and the file is called funscript located in our bin directory.

$ nano ~/bin/funscript

(Below is the screen from within nano.)

GNU nano 2.2.6 File: /home/factorpad/bin/funscript #!/bin/bash # The shebang gives the absolute path to the executable program # To see if it points to the right executable try: which bash # This is our first script called funscript # From video 87, print text and the current date/time echo -e "\nThe current date and time:" date # From video 88, we measure the time to run the script sleep 2s # From video 89, we take input from the keyboard echo -e "What is your name? \c" read yourname echo "Thank you $yourname" # From video 90, report files and lines since last archive echo "Lines and files since your last archive:" find . -type f -cnewer "video73.tar" | xargs wc -l [ Read 24 lines ] ^G Get Help ^0 WriteOut ^R Read File ^Y Prev Page ^K Cut Text ^C Cur Pos ^X Exit ^J Justify ^W Where Is ^V Next Page ^U UnCut Text ^T To Spell

And I added these lines (the comment for video 90 and below). It will search for files later than the date of the latest tar file and then count lines and then show a message with it. Okay, please pause if you'd like to catch up on your end and then we'll try it out.

(Hit Ctrl-x to leave nano and y to confirm saving.)

And last, explicitly run with this pointing to the ~/bin/funscript.

$ ~/bin/funscript The current date and time: Mon Nov 15 17:22:14 PST 2016 What is your name? paul Thank you paul Lines and files since your last archive: 0 ./video79.script 5 ./video85.txt 0 ./video75.txt 0 ./video76.txt 61 ./video85b.txt 66 total $ _

And it takes a second here, but you'll see, there's our reminder.

A Final Tip

Okay now you know how to use xargs. And you know the syntax for commands, options and arguments.

One last tip about the xargs command. So if xargs is tough, don't worry, it is a tricky but can be a time saver, so please spend some time with it.

Okay, thanks for visiting today. I hope this was a helpful introduction to the xargs command.

Learn More About The Series

For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:

What's Next?

Now that we're nearing the end of this tutorial series for Linux beginners, check out our YouTube Channel and other links at this site for more advanced programming content, particularly with Python.

Outline Back Tip Next

/ factorpad.com / tech / linux-essentials / xargs-command.html

linux xargs
xargs examples
xargs linux
xargs command
unix xargs
xargs example
shell script
xargs unix
bash xargs
find xargs
bash script
man xargs
bash script arguments
bash arguments
shell script arguments
bash command line arguments

A newly-updated free resource. Connect and refer a friend today.