
How to use the find command in linux:
In this how-to, we’ll look at various ways of using the find command to help us find files and directories across the Linux filesystem. Sometimes we misplace a file or directory and we can spend precious time searching via the terminal. On the Linux desktop, the file manager will have a builtin search tool, as does the terminal. The find command is immensely useful, and exceptionally easy to use.
Whilst you become accustomed to these commands, it’s good to work with test files and directories and you should take extra care to ensure you are carefully following the instructions.
All the commands in this how-to will work on most Linux machines. I’ve used a Ubuntu 20.04 install but you could run this how-to on anything from a Windows emulator to a Raspberry Pi. All of the how-to is performed via the Terminal. You can open a terminal window on most Linux machines by pressing ctrl, alt and t.
Locate Linux Files by Their Name or Extension
Type find into the command line to track down a particular file by its name or extension. If you want to look for *.err files in the /home/username/ directory and all sub-directories, try this: find /home/username/ -name "*.err"
Typical Linux Find Commands and Syntax
find command expressions look like this:
find command options starting/path expression
The options attribute controls the behavior and optimization method of the find process. The starting/path attribute defines the top-level directory where the find command in Linux begins the filtering process. The expression attribute controls the assessments that scour the directory tree to create output.
Let’s break down a Linux find command where we don’t just want Linux find file by name:
find -O3 -L /var/www/ -name "*.html"
It enables the top-level optimization (-O3) and permits find to follow symbolic links (-L). The find command in Linux searches through the whole directory hierarchy under /var/www/ for files that have .html on the end.
Basic Examples
1. find . -name thisfile.txt
If you need to know how to find a file in Linux called thisfile.txt, it will look for it in current and sub-directories.
2. find /home -name *.jpg
Look for all .jpg files in the /home and directories below it.
3. find . -type f -empty
Look for an empty file inside the current directory.
4. find /home -user randomperson-mtime 6 -iname ".db"
Look for all .db files (ignoring text case) that have been changed in the preceding 6 days by a user called randomperson.
Options and Optimization for Find Command for Linux
find is configured to ignore symbolic links (shortcut files) by default. If you’d like the find command to follow and show symbolic links, just add the -L option to the command, as we did in this example.
find can help Linux find file by name. The Linux find command enhances its approach to filtering so that performance is optimised. The user can find a file in Linux by selecting three stages of optimisation-O1, -O2, and -O3. -O1 is the standard setting and it causes find to filter according to filename before it runs any other tests.
-O2 filters by name and type of file before carrying on with more demanding filters to find a file in Linux. Level -O3 reorders all tests according to their relative expense and how likely they are to succeed.
-O1
– (Default) filter based on file name first-O2
– File name first, then file-type-O3
– Allow find to automatically re-order the search based on efficient use of resources and likelihood of success-maxdepth X
– Search this directory along with all sub-directories to a level of X-iname
– Search while ignoring text case.-not
– Only produce results that don’t match the test case-type f
– Look for files-type d
– Look for directories
Find Files by When They Were Modified
The Linux find command contains the ability to filter a directory hierarchy based on when the file was last modified:
find / -name "*jpg" -mtime 5
find /home/randomuser/ -name "*jpg" -mtime 4
The initial Linux find command pulls up a list of files in the whole system that end with the characters jpg and have been modified in the preceding 5 days. The next one filters randomuser’s home directory for files with names that end with the characters “conf” and have been modified in the preceding 4 days.
Use Grep to Find Files Based on Content
The find command in Linux is great but it can only filter the directory tree according to filename and meta data. To search files based on what they contain you’ll need a tool like grep. Take a look:
find . -type f -exec grep "forinstance" '{}' ; -print
This goes through every object in the current directory tree (.) that’s a file (-type f) and then runs grep ” forinstance ” for every file that matches, then prints them on the screen (-print). The curly braces ({}) are a placeholder for those results matched by the Linux find command. The {} go inside single quotes (‘) so that grep isn’t given a misshapen file name. The -exec command is ended with a semicolon (;), which also needs an escape (;) so that it doesn’t end up being interpreted by the shell.
Before -exec was implemented, xargs would have been used to create the same kind of output:
find . -type f -print | xargs grep "forinstance"
Finding a File in Linux
To begin, let’s create some example files in a directory and then use the find command to find them.

1. Create a test folder containing test files.
After creating the test directory and files check the files have been created using ls.
mkdir test
cd test
touch test1.txt test2.h test3.c TEST.f
ls
2. In the test directory, find the file called test1.txt. Using find with “.” indicates that the search should be confined to the current working directory. After running the find command you should see the test1.txt file listed as a result.
find . -name test1.txt
Searching Using a Partial Filename in Linux
On occasion, we may need to search using partial file or directory names. Let’s look at how to do this and how searching for partial terms affects the results.
1. In the test directory run the following command searching for files that contain the term “tes” within their name.
find . -name "*tes*"
In the list of results, you should see that all the files have been found and listed apart fromTEST.f , this is due to -name returning case sensitive results. We’ll look at an alternative that returns non case sensitive results in a later section.
2. Repeat the command searching for a specific file extension.
We can use the same method to search for a particular file type. Changing the command to search for“*.txt*” will return only the .txt filetype.
find . -name "*.txt*"
3. Use -iname to return non-case sensitive results. Here we use the partial search term“*tes*” again but using -iname forces the command to show all results regardless of upper or lower case. Therefore the results include our file TEST.f .
find . -iname "*tes*"
Distinguishing Between Directories and Files in Linux
In its standard form, the find command will also return any matching results regardless be they files or directories. We can also add tags to the find command that force the command to only return files or only return directory results.
1. Add a directory inside our test directory called test2.
Use ls to confirm that the directory has been created.
cd test
mkdir test2
ls
2. Run a find command that will return both file and directory results.
You should see that the result contains all the test files and also the test2 directory.
find . -iname "*test*"
3. Add the -type f tag to return only file results.
Note that in the results the directory test2 is omitted.
find . -iname "*test*" -type f
4. Add the -type d tag to return only directory results. Note that the only result now should be the test2 directory.
find . -iname "*test*" -type d
Searching the Entire Filesystem in Linux
You may need to search the entire filesystem to try and find a misplaced or forgotten file.
1. Search for the test1.txt file from the root (/) of the filesystem.
This step isn’t tremendously successful and has been added to illustrate a common issue.
cd
find / -iname test1.txt
You will find that you don’t have permission to search in a lot of areas, this results in a long reported list of areas we can’t search and, although our test1.txt file has been located we need to search through the report list to find it. Notice in this example we use / to enable the command to search all sub directories.
2. Repeat the previous search but use sudo to add root privileges.
This then gives the command permission to access most places within the filesystem and as such the returned report will be much clearer and easier to read.
sudo find / -iname test1.txt
With these examples you should now have a basic tool set to find any file anywhere on your system, even if you only know a part of its name.
Extra stuff!
How to Change Permissions with File Manager
There are some web pages and files that you don’t necessarily want to share with the world, and that’s where altering their permissions settings can come in handy.
To achieve this, find the item you want to restrict Internet access for like this:
- Place your cursor over it and wait for the highlight to appear as in the previous example.
- Click on the file to open its context menu and do the same again on Change Permissions.
- Make your change and then hit OK. If you’d like to find out more about how to look at and alter permissions in Setting File and Directory Access Permissions.
File Manager’s default approach is to change permissions in a non-recursive manner, so consequently, sub-files and directories don’t aren’t affected by the changed permissions of the higher-level directories they belong to. With Plesk for Linux, you can make File Manager modify permissions in a recursive manner, assuming that your Plesk administrator set up the Permissions Recursive extension and that you understand the octal notation of file permissions.
To enable recursive editing of access permissions:
- Place the cursor over the directory and wait for the highlight.
- Click to open its context menu and then again on Set Permissions Recursive.
- Now you can edit them. “Folder Permissions” is talking about the higher-level directory and any of its associated sub-directories. “File Permissions” applies to sub-files in this instance.
- When you’ve completed your permission amendments, click OK.