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. We’ve used a Ubuntu 20.04 install but you could run this how-to on 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.
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.