find
find ( /usr/bin/find
) is a program for scanning file systems . It is a fundamental component of the software equipment of UNIX systems and as such is defined by the
POSIX standard (or the identical standards IEEE Std 1003 and ISO / IEC 9945), marked as mandatory (required component) and thus part of the Single UNIX Specification . UNIX-like operating systems also usually have this command.
Under Microsoft Windows there is also the command place but it searches the contents of files and has more of (remote) resemblance to the command grep .
history
The first find
appeared in Fifth Edition Unix (around June 1974) as part of the PWB / UNIX project ( Programmers Workbench ), which was incorporated into Research Unix . It was written by Richard C. ( Dick ) Haight , a member of the PWB / UNIX developers, as were cpio and expr . find
and cpio
were designed to be used together.
Working method
As a typical command line utility, its behavior is find
regulated by section 12.2 of the standard ( Utility Syntax Guidelines ). Starting from a base directory to be specified, find
the file system structure goes through recursively and generates a list of file system contents (files, directories, etc.), which (at least in most cases) is <stdout>
output. This list can be progressively restricted by one or more operands according to various criteria:
find /some/dir -print # jeden Eintrag in /some/dir anzeigen find /some/dir -type f -print # angezeigte Einträge auf (reguläre) Dateien beschränken find /some/dir -type f -name 'x*' -print # angezeigte Einträge auf Dateien deren Namen mit „x“ beginnt, einschränken
It is possible to link several such operands using logical operations (AND, OR, NOT) and to influence the precedence of the operands using brackets. Complex filter types can also be implemented in this way.
What all operands have in common is that they return a logical TRUE
or as a result FALSE
, on the basis of which the entity being examined participates in further processing (until it is - usually - output at the end) or is excluded from it.
It is possible to restrict the output format to the pure file name ( -print
) or to output a list of attributes in table form ( -ls
which ls -ails
results in output similar to that of the command ).
-exec
Has a special position among the operands -exec
. As an argument, it expects a (simple or compound) command in which the name of the entity being examined is {}
represented by the symbol . This makes it possible to carry out an action represented by a command for a list of file system entries that has just been created. The example searches for calls or the code of the function in all files *.c
in the directory (the file is specified so that the file names of the occurrences are also displayed):
/some/dir
myfunc()
/dev/null
grep
find /some/dir -type f -name '*\.c' -exec grep 'myfunc(' /dev/null {} \;
In addition, -exec
the return code of the called command is returned as the return value so that it can also be used for filtering purposes. If in the above example only the file names of the files in which the function is used are searched, this can be done by:
find /some/dir -type f -name '*\.c' -exec grep -q 'myfunc(' {} \; -print
happen.
Time measurement and comparisons
Various operand ( -atime
, -ctime
, -mtime
) allow comparing time stamps of files, with the comparative periods in days are given. In fact, these periods are interpreted as the result of an integer division of the time difference in seconds by 86400. The standard leads in its explanations as an example (here completed)
find /some/dir -atime 2
that finds all files (or other file system entities) whose time of last access is between 48 and 72 hours before the command was executed.
Notes and restrictions
Numeric arguments
Different operands (for example -size
or -atime
) expect numeric arguments. In such cases, the specification n (for numerical values n ) always means exactly n , -n, on the other hand, means less than n and + n means greater than n .
Be careful with -exec
Since it is often find
not clear how many hits are ultimately in the result set when calling , the use of -exec
can lead to a disproportionate load on the system, since a fork () system call (characterized by comparatively high resource consumption ) must be carried out for each hit .
Execplus
Implementations that conform to the POSIX standard therefore have the plus ( +
) at the end of the external call . Here the external command is called with a list of hits at the same time, so that the mentioned load on the system due to many fork () s is reduced. At the same time, however, the return value of the command for an individual file becomes indeterminate, so that it -exec
can no longer act as a filtering operand.
Further problems with the use of -exec
can arise with complex commands: it is possible in principle to use nested commands, but ultimately there is find
no shell and therefore limited in the interpretation of such nested commands . In particular, typical shell constructions such as the logical link cmd1 && cmd2 fail within commands -exec
.
{}
and -exec
The argument {}
can only be used once in an -exec
operand, not more than that. In addition, it must be used as a single (that is, stand-alone) argument. The following constructions are therefore all invalid:
find /some/where -type f -exec mv {} {}.old \; find /some/where -type f -exec gzip {} >/packed/files/{} \;
For such purposes, a script should be written to which the file name is passed as a parameter and which is used as an argument in -exec
. For the first example:
$ cat > /tmp/mymove.sh <<EOF mv $1 ${1}.old EOF
$ find /some/where type f -exec /tmp/mymove.sh {} \;
Protection against infinite recourse
In practice it can happen that endless references are built up in file systems (by hard links or soft links ). The POSIX standard therefore stipulates that conforming versions must recognize this fact and cancel the recursion.
Nonstandard variants
The GNU project has a copy of the command as part of the findutils package. It differs from the POSIX-compliant original in a few ways.
Web links
-
find(1)
: find files - Open Group Base Specification -
find(1)
: search for files in a directory hierarchy - Debian GNU / Linux executables or shell commands man page -
find(1)
: walk a file hierarchy - OpenBSD General Commands Manual - Linux and Unix find command tutorial with examples
Individual evidence
- ↑ a b c find (Opengroup Base Specifications Issue 6). Accessed March 8, 2018 .
- ↑ However, the designation was not used until 1978 for this line of development, in order to distinguish it from PWB / UNIX and MERT , see Bell System Technical Journal Vol. 57, No 6, Pt. 2 Jul / Aug 1978
- ↑ M. Douglas McIlroy : A Research Unix reader: annotated excerpts from the Programmer's Manual, 1971-1986. (pdf) Retrieved March 8, 2018 (English).
- ↑ The Open Group Base Specifications Issue 7, 2018 edition, chap. 12. Utility Conventions. Retrieved May 15, 2019 .