Created
May 5, 2020 12:27
-
-
Save bsolomon1124/001eb27994a9ccf6f5c85ca1c03aceb7 to your computer and use it in GitHub Desktop.
Find - the good stuff
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DESCRIPTION & SIGNATURE | |
Signature (minus some options you don't need): | |
find [starting-point...] [expression] | |
GNU find searches the directory tree rooted at each given `starting-point` by | |
evaluating the given `expression` from left to right. | |
If no `starting-point` is specified, `.` is assumed. | |
`expression` is composed of: | |
- tests: evaluate to true or false (example: `-type f`) | |
- actions: have side effects(example: `-delete`) | |
- options: affect the behavior of the rest of command (example: `-depth`) | |
- operators: join together other items in `expression` (example: `-o`) | |
EXAMPLES | |
find --version | |
find -regextype help | |
find -iname '*foo*' | |
find -iname 'fo*' | |
find -iname 'F??' | |
find -path './sr*sc' | |
find -regex '.*bar' | |
find -regex '.*b.*3' | |
find . -name '*.c' -print | |
find /tmp -type f,d,l | |
find / -name needle -print -quit | |
find . -type f -exec file '{}' \; | |
find . \( -name afile -o -name bfile \) -print | |
find /tmp -name core -type f -print0 | xargs -0 /bin/rm -f | |
TESTS | |
-empty | |
File is empty and is either a regular file or a directory. | |
-name pattern | |
Base of file name matches shell pattern `pattern`. Don't forget to enclose the | |
pattern in quotes in order to protect it from expansion by the shell. | |
-iname pattern | |
Like -name, but the match is case insensitive. | |
-path pattern | |
File name matches shell pattern `pattern`. | |
The metacharacters do not treat `/' or `.' specially. | |
-ipath pattern | |
Like -path, but the match is case insensitive. | |
-regex pattern | |
File name matches regular expression pattern. | |
This is a match on the whole path, not a search. | |
The regular expressions understood by find are by | |
default Emacs Regular Expressions (except that `.' matches newline), | |
but this can be changed with the -regextype option. | |
-iregex pattern | |
Like -regex, but the match is case insensitive. | |
-type c | |
File is of type c: | |
b block (buffered) special | |
c character (unbuffered) special | |
d directory | |
p named pipe (FIFO) | |
f regular file | |
l symbolic link | |
s socket | |
D door (Solaris) | |
To search for more than one type at once, you can supply the | |
combined list of type letters separated by a comma `,` (GNU | |
extension). | |
-wholename pattern | |
Alias for -path. Prefer -path. | |
-iwholename pattern | |
Alias for -ipath. Prefer -ipath. | |
-lname pattern | |
File is a symbolic link whose contents match shell pattern `pattern`. | |
-ilname pattern | |
Like -lname, but the match is case insensitive. | |
ACTIONS | |
Print the full file name on the standard output, followed by a newline. | |
Because files may contain newlines, you should use -print0 if you are piping | |
to another command. | |
-print0 | |
Print the full file name on the standard output, followed by a NUL. | |
-delete | |
Delete files. *Remember to specify this last, not first.* | |
-exec command | |
Execute `command`. You should put the braces in single quotes | |
and backslash-escape the semicolon: `-exec file '{}' \;` | |
-execdir command ; | |
Like -exec, but the specified command is run from the subdirectory containing | |
the matched file, which is not normally the directory in which you started find. | |
-ok command ; | |
Like -exec but ask the user first. | |
-quit | |
Exit immediately. Useful for finding a single file. | |
OPTIONS | |
-regextype type | |
Changes the regular expression syntax understood by -regex and | |
-iregex tests which occur later on the command line. | |
Use `find -regextype help` to list them. | |
OPERATOR PRECEDENCE | |
( expr ) Force precedence | |
! expr True if expr is false | |
expr1 expr2 | |
expr1 -a expr2 And | |
expr1 -o expr2 Or | |
Incorrect: find .-name afile -o -name bfile -print | |
Correct: find . \( -name afile -o -name bfile \) -print | |
TRICKS & GOTCHAS | |
The expression is evaluated left to right with short-circuiting. | |
This means you should prefer `-name core -type f` so that the -name test comes | |
before the -type test in order to avoid having to call stat(2) on every file. | |
File names can contain any character except `\0` and `/`. You should therefore | |
use `-print0` instead of `-print` if you are using find in a script or in a | |
situation where the matched files might have arbitrary names: | |
Bad: find /tmp -name core -type f -print | xargs /bin/rm -f | |
Good: find /tmp -name core -type f -print0 | xargs -0 /bin/rm -f | |
Avoid shell expansion by enclosing patterns in quotes: | |
Bad: find . -name *.c -print | |
Good: find . -name '*.c' -print |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment