find

from Wikipedia, the free encyclopedia

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 findappeared 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 . findand cpiowere designed to be used together.

Working method

As a typical command line utility, its behavior is findregulated by section 12.2 of the standard ( Utility Syntax Guidelines ). Starting from a base directory to be specified, findthe 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 TRUEor 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 ( -lswhich ls -ailsresults 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 *.cin the directory (the file is specified so that the file names of the occurrences are also displayed): /some/dirmyfunc()/dev/nullgrep

find /some/dir -type f -name '*\.c' -exec grep 'myfunc(' /dev/null {} \;

In addition, -execthe 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 -sizeor -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 findnot clear how many hits are ultimately in the result set when calling , the use of -execcan 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 -execcan no longer act as a filtering operand.

Further problems with the use of -execcan arise with complex commands: it is possible in principle to use nested commands, but ultimately there is findno 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 -execoperand, 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

Individual evidence

  1. a b c find (Opengroup Base Specifications Issue 6). Accessed March 8, 2018 .
  2. 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
  3. M. Douglas McIlroy : A Research Unix reader: annotated excerpts from the Programmer's Manual, 1971-1986. (pdf) Retrieved March 8, 2018 (English).
  4. The Open Group Base Specifications Issue 7, 2018 edition, chap. 12. Utility Conventions. Retrieved May 15, 2019 .