find command
Updated 29 October 2024
Usage
bash
$ find [path] [options] [action]File types
| Type | Description |
| -type b | block (buffered) special |
| -type c | character (unbuffered) special |
| -type d | directory |
| -type p | named pipe (FIFO) |
| -type f | regular file |
| -type l | symbolic link |
| -type s | socket |
| -type d | door (Solaris) |
File sizes
| Size | Description |
| -size b | 512-byte blocks (default) |
| -size c | Bytes |
| -size k | Kilobytes |
| -size M | Megabytes |
| -size G | Gigabytes |
Find files based on size
The + and - prefixes signify greater than and less than, as usual.
| Example | Description |
|---|---|
| $ find / -size -15K | find files less than 15 Kilobytes |
| $ find / -size +15M | find files greater than 15 Megabytes |
| $ find / -size +15K -size -15G | find files greater 15 Kilobytes and less than 15 Gigabytes |
Find files based on time and date
| Example | Description |
|---|---|
| $ find / -mtime +0 | find files modified greater than 24 hours ago |
| $ find / -mtime 0 | find files modified between now and 1 day ago |
| $ find / -mtime -1 | find files modified less than 1 day ago (same as -mtime 0) |
| $ find / -mtime 1 | find files modified between 24 and 48 hours ago |
| $ find / -mtime +1 | find files modified more than 48 hours ago |
| $ find / -mtime 30 | find files last modified 30 days ago |
| $ find / -mtime +30 –mtime -120 | find files last modified between 30 and 120 days |
| $ find / -mmin -30 | find files last modified in 30 minutes ago |
| $ find / -mtime +1w | find files last modified more than 1 week ago |
| $ find / -atime 0 | find files last accessed between now and 24 hours ago |
| $ find / -atime +0 | find files accessed more than 24 hours ago |
| $ find / -atime 1 | find files accessed between 24 and 48 hours ago |
| $ find / -atime +1 | find files accessed more than 48 hours ago |
| $ find / -atime 30 | find files last accessed 30 days ago |
| $ find / -amin -30 | find files last accessed 30 minutes ago |
| $ find / -mtime -1 | find files accessed less than 24 hours ago (same as -atime 0) |
| $ find / -ctime -5h45m | find files with file status changed within the last 6 hours and 30 minutes |
Options
| Option | Description |
|---|---|
| -name | find files by name. |
| -iname | like -name, but the match is case insensitive |
| -type | find specific file types. |
| -size | find files by their sizes. |
| -perm | find files by their permissions. |
| -user | find files by their owner. |
| -group | find file based on the group they belong to. |
| -mtime | find files by their modification time. |
| -atime | find files by the time they where accessed. |
| -ctime | find files files based on the time their status were changed. |
| -regex | use regular expressions with find. |
| -empty | find empty files. |
| -readable | find files which are readable by the current user. |
| -writable | find files which are writable by the current user. |
| -executable | find files executable by the current user. |
| -maxdepth | levels of directories to go when searching files. |
| -minlevels | do not search for files at levels less than the specified level. |
| -not | negate an option. |
Find files based on permissions
| Example | Description |
|---|---|
| $ find . -type f -perm 0667 | find the files whose permissions are 667 |
| $ find / -perm /u=s | find files with SUID bit set |
| $ find / -perm /g=s | find files with SGID bit set |
| $ find / -perm /u=r | find files with read-only permission |
| $ find / -perm /u=w | find files with write-only permission |
| $ find / -perm /u=x | find files with executable-only permission |
Actions
| Action | Description |
|---|---|
| -delete | delete matching files or directories |
| -exec | execute commands on matching files, for example ls, rm and so on |
| -fls | write the output to a file |
| -fprint | print the full file name into file file |
| -ls | print matched file information for example size, permissions, owner and so on |
| -ok | like -exec but ask the user first. If the user agrees, run the command |
| print the full file name on the standard output, followed by a newline. If you are piping the output of find into another program and there is the faintest possibility that the files which you are searching for might contain a newline, then you should seriously consider using the -print0 option instead of -print | |
| -print0 | print the full file name on the standard output, followed by a null character (instead of the newline character that -print uses). This allows file names that contain newlines or other types of white space to be correctly interpreted by programs that process the find output. |
| -printf | rint format on the standard output, interpreting \\' es capes and %' directives. |
Condition flow
| Condition | Example | Description |
|---|---|---|
| \! or -not (but not POSIX compliant) | $ find / \! -name "*.txt" or $ find / -not -name "*.txt" | find or the files without the .txt extension |
| \( x -or y \) | $ find / -type f \( -name "*.sh" -o -name "*.txt" \) | find all files with .sh or .txt extension |
Find files and perform specif action
Find and replace
| Command | Action |
| $ find ./ -type f -exec sed -i 's/var/let/g' {} \; | Find all files and replace var with let |
| $ find ./ -type f -readable -writable -exec sed -i "s/var/let/g" {} \; | Find readable and writable files and modify the content varto let |
Find and sort
| Command | Action |
| $ find . -type f | sort | Find files and sort in ascending |
| $ find . -type f | sort -r | find files and sort descending |
Find and move
| Command | Action |
| $ find . -name '*.mp3' -exec mv {} ~/Documents \; | Find and move it to a specific directory (~/Documents) |
Find and rename
| Command | Action |
| $ find . -type f -name 'file*' -exec mv {} {}.bak\; | Find and suffix (added .bak) |
| $ find ./ -depth -name "*.html" -exec sh -c 'mv "$1" "${1%.html}.gohtml"' _ {} \; | Find and rename extension (.html => .gohtml) |
Find and concatenate
| Command | Action |
| $ find download -type f -iname '*.csv' | xargs cat > merged.csv | Merge all csv files in the download directory into merged.csv |
| $ find download -type f -iname '*.csv' | sort | xargs cat > merged.csv | Merge all sorted csv files in the download directory into merged.csv |
Find and copy
| Command | Action |
| $ find . -name '*2023*.xml' -exec cp -r "{}" /tmp/backup \; | Find matching files and copy to a specific directory (~/backup) |
Spotted a mistake or want something added? Send me a note.