On this page 16 sections
- How find works
- Search by name
- Search by type
- Search by size
- Search by owner or group
- Search by permissions
- Find writable files for the current user
- Search by modification time
- Modification, access and metadata time
- Search newer than another file
- Limit recursion depth
- Exclude directories with prune
- Combine conditions
- Execute commands on results
- Use -ls and -printf
- Useful enumeration searches
How find works
find recursively walks a directory tree and evaluates each file against one or more conditions. The basic structure is: find <path> <conditions> <actions>. Multiple conditions can be combined to build precise searches.
find /path -type f -name "*.conf"Search by name
-name performs case-sensitive filename matching while -iname ignores case. Wildcards should normally be quoted so the shell does not expand them before find receives them.
find /etc -type f -name "*.conf"find /var/www -type f -iname "*.php"find / -type f -name "id_rsa" 2>/dev/nullSearch by type
-type restricts results to a specific filesystem object. The most common values are f for regular files, d for directories and l for symbolic links.
find /opt -type ffind /opt -type dfind / -type l 2>/dev/nullSearch by size
-size searches by file size. Prefix + means larger than the specified size and - means smaller. Common suffixes include c for bytes, k for KiB, M for MiB and G for GiB.
find /var -type f -size +100M 2>/dev/nullfind / -type f -size +10M -size -100M 2>/dev/nullSearch by owner or group
-user and -group locate files associated with a specific account or group. -nouser and -nogroup are useful for identifying files whose original UID or GID no longer maps to an existing account.
find / -user john 2>/dev/nullfind /opt -type f -user root -lsfind / -nouser 2>/dev/nullSearch by permissions
-perm allows searches using Unix permission bits. An exact mode matches only that permission set, while the - prefix means all specified bits must be present.
find / -type f -perm -4000 2>/dev/nullfind / -type f -perm -2000 2>/dev/nullfind / -type f -perm -0002 2>/dev/nullfind / -type d -perm -0002 2>/dev/nullfind / -not -type l -perm -o+wFind writable files for the current user
-writable checks whether the current process can write to an object. This is often more practical than manually interpreting permission bits because ACLs and the current identity may also affect access.
find /opt -type f -writable -ls 2>/dev/nullfind / -type d -writable 2>/dev/nullSearch by modification time
-mtime works in 24-hour units. -mmin uses minutes and is more useful when investigating recent system activity.
find /var/log -type f -mtime -1find /tmp -type f -mmin -30find /var/log -type f -mtime +30Modification, access and metadata time
-mtime tracks content modification time, -atime tracks access time and -ctime tracks inode metadata changes such as permissions or ownership. ctime is not the file creation time.
find /etc -type f -ctime -1find /home -type f -atime -1 2>/dev/nullSearch newer than another file
-newer compares modification timestamps against a reference file. This is useful when investigating everything changed after a known event.
find /etc -type f -newer /tmp/referenceLimit recursion depth
-maxdepth prevents find from descending too deeply. -mindepth can exclude the starting directory itself or shallow results.
find /opt -maxdepth 1 -type ffind /var/www -maxdepth 2 -type fExclude directories with prune
-prune prevents find from descending into matching directories. This is useful for excluding large or irrelevant trees such as proc, sys or node_modules.
find . -path "./node_modules" -prune -o -type f -name "*.js" -printCombine conditions
find supports logical operators. -a means AND, -o means OR and ! negates a condition. AND is implicit when conditions are written consecutively.
find /var/www -type f \( -name "*.php" -o -name "*.js" \)find /var/www -type f ! -name "*.log"Execute commands on results
-exec runs another command using each matching path. {} represents the current result. Ending with \; executes once per result, while + groups multiple paths into fewer command executions.
find /opt -type f -exec ls -lh {} \;find /etc -type f -name "*.conf" -exec grep -H "password" {} + 2>/dev/nullUse -ls and -printf
-ls produces detailed metadata directly from find. -printf allows custom output, making it useful for scripts and investigations.
find /opt -type f -lsfind /opt -type f -printf '%M %u %g %s %p\n'Useful enumeration searches
Some searches are especially useful during Linux administration and authorised security assessments: privileged binaries, writable configuration files, SSH keys, backups and recently modified files.
find /home -type f -name "id_rsa" 2>/dev/nullfind / -type f \( -name "*.bak" -o -name "*.old" -o -name "*.backup" \) 2>/dev/nullfind /etc -type f -writable 2>/dev/null