This article will go through how to find a file in Linux. When working with Linux systems, most of the time you will be required to search for files and directories. In this guide, we will look at the find and locate command which is commonly used to search for files in a directory hierarchy.
How to find a file in Linux using the find command
In the examples below, we will go through how to find a file using different criteria.
Find File by Name
- To find a specific file by name we will use the
find -iname "filename". The command below will search for a file named demofile.txt in the
/home/itnixprodirectory. However, the file name will be case-sensitive.
find /home/itnixpro -name demofile.txt
- To search for a file without the case sensitivity, use the
find /home/itnixpro -iname DeMofiLe.txt
Find File by Type
- You can perform file search queries by type by using the
-typeoption. Check the common descriptors used to describe file types.
f: regular file
l: symbolic link
c: character devices
b: block devices
p: named pipe (FIFO)
- To find directories in a specific user’s home directory
find /home/itnixpro -type d
- To find specific types of files e.g. files that end with
find /home/itnixpro -type f -name "*.txt"
Find File by Size
- Files can also be searched by their file size by using the
- Below are some of the suffixes used to identify file size.
b: 512-byte blocks
w: two-byte words
- To find files with exactly 0 bytes size under
/home/itnixprodirectory use the command below.
find /home/itnixpro -size 0c
- Check for files that are less than 100 megabytes.
find /home/itnixpro -size -100M
- Search for files that are more than 1 gigabyte.
find /home/itnixpro -size +1G
Find File by Time
- Files can be searched with time such as access time(the most recent read-write operation on a file), change time(the most recent modification to the file’s content) and access time(the most recent modification to the file’s inode metadata).
Access Time – command used
Modification Time – command use
Change Time – command used
- Look for files that were accessed less than five days.
find /home/itnixpro -atime -5
- Look for files that changed during the last three days.
find /home/itnixpro -mtime 3
- Find files whose metadata changed more than five days ago.
find /home/itnixpro -ctime +5
Find File by Owner and Permissions
- Search for files in the
/homedirectory that is owned by the
find /home -user itnixpro
- When searching files based on permissions, use the
-permoption. Let’s search for files with 644 permission inside the
/vardirectory in the example below.
find /var -perm 644
- To search for files with one permission option such as group or user, add
/before the permission.
find . -perm /644
How to find a file in Linux using the locate command
- In the example below we will go through how to search for files using the locate command which is a faster way than the find command. The locate command is not installed by default on most Linux systems.
- Install locate command on Debian distributions such as Ubuntu, Kali Linux, Zorin, POP, etc. using the command below.
sudo apt install mlocate
- Install locate command on Red Hat distributions such as CentOS, AlmaLinux, Rocky Linux, etc. using the following command.
sudo dnf install mlocate
- Update locate command database before using it so that you can find the latest search results because locate command depends on its database which is updated once a day automatically by a cron script.
- To locate a file e.g.
demofile.txtuse the command below.
- To count the number of files that exists in the search query such as
locate -c demofile.txt
- When searching using locate command, you can ignore case sensitivity by using the
locate -i DeMoFilE.txt
- Use the
-noption to limit the number of search results in the example of
locate demofile.txt -n 5
- That concludes our article on how to find a file in Linux. We have gone through use cases of find and locate commands.
Read more about Find command
Read more about Locate command