If you want to print most of the files in a large directory,
put the output of
into a file.
Edit the file to leave just the filenames you want printed.
Give the file to
' standard input:
ls > allfiles.tmp
xargs lpr < allfiles.tmp
What did that do?
With lines like these in
ran one or more
commands, each with a group of
arguments, until it had read every word in the file:
lpr afile application ...
lpr ... yoyotest zapme
The standard output of
is the standard output of the
commands it runs.
So, if you'd created
above but you wanted to format
the files with
first, you could type:
xargs pr < allfiles.tmp | lpr
would run all of these
The shell would pipe their standard outputs
to a single
pr afile application ...
pr ... yoyotest zapme
In this next example,
gets a list of all files in the directory tree.
Next, we use
to read those filenames and run
to find which files contain the word "WARNING."
Next, we pipe that to a setup with
, like the one
in the previous example:
find . -type f -print | xargs grep -l WARNING | xargs pr | lpr
"Huh?" you might say.
Just take that step by step.
The output of
is a list of filenames, like
./afile ./bfile ... ./adir/zfile
and so on.
gives those filenames to one or more
grep -l WARNING ./afile ./bfile ...
grep -l WARNING ./adir/zfile ...
The standard output of all those
s is a (shortened) list of
filenames that match.
That's piped to another
commands with the
UNIX is weird and wonderful!
Sometimes you don't want
to run its command with as many
arguments as it can fit on the command line.
option sets the maximum number of arguments
will give to each command.
Another handy option,
, prompts you before running each command.
Here's a directory full of files with errors (whose names end with
) and corrected versions (named
to give the list of files to
; it reads two
filenames at once, then asks whether I want to run
those two files.
It keeps prompting me and running
until it runs out of file pairs:
ls | xargs -p -n2 diff -c
diff -c chap1.bad chap1.fixed ?...
Output of diff command for chap1
diff -c chap2.bad chap2.fixed ?...
diff -c chap3.bad chap3.fixed ?...
Output of diff command for chap3