find command

Updated 29 October 2024

Usage

bash
$ find [path] [options] [action]

File types

TypeDescription
-type bblock (buffered) special
-type ccharacter (unbuffered) special
-type ddirectory
-type pnamed pipe (FIFO)
-type fregular file
-type lsymbolic link
-type ssocket
-type ddoor (Solaris)

File sizes

SizeDescription
-size b512-byte blocks (default)
-size cBytes
-size kKilobytes
-size MMegabytes
-size GGigabytes

Find files based on size

The + and - prefixes signify greater than and less than, as usual.
ExampleDescription
$ find / -size -15Kfind files less than 15 Kilobytes
$ find / -size +15Mfind 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

ExampleDescription
$ find / -mtime +0find files modified greater than 24 hours ago
$ find / -mtime 0find files modified between now and 1 day ago
$ find / -mtime -1find files modified less than 1 day ago (same as -mtime 0)
$ find / -mtime 1find files modified between 24 and 48 hours ago
$ find / -mtime +1find files modified more than 48 hours ago
$ find / -mtime 30find files last modified 30 days ago
$ find / -mtime +30 –mtime -120find files last modified between 30 and 120 days
$ find / -mmin -30find files last modified in 30 minutes ago
$ find / -mtime +1wfind files last modified more than 1 week ago
$ find / -atime 0find files last accessed between now and 24 hours ago
$ find / -atime +0find files accessed more than 24 hours ago
$ find / -atime 1find files accessed between 24 and 48 hours ago
$ find / -atime +1find files accessed more than 48 hours ago
$ find / -atime 30find files last accessed 30 days ago
$ find / -amin -30find files last accessed 30 minutes ago
$ find / -mtime -1find files accessed less than 24 hours ago (same as -atime 0)
$ find / -ctime -5h45mfind files with file status changed within the last 6 hours and 30 minutes

Options

OptionDescription
-namefind files by name.
-inamelike -name, but the match is case insensitive
-typefind specific file types.
-sizefind files by their sizes.
-permfind files by their permissions.
-userfind files by their owner.
-groupfind file based on the group they belong to.
-mtimefind files by their modification time.
-atimefind files by the time they where accessed.
-ctimefind files files based on the time their status were changed.
-regexuse regular expressions with find.
-emptyfind empty files.
-readablefind files which are readable by the current user.
-writable find files which are writable by the current user.
-executablefind files executable by the current user.
-maxdepthlevels of directories to go when searching files.
-minlevelsdo not search for files at levels less than the specified level.
-notnegate an option.

Find files based on permissions

ExampleDescription
$ find . -type f -perm 0667find the files whose permissions are 667
$ find / -perm /u=sfind files with SUID bit set
$ find / -perm /g=sfind files with SGID bit set
$ find / -perm /u=rfind files with read-only permission
$ find / -perm /u=wfind files with write-only permission
$ find / -perm /u=xfind files with executable-only permission

Actions

ActionDescription
-deletedelete matching files or directories
-execexecute commands on matching files, for example ls, rm and so on
-flswrite the output to a file
-fprintprint the full file name into file file
-lsprint matched file information for example size, permissions, owner and so on
-oklike -exec but ask the user first. If the user agrees, run the command
-printprint 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
-print0print 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.
-printfrint format on the standard output, interpreting \\' es capes and %' directives.

Condition flow

ConditionExampleDescription
\! 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

CommandAction
$ 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

CommandAction
$ find . -type f | sort Find files and sort in ascending
$ find . -type f | sort -r find files and sort descending

Find and move

CommandAction
$ find . -name '*.mp3' -exec mv {} ~/Documents \;Find and move it to a specific directory (~/Documents)

Find and rename

CommandAction
$ 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

CommandAction
$ find download -type f -iname '*.csv' | xargs cat > merged.csvMerge all csv files in the download directory into merged.csv
$ find download -type f -iname '*.csv' | sort | xargs cat > merged.csvMerge all sorted csv files in the download directory into merged.csv

Find and copy

CommandAction
$ 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.