find
FilesFundamentals In simulator In real LinuxSearch for files by name, type and more
Name: find
You can practise it right away in the simulator, and use it in real Linux mode too.
Description
find walks the directory tree from the starting points you give, evaluates an expression (tests like -name, -type, -size, -mtime) for every file, and performs an action on matches; the default action is to print the path.
Unlike locate it scans the disk live, so results are always current but can be slow on large trees. With -exec or xargs you can run commands on everything it finds.
Syntax
find [PATH]... [EXPRESSION]Common options
| -name PATTERN | Match the file name; quote wildcards |
| -iname PATTERN | Like -name, case-insensitive |
| -path PATTERN | Match the whole path, e.g. -path "*/cache/*" |
| -type TYPE | f for files, d for directories |
| -maxdepth N | Descend at most N levels |
| -mindepth N | Start matching N levels down (-mindepth 1 skips the starting point) |
| -size [+-]N | By size, e.g. +10M |
| -mtime [+-]N | By modification time, e.g. -7 for the last 7 days |
| -mmin [+-]N | By modification time in minutes: -mmin -30 is changed in the last 30 minutes |
| -atime / -ctime [+-]N | By access time / status change (mode, owner), in days |
| -regex PATTERN | The whole path matches a regular expression (-regextype picks the flavour) |
| -newer FILE | Modified more recently than FILE |
| -perm MODE | By permissions: -perm 644 exactly 644; -perm -4000 at least setuid |
| -user NAME | Owned by the user NAME |
| -group NAME | Owned by the group NAME |
| -empty | Empty files and directories |
| -not | Negate the next test; also written ! (quote or escape it) |
| -o | Or: either test will do, usually in parentheses: \( -name "*.c" -o -name "*.h" \) |
| -prune | Do not descend into this directory (as in -path ./.git -prune -o …) |
| -exec | Run a command on each match, e.g. -exec rm {} \; |
| -delete | Delete what is found (look at the list without -delete first) |
| -print0 | Separate results with NUL, for xargs -0; safe with spaces in names |
| -ls | List what is found like ls -l |
| -L | Follow symbolic links into what they point to |
| -xdev / -mount | Stay on this file system (common when searching all of /) |
Examples
$ find . -name "*.txt"
Find every .txt file below the current directory
Explain$ find projects -type d
List directories only
Explain$ find /etc -maxdepth 1 -name "host*"
Search only the top level of /etc
Explain$ find / -perm -4000 -type f 2>/dev/null
Every setuid program on the system (a common security check)
Explain