/ factorpad.com / tech / linux-essentials / xargs-command.html
An ad-free and cookie-free website.
Intermediate
Learn to build and execute commands at the Linux command line.
Videos can also be accessed from the Linux Essentials Playlist on YouTube.
Linux xargs Command Summary with Examples (3:41)
Our ninetieth word, or command to memorize is
xargs
from our category
Workflow.
xargs
allows you to build and execute
commands.
-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.
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.
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.
(Below is the screen from within nano
.)
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
.
And it takes a second here, but you'll see, there's our reminder.
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.
For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:
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.
read
command,
hit Back.tee
command, click Next./ factorpad.com / tech / linux-essentials / xargs-command.html
A newly-updated free resource. Connect and refer a friend today.