Special File Types  «Prev  Next»
Lesson 8 Using xargs and helpful utilities with find
Objective Use the find command, its predicates, and other utilities to create more targeted finds.

Using xargs and Helpful Utilities with find

The xargs command is used to modify the behavior of other commands so that they read their arguments from standard input instead of the command line. It is usually used in a pipe[1]. For example:

wc Example

For example,
who | wc -l 
would take the output of the who command and use that as the input for the
wc -l 
command.
This would give you the number of users currently logged in.

find . –type d –print | xargs ls –l

The find command in this example searches for all directories below the current directory (.). It generates a list of these files. The xargs command passes this list as arguments to the ls –l command, yielding a directory listing of all subdirectories of (.).
Another interesting and useful argument for the find command is the -ok argument. This argument allows you to issue any command, then be prompted by the system before it executes the command. For example:

find / –atime +365 -ok rm {} /;

would find all files on the system (/) accessed more than 365 days ago (-atime +365) and would then prompt you to confirm that you want to remove (rm) the files. The syntax for using this argument is
-ok command {} /;

where command is any UNIX command.

Other handy tricks

Just like you can use the pipe command (|), you can use the redirect command (>) to take the results from a find or list, then place it into a text file. For example:
ls –l > listfile

would place the output of the listing into a file named listfile.
Three other handy commands are wc, head, and tail.
Do not forget the grep command.
It searches the named input files for lines containing a match to the given pattern.

Creating Targeted Finds

Click the link below to read about using the xargs command and other utilities in conjunction with the find command.
Creating Targeted Finds

[1]pipe: A pipe is a way of using the output of one command as the input for another command and is designated by the character |.