Introduction
The find
command allows you to search for a specific string of characters using your Linux command-line interface. It is a highly practical tool as it is not limited to file names. You can use it to search a specific location, add options to control the behavior of the search, and use additional commands to tell it how to manage the data it finds.
This tutorial shows you how to locate and manage files in Linux with the find
command.
Prerequisites
- Access to a terminal window/command line
- A user account with sudo privileges
Basic find Command Syntax
The basic syntax of the find
command uses the following format:
find <options> <location> <expression> <action> <name>
- Location – Instructs
find
where to start looking for your search term. - Expression – Tells
find
what category of information it’s supposed to look for. - Action – The action attribute executes a command on the search results. It is an optional element.
- Name – Represents the string of text (or other data) you are searching for.
- Options – This attribute is not a mandatory element of the
find
command. It can be used to control the behavior and optimize the search process.
How to Use find Command
The find
command lists all instances of the character string you included in your search. The find
command also enables you to define search parameters, options, and locations with a high level of accuracy.
Find Files by Location
Searching for files based on their location string can be a simple command such as:
sudo find /home/user filename
This command searches the user account in the home directory. If you are not certain where you want to look, you can widen the search location.
For example, if you have two user accounts and you don’t remember which one you saved a file to, you can enter:
sudo find /home filename
The find
command searches everything under the /home directory (including all user directories) for your search term. In our example, the search term is filename.
Use a period .
to search the directory you’re currently in:
sudo find .
Enter a slash /
, and your system is going to search the entire hard drive:
sudo find /
The tilde character ~
is used to search the home directory of the current user:
sudo find ~
Use Expressions with find Command
Expressions apply additional parameters to the find
command search process. The most common expression is -name
. This expression searches the location you’ve specified for a file with the name you type in the name field.
sudo find –name settings.txt
The system will search the current directory for any file with the name settings.txt.
The system only displays filenames with an exact match to your search terms. This limitation might cause problems if a filename has capital letters, for example.
To tell find
to ignore upper/lower case, use the –iname
expression, as follows:
sudo find /home –iname filename
The system searches the /home directory, and return anything named filename, FileName, and even fIlEnAmE.
Use the –type d
expression to specify if you are searching for a file or a directory:
sudo find . –type d –name Videos
This command would search the current directory (with the period) for a directory with the name “Videos.”
By default, find
searches for files, but you can specify files by using -type f
.
Show all files that don’t include the searched name:
sudo find . -not filename
Search for files with a given permission level:
sudo find . -perm 0000
Return list of all files owned by specific username:
sudo find . -user username
Show the last 00 files modified:
sudo find . -mtime 00
The numerical value defines the number of files the system displays. Show the last 00 files accessed:
sudo find . -atime 00
Show all files greater than 20 megabytes:
sudo find . -size +20M
The terminal presents a list of all files that match the size criteria.
Commands referencing the size of the searched items can be used individually to show all files larger than a certain size, smaller than a certain size, or within a defined range.
Find Name or Extension
The name
attribute defines the object of your search. The find
tool is going to the specified location for whatever string of characters you enter. Typically, the name of the file you’re looking for comes at the end of the command, after the location, options, and expressions.
Standard letters and numerals are entered as a string of characters at the end of the command. Special characters – a wildcard, a period, a slash, etc. – require the use of single quotes around the name. Single quotes ensure that the system interprets the request correctly.
Enter the following command to search for files with a specific extension, like .html or .php:
sudo find . –type f –name ‘*.html’
The system searches the current directory for regular files (type –f
) that have an extension of .php. In this case, the asterisk (the * sign) is used as a wildcard, meaning that it can stand in for any number of characters of any type.
Wildcards are useful if you only remember part of a filename, or are searching for files with a particular extension.
Perform Actions with find Command
We have seen how to use the find
command to display search results in your terminal window. An action attribute can enhance that functionality and perform a particular task once a file is located.
The following command searches the actual content of files (instead of just the names of files):
sudo find ~ –type f –exec grep “contents” ‘{}’ \; –print
- We are asking the system to find a file (
-type f
) in our user’s home directory (~
). - We instruct the system to take action (
-exec
) by running thegrep
command to search through the contents of all the files for the word contents. - The
{}
brackets indicate the match results of thefind
command, and they’re in single quotes so that thegrep
command doesn’t misinterpret them. - We’re closing out the
grep
command with a semicolon (;
), and we’re using a backslash (\
) so that the system doesn’t misinterpret the semicolon as an instruction. - Finally, the
–print
option tells the system to display the results.
Copy and paste this command into your terminal window, and substitute your search path for the ~ character, and your search term for the word contents.
You can use the find –exec
combination to change file permissions quickly:
sudo find /path –name “filename.ext” –exec chmod 777 ‘{}’ \;
This command uses the –exec
command to run chmod
to change the file permissions of the file filename.ext so that it can be read, written, and executed by everyone.
Locate and Delete Files with find Command
With the –delete
command, you can quickly find and remove one or multiple files in one command.
Note: Exercise extreme caution when using the -delete action. Deleted files may be unrecoverable, and deleting system files can make your system unstable. The find
command is an expression. Placing -delete
in front prompts find
to try and delete everything before the specified starting point. If you intend on permanently deleting Linux files, we recommend the shred command.
In this example, find
deletes all files that end with the .html extention.
sudo find . –name “*.html” –delete
This command is going to search the current directory (displayed in your command prompt) for all files with the .html extension, and delete them.The image illustrates the simplicity of the process.
This type of action is useful for removing old backup files if you substituted “*.bak” instead of “*.bmp”.
Add Options to find Command
Options are special functions inserted before the search path in a find
command string. The -L
, -H
, and -P
are optional elements used to control the handling of symbolic links.
–P
This option represents the default behavior. The system does not follow symbolic links.-H
The system does not follow symbolic links except while processing this specific command.-L
Follows symbolic links. The information is taken from the file to which the link directs to. Not for the link itself.
Options can change the search optimization level. There are three (3) optimization levels that define how the find program organizes searches and the speed of execution of that search.
- –O1 This optimization level is used by default. Searches based on the names of files are run first.
- –O2 Searches based on type are run right after searches based on names. Before any other searches take place.
- –O3 Searches are performed to maximize speed. Regardless of other query elements.
Conclusion
The find
command in Linux can help you keep track of files on a shared system, locate a file that’s gone missing, or that has been accidentally moved. You can use it to search for a file that you created or saved, but don’t recall which directory it was saved to.
The find tool can help administrators with system management. The ability to use grep
to search the contents of files can be especially useful when looking for keywords within files.
If you are looking for a faster way to find files and directories, read our article on how to use the locate command in Linux.