Introduction
The find
command in Linux is a powerful tool used to search for files and directories within a specified path based on different criteria. Moreover, it allows users to locate files by name, type, size, permissions, and more, making the tool essential for file management and system administration.
This tutorial shows you how to locate and manage files in Linux with the find
command.
Prerequisites
- A Linux system (this tutorial uses Ubuntu 22.04).
- Access to the terminal.
- A user account with sudo or root privileges
find Command Syntax
The basic find
command syntax uses the following format:
find [options] [path] [expression]
- Options. Customize the
find
output. - Path. Instructs
find
where to start looking for the search term. - Expression. Tells
find
what information category it's supposed to look for.
find Command Options
The find
command has many options that modify how it works. The following table presents common find
options:
Options | Description |
---|---|
-name | Searches for files or directories by name (case-sensitive). |
-prune<br> | Follows symbolic links. The command operates on the files or directories that the links point to rather than the links themselves. |
-not | Negates a condition. |
-empty | Searches for empty files and directories. |
-mindepth | Starts searching only after a certain directory depth. |
-maxdepth<br> | Limits the search to a specific depth. |
-print | Displays the full file path of each match. This is the default action if no other action is specified. |
-exec | Executes a command on each file found. |
-perm | Searches for files with specific permissions. |
-group | Finds files owned by a specific group. |
-user<br> | Delivers files owned by a specific user. |
-ctime | Searches for files with their status changed within a certain number of days. |
-atime | Delivers files accessed within a certain number of days. |
-mtime | Searches for files modified within a certain number of days. |
-size | Finds files of a specific size. |
-type<br> | Searches for files of a specific type (f for files, d for directories, l for symbolic links). |
-iname | Finds files or directories by name (case-insensitive). |
-L | Follows symbolic links. The command operates on the files or directories that the links point to, rather than the links themselves. |
-H | Follows symbolic links in the command itself but not the links encountered while searching. |
-P | Default behavior. Does not follow symbolic links. The search does not resolve or act on the files or directories the links point to. |
find Command optimization levels
The find
command has three optimization levels. An optimization level in the find
command determines how search operations are prioritized and executed for efficiency. Therefore, the command affects the order in which different search types (such as by name or type) are performed, impacting the overall speed and performance of the search process.
Higher optimization levels aim to maximize search speed, while lower levels focus on specific criteria first. The optimization levels are:
Optimization Level | Description |
---|---|
-O1 | Performs searches based on file names first. The default optimization level. |
-O2 | Runs searches based on type immediately after name-based searches, optimizing for type-based queries. |
-O3 | Maximizes search speed regardless of other search criteria, optimizing overall performance. |
find Command Examples
The find
command is a versatile tool in Linux for locating files and directories based on various criteria. The following sections present practical examples demonstrating how to use find
to search by file name, type, size, and more.
Find Files by Name
The find
command allows you to locate files by their name and quickly identify specific files within a directory hierarchy.
To find a file by name, use the -name
option followed by the file's name in quotes. This searches the specified directory (and its subdirectories) for files that match the given name.
For example, find a file named test1.txt in your home directory with:
find ~ -name "test1.txt"
This command searches the current user's directory (~
) for any file named test1.txt
Find Files by Approximate Name
To find files with names that approximately match a given pattern, use the find
command combined with wildcards (*
). This is useful when you're unsure of the exact file name or if you're looking for files that share a common naming pattern.
For example, to search for text files with the term test, run:
find ~ -name "test*.txt"
Find Files by Extension
To search for files with a specific extension, use the find
command with the -name
option, followed by a pattern that includes the desired file extension.
For example, find all .txt files within the /Documents directory with:
find /home/sara/Documents -name "*.txt"
Find Files and Directories by Type
The find
command allows you to search for files or directories based on their type. Additionally, by using the -type
option, you specify whether you're looking for regular files, directories, symbolic links, or other types of files.
The file types are:
f
- Regular files.d
- Directories.l
- Symbolic links.
For example, to search for all directories within the /home/sara directory, use:
find /home/sara -type d
This command lists all directories in /home/sara and its subdirectories. However, to make it easier to read, pipe the command to less:
find /home/sara -type d | less
Find Files by Location
When searching for files based on their location, the find
command allows you to specify a directory path. The command searches search through that directory and its subdirectories. If you do not specify a filename, the command returns a list of all files and directories within the specified path.
For example, search for all files and directories across the entire hard drive with the following command:
sudo find /
The sudo command lets you see files that are available only with administrative privileges. Pipe the command to less
to make it easier to read:
sudo find / | less
To search the directory you're currently in, use a period (.
).
sudo find . |less
Use the tilde (~) character to search the current user's home directory:
sudo find ~ |less
Search all user directories under /home for a file named test1.txt
with:
sudo find /home -name "test1.txt"
To find the file test1.txt specifically within the sara user home directory, run:
sudo find /home/sara -name "test1.txt"
Find and Delete File
Use the find
command to locate files and delete them in one step. This action is useful for cleaning up files that match specific criteria, such as old log files or temporary files.
For example, delete file1.txt file with:
find /home/sara/Documents -name "file1.txt" -delete
The command has no output.
Find Empty Files and Directories
Use the find
command to search for empty files and directories on your system. The -empty
option is used to locate files and directories that have no content. For example, search for all empty files within the <em>/home/sara</em>
directory with:
find /home/sara/ -type f -empty
Find by Content
To search for files based on their content, use the grep command in combination with find
. The grep
command searches for specific text within files while find
locates the files to be searched.
For example, find all files within the /home/sara/Documents directory that contain the text example with:
find /home/sara/Documents -type f -exec grep -l "example" {} +
The command consists of:
find
. Searches for files and directories./home/sara/Documents
. Represents the directory where the search begins. Thefind
command recursively searches through this directory and its subdirectories.-type f
. Restricts the search to regular files only.-exec
. Allows you to execute a command on each file thatfind
matches. In this case, it is used to rungrep
on each file.grep
. Searches for text patterns within files.-l
. Instructsgrep
to list only the names of files that contain the specified text rather than displaying the matching lines."example"
. Represents the text string you are searching for within the files.{}
. Is the placeholder replaced by each file name thatfind
matches.grep
runs on these file names.+
. Ends the-exec
command and indicatesfind
should pass multiple filenames togrep
at once, rather than invokinggrep
separately for each file. This is more efficient than runninggrep
for each file individually.
Find by Size
Use the find
command to search for files based on their size. The -size
option allows you to specify file size criteria for the search. Sizes are:
b
represents 512-byte blocks (default unit if no suffix is used).c
is for bytes.k
is for kilobytes.M
is for megabytes.G
is for gigabytes.
For example, find files larger than one megabyte in the /home/sara/ directory:
find /home/sara/ -type f -size +1M
Search Files with Specific Permissions
Use the find
command to locate files based on their permissions. The -perm
option allows you to specify permission criteria for the search. Also, the option goes with different permission criteria. Permission criteria in the find
command specify the exact or relative permission settings to search for files.
Permissions criteria are:
- Exact permissions. Specifies exact permission bits.
- Permissions with a plus sign (
+
). Matches files with at least the specified permissions. - Permissions with a minus sign (
-
). Matches files with at most the permissions you specify.
For example, find files with exact permissions of 644
(read and write for owner, read-only for group and others) in the /home/sara directory:
find /home/sara -type f -perm 644 |less
Conclusion
This article explained how the find
command helps you search and locate files and directories. It explained the wide range of search criteria, including file names, types, sizes, permissions, and modification times that the command supports.
If you want to find files and directories quickly, learn how to use the locate command in Linux.