How to Find Files in Linux

0

In this tutorial, we will see different ways to use the to find command to help us find files and directories in the Linux file system. Sometimes we misplace a file or directory and can spend valuable time searching through the terminal. On the Linux desktop, the file manager will have a built-in search tool, just like the terminal. the to find The command is extremely useful and exceptionally easy to use.

While you get used to these commands, it’s good to work with test files and directories, and you should take extra care to ensure that you follow the instructions carefully.

All commands in this tutorial will work on most Linux machines. We used an Ubuntu 20.04 install, but you can run this guide on a Raspberry pie. All procedures are performed through the terminal. You can open a terminal window on most Linux machines by pressing ctrl, alt and t.

Find a file in Linux

(Image credit: Tom’s Hardware)

To start, let’s create some sample files in a directory, then use the to find command to find them.

1. Create a test folder containing the test files. After creating the test directory and files, verify that the files were 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 to find with “.” indicates that the search should be limited to the current working directory. After running the to find command, you should see the test1.txt file listed accordingly.

find . -name test1.txt

Search using partial filename in Linux

(Image credit: Tom’s Hardware)

Occasionally, we may need to search using partial filenames or directories. Let’s see how to do this and how searching for partial terms affects the results.

1. In the test directory run the following command looking for files containing the term “your” within their name.

find . -name “*tes*”

In the results list, you should see that all files were found and listed exceptTEST.f , it is because of -Name return case-sensitive results. We’ll look at an alternative that returns case-insensitive 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“*.SMS*” will only return the .txt file type.

find . -name “*.txt*”

3. Utilize -my name to return case-insensitive results. Here we use the partial search term“*your*” again but using -my name forces the command to display all results regardless of upper or lower case. The results therefore include our file TEST.f .

find . -iname “*tes*”

Distinguish between directories and files in Linux

(Image credit: Tom’s Hardware)

In its standard form, the to find The command will also return all matching results, whether files or directories. We can also add tags to the to find command that forces the command to return only files or only directory results.

1. Add a directory inside our test called directory test2. Utilize ls to confirm that the directory was created.

cd test
mkdir test2
ls

2. Run a to find command that will return both file and directory results. You should see that the result contains all the test files as well as the test2 phone book.

find . -iname “*test*”

3. Add the -type f tag to return results from the file only. Note that in the results the directory test2 is omitted.

find . -iname “*test*” -type f

4. Add the -type d tag to return results from the directory only. Note that the only result now should be the test2 phone book.

find . -iname “*test*” -type d

Search the entire filesystem in Linux

(Image credit: Tom’s Hardware)

You may have to search the entire file system to try to find a misplaced or forgotten file.

1. Search the test1.txt file from the root (/) of the file system. This step is not very successful and was added to illustrate a common problem.

cd
find / -iname test1.txt

You will find that you are not allowed to search in many domains, this results in a long list of domains that we cannot search and, although our test1.txt file has been located, we need to search the list of reports to find it. Notice in this example that we use / to allow the command to search all subdirectories.

(Image credit: Tom’s Hardware)

2. Repeat the previous search but use sudo to add root privileges. This then gives the command permission to access most places in 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 set of tools to find any file anywhere on your system, even if you only know part of its name.

Share.

Comments are closed.