Understanding the find Command

Basic Usage of find

The find command is used to search for files and directories in a directory hierarchy. It can search by name, size, modification date, permissions, and more.

find [path] [options] [expression]

Where [path] specifies where to search, [options] are additional search criteria, and [expression] is the condition for matching files.

Options Available with find

find [path] -name [filename] (Search by Name)

Finds files with a specific name:

find /home -name "file.txt"

This will search for all files named file.txt in the /home directory.

find [path] -iname [filename] (Case-Insensitive Search by Name)

Finds files with a specific name, ignoring case:

find /home -iname "file.txt"

This will search for file.txt or FILE.TXT (case-insensitive) in the /home directory.

find [path] -type [f|d] (Search by File Type)

Searches for files (-type f) or directories (-type d):

find /home -type f

This will search for all regular files in the /home directory.

find [path] -size [size] (Search by Size)

Finds files based on their size. You can use the following units:

find /var/log -size +50M

This searches for files larger than 50MB in the /var/log directory.

find [path] -mtime [+|-|n] (Search by Last Modification Time)

Finds files modified in the last n days:

find /home -mtime +7

This will find files in the /home directory that were modified more than 7 days ago.

find [path] -perm [mode] (Search by File Permissions)

Searches for files with specific permissions:

find /home -perm 644

This will find files in the /home directory that have permission 644 (read and write for owner, read for group and others).

find [path] -user [username] (Search by File Owner)

Finds files owned by a specific user:

find /var/www -user root

This will search for files owned by the user root in the /var/www directory.

find [path] -exec [command] {} \; (Execute Command on Found Files)

Executes a command on each found file. The {} is replaced with the file name:

find /home -name "*.txt" -exec rm {} \;

This will search for .txt files in /home and delete them.

find [path] -delete (Delete Found Files)

Deletes all found files:

find /home -name "*.tmp" -delete

This will search for .tmp files and delete them.

find [path] -empty (Search for Empty Files or Directories)

Finds empty files or directories:

find /home -empty

This will search for empty files or directories in the /home directory.

find --help

Displays help information for the find command:

find --help

Summary of Options

Option Description
find [path] -name [file] Search by file name (case-sensitive).
find [path] -iname [file] Search by file name (case-insensitive).
find [path] -type f Search for regular files.
find [path] -type d Search for directories.
find [path] -size [size] Search by file size.
find [path] -mtime [n] Search by modification time.
find [path] -perm [mode] Search by file permissions.
find [path] -user [user] Search by file owner.
find [path] -exec [command] {} \; Execute a command on found files.
find [path] -delete Delete found files.
find [path] -empty Search for empty files or directories.
find --help Display help for the find command.

Manual Pages

For more detailed information, use the manual page for the find command:

man find