find

what is find?

In Unix-like and some other operating systems, find is a command-line utility that locates files based on some user-specified criteria and then applies some requested action on each matched object.

It initiates a search from a desired starting location and then recursively traversing the nodes (directories) of a hierarchical structure (typically a tree). find can traverse and search through different file systems of partitions belonging to one or more storage devices mounted under the starting directory.

The possible search criteria include a pattern to match against the filename or a time range to match against the modification time or access time of the file. By default, find returns a list of all files below the current working directory, although users can limit the search to any desired maximum number of levels under the starting directory.

The related locate programs use a database of indexed files obtained through find (updated at regular intervals, typically by cron job) to provide a faster method of searching the entire file system for files by name.

How to use find

1 - find a file, or a directory based on the filetype

find /etc -type f # gets all files, starting from the /etc directory
find /etc -type d # gets all directories, starting from the /etc directory

2 - find anything based on the name of a file/directory

find /etc -name "NAME" # case sensitive
find /etc -iname "NAME" # not case sensitive

3 - find based on file permissions

find /etc -perm 0777 # finds anything in the /etc directory that has the permission 777
find /etc -type f -perm 0777 # finds all files in the directory /etc with 777 permission

4 - find based on file permissions, and change these permissions immediately

find /etc type f -perm 0777 -exec chmod 775 {} \; # find all 777 files, change them to 775

5 - find all files that were modified

find /etc -mmin -30 # find all files in the /etc directory that were modified in the last 30 minutes
find /etc -mtime -1 # find all files in the /etc directory that were modified the last day

6 - find based on file size

find /etc -size +10M # find all files/directories that have a volume, greater then 10MB
find /etc -size -10M # find all files/directories that have a volume, smaller then 10MB

Last updated