FactorPad
Faster Learning Tutorials

Linux xargs command summary with examples

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.
Paul Alan Davis, CFA, November 15, 2016
Updated: July 25, 2018
In this tutorial, 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 Next

~/ home  / tech  / linux essentials  / xargs command


The Linux xargs command

Intermediate

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

Video Tutorial

Linux xargs command summary with examples (3:41)

Videos can also be accessed from the YouTube Playlist.

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.

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.

Demonstration

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 for more advanced programming content. Subscribe here.

  • For the Outline to all 100 tutorials, click Outline.
  • To go back to the read command, hit Back.
  • To gain flexibility in your shell scripts with the tee command, click Next.

Outline Back Next

~/ home  / tech  / linux essentials  / xargs command



 
 
Keywords:
Linux xargs command
bash xargs
bash pass arguments
xargs unix
xargs shell script
find xargs
xargs example
xargs Linux
bash script arguments
bash arguments
find xargs
man xargs
shell script arguments
bash command line arguments
pass results as arguments
bash arguments