Back to the command reference

find

FilesFundamentals In simulator In real Linux

Search 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 PATTERNMatch the file name; quote wildcards
-iname PATTERNLike -name, case-insensitive
-path PATTERNMatch the whole path, e.g. -path "*/cache/*"
-type TYPEf for files, d for directories
-maxdepth NDescend at most N levels
-mindepth NStart matching N levels down (-mindepth 1 skips the starting point)
-size [+-]NBy size, e.g. +10M
-mtime [+-]NBy modification time, e.g. -7 for the last 7 days
-mmin [+-]NBy modification time in minutes: -mmin -30 is changed in the last 30 minutes
-atime / -ctime [+-]NBy access time / status change (mode, owner), in days
-regex PATTERNThe whole path matches a regular expression (-regextype picks the flavour)
-newer FILEModified more recently than FILE
-perm MODEBy permissions: -perm 644 exactly 644; -perm -4000 at least setuid
-user NAMEOwned by the user NAME
-group NAMEOwned by the group NAME
-emptyEmpty files and directories
-notNegate the next test; also written ! (quote or escape it)
-oOr: either test will do, usually in parentheses: \( -name "*.c" -o -name "*.h" \)
-pruneDo not descend into this directory (as in -path ./.git -prune -o …)
-execRun a command on each match, e.g. -exec rm {} \;
-deleteDelete what is found (look at the list without -delete first)
-print0Separate results with NUL, for xargs -0; safe with spaces in names
-lsList what is found like ls -l
-LFollow symbolic links into what they point to
-xdev / -mountStay 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

Related