Go to the previous, next section.
There are several ways you can print information about the files that
match the criteria you gave in the find
expression. You can
print the information either to the standard output or to a file that
you name. You can also execute commands that have the file names as
arguments. You can use those commands as further filters to select files.
True; print the full file name on the standard output, followed by a newline.
True; print the full file name into file file, followed by a
newline. If file does not exist when find
is run, it is
created; if it does exist, it is truncated to 0 bytes. The file names
`/dev/stdout' and `/dev/stderr' are handled specially; they
refer to the standard output and standard error output, respectively.
True; list the current file in `ls -dils' format on the standard output. The output looks like this:
204744 17 -rw-r--r-- 1 djm staff 17337 Nov 2 1992 ./lwall-quotes
The fields are:
POSIXLY_CORRECT
is set, in which
case 512-byte blocks are used. See section Size, for how to find files based
on their size.
True; like `-ls' but write to file like `-fprint' (see section Print File Name).
True; print format on the standard output, interpreting `\'
escapes and `%' directives. Field widths and precisions can be
specified as with the printf
C function. Unlike `-print',
`-printf' does not add a newline at the end of the string.
True; like `-printf' but write to file like `-fprint' (see section Print File Name).
The escapes that `-printf' and `-fprintf' recognize are:
\a
\b
\c
\f
\n
\r
\t
\v
\\
A `\' character followed by any other character is treated as an ordinary character, so they both are printed, and a warning message is printed to the standard error output (because it was probably a typo).
`-printf' and `-fprintf' support the following format
directives to print information about the file being processed. Unlike
the C printf
function, they do not support field width specifiers.
`%%' is a literal percent sign. A `%' character followed by any other character is discarded (but the other character is printed), and a warning message is printed to the standard error output (because it was probably a typo).
%p
%f
%h
%P
%H
%g
%G
%u
%U
%m
%k
%b
%s
%d
%F
%l
%i
%n
Some of these directives use the C ctime
function. Its output
depends on the current locale, but it typically looks like
Wed Nov 2 00:42:36 1994
%a
ctime
function.
%Ak
%c
ctime
function.
%Ck
%t
ctime
function.
%Tk
Below are the formats for the directives `%A', `%C', and
`%T', which print the file's timestamps. Some of these formats
might not be available on all systems, due to differences in the C
strftime
function between systems.
The following format directives print single components of the time.
H
I
k
l
p
Z
M
S
@
The following format directives print single components of the date.
a
A
b
h
B
m
d
w
j
U
W
Y
y
The following format directives print combinations of time and date components.
r
T
X
c
D
x
You can use the list of file names created by find
or
locate
as arguments to other commands. In this way you can
perform arbitrary actions on the files.
Here is how to run a command on one file at a time.
Execute command; true if 0 status is returned. find
takes
all arguments after `-exec' to be part of the command until an
argument consisting of `;' is reached. It replaces the string
`{}' by the current file name being processed everywhere it
occurs in the command. Both of these constructions need to be escaped
(with a `\') or quoted to protect them from expansion by the shell.
The command is executed in the directory in which find
was run.
For example, to compare each C header file in the current directory with the file `/tmp/master':
find . -name '*.h' -exec diff -u '{}' /tmp/master ';'
Sometimes you need to process files alone. But when you don't, it is faster to run a command on as many files as possible at a time, rather than once per file. Doing this saves on the time it takes to start up the command each time.
To run a command on more than one file at once, use the xargs
command, which is invoked like this:
xargs [option...] [command [initial-arguments]]
xargs
reads arguments from the standard input, delimited by
blanks (which can be protected with double or single quotes or a
backslash) or newlines. It executes the command (default is
`/bin/echo') one or more times with any initial-arguments
followed by arguments read from standard input. Blank lines on the
standard input are ignored.
Instead of blank-delimited names, it is safer to use `find -print0'
or `find -fprint0' and process the output by giving the `-0'
or `--null' option to GNU xargs
, GNU tar
, GNU
cpio
, or perl
.
You can use shell command substitution (backquotes) to process a list of arguments, like this:
grep -l sprintf `find $HOME -name '*.c' -print`
However, that method produces an error if the length of the `.c'
file names exceeds the operating system's command-line length limit.
xargs
avoids that problem by running the command as many times as
necessary without exceeding the limit:
find $HOME -name '*.c' -print | grep -l sprintf
However, if the command needs to have its standard input be a terminal
(less
, for example), you have to use the shell command
substitution method.
Because file names can contain quotes, backslashes, blank characters,
and even newlines, it is not safe to process them using xargs
in its
default mode of operation. But since most files' names do not contain
blanks, this problem occurs only infrequently. If you are only
searching through files that you know have safe names, then you need not
be concerned about it.
In many applications, if xargs
botches processing a file because
its name contains special characters, some data might be lost. The
importance of this problem depends on the importance of the data and
whether anyone notices the loss soon enough to correct it. However,
here is an extreme example of the problems that using blank-delimited
names can cause. If the following command is run daily from
cron
, then any user can remove any file on the system:
find / -name '#*' -atime +7 -print | xargs rm
For example, you could do something like this:
eg$ echo > '# vmunix'
and then cron
would delete `/vmunix', if it ran
xargs
with `/' as its current directory.
To delete other files, for example `/u/joeuser/.plan', you could do this:
eg$ mkdir '# ' eg$ cd '# ' eg$ mkdir u u/joeuser u/joeuser/.plan' ' eg$ echo > u/joeuser/.plan' /#foo' eg$ cd .. eg$ find . -name '#*' -print | xargs echo ./# ./# /u/joeuser/.plan /#foo
Here is how to make find
output file names so that they can be
used by other programs without being mangled or misinterpreted. You can
process file names generated this way by giving the `-0' or
`--null' option to GNU xargs
, GNU tar
, GNU
cpio
, or perl
.
True; print the full file name on the standard output, followed by a null character.
True; like `-print0' but write to file like `-fprint' (see section Print File Name).
xargs
gives you control over how many arguments it passes to the
command each time it executes it. By default, it uses up to
ARG_MAX
- 2k, or 20k, whichever is smaller, characters per
command. It uses as many lines and arguments as fit within that limit.
The following options modify those values.
--no-run-if-empty
-r
--max-lines[=max-lines]
-l[max-lines]
--max-args=max-args
-n max-args
xargs
will exit.
--max-chars=max-chars
-s max-chars
--max-procs=max-procs
-P max-procs
xargs
will run as many processes as
possible at a time. Use the `-n', `-s', or `-l' option
with `-P'; otherwise chances are that the command will be run only
once.
xargs
can insert the name of the file it is processing between
arguments you give for the command. Unless you also give options to
limit the command size (see section Limiting Command Size), this mode of
operation is equivalent to `find -exec' (see section Single File).
--replace[=replace-str]
-i[replace-str]
find bills -type f | xargs -iXX sort -o XX.sorted XX
The equivalent command using `find -exec' is:
find bills -type f -exec sort -o '{}.sorted' '{}' ';'
To ask the user whether to execute a command on a single file, you can
use the find
primary `-ok' instead of `-exec':
Like `-exec' (see section Single File), but ask the user first (on the standard input); if the response does not start with `y' or `Y', do not run the command, and return false.
When processing multiple files with a single command, to query the user
you give xargs
the following option. When using this option, you
might find it useful to control the number of files processed per
invocation of the command (see section Limiting Command Size).
--interactive
-p
You can test for file attributes that none of the find
builtin
tests check. To do this, use xargs
to run a program that filters
a list of files printed by find
. If possible, use find
builtin tests to pare down the list, so the program run by xargs
has less work to do. The tests builtin to find
will likely run
faster than tests that other programs perform.
For example, here is a way to print the names of all of the unstripped
binaries in the `/usr/local' directory tree. Builtin tests avoid
running file
on files that are not regular files or are not
executable.
find /usr/local -type f -perm +a=x | xargs file | grep 'not stripped' | cut -d: -f1
The cut
program removes everything after the file name from the
output of file
.
If you want to place a special test somewhere in the middle of a
find
expression, you can use `-exec' to run a program that
performs the test. Because `-exec' evaluates to the exit status of
the executed program, you can write a program (which can be a shell
script) that tests for a special attribute and make it exit with a true
(zero) or false (non-zero) status. It is a good idea to place such a
special test after the builtin tests, because it starts a new
process which could be avoided if a builtin test evaluates to false.
Use this method only when xargs
is not flexible enough, because
starting one or more new processes to test each file is slower than
using xargs
to start one process that tests many files.
Here is a shell script called unstripped
that checks whether its
argument is an unstripped binary file:
#!/bin/sh file $1 | grep 'not stripped' > /dev/null
This script relies on the fact that the shell exits with the status of
the last program it executed, in this case grep
. grep
exits with a true status if it found any matches, false if not. Here is
an example of using the script (assuming it is in your search path). It
lists the stripped executables in the file `sbins' and the
unstripped ones in `ubins'.
find /usr/local -type f -perm +a=x \ \( -exec unstripped '{}' \; -fprint ubins -o -fprint sbins \)
Go to the previous, next section.