How to find a file in Linux

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 -name option e.g. find -iname "filename". The command below will search for a file named demofile.txt in the /home/itnixpro directory. 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 -iname option instead.
find /home/itnixpro -iname DeMofiLe.txt

Find File by Type

  • You can perform file search queries by type by using the -type option. Check the common descriptors used to describe file types.

f: regular file
d: directory
l: symbolic link
c: character devices
b: block devices
p: named pipe (FIFO)
s: socket

  • 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 .txt extension.
find /home/itnixpro -type f -name "*.txt"

Find File by Size

  • Files can also be searched by their file size by using the -size option.
  • Below are some of the suffixes used to identify file size.

b: 512-byte blocks
c: bytes
w: two-byte words
k: kilobytes
M: megabytes
G: gigabytes

  • To find files with exactly 0 bytes size under /home/itnixpro directory 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 -atime
Modification Time – command use -mtime
Change Time – command used -ctime

  • 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 /home directory that is owned by the itnixpro user.
find /home -user itnixpro
  • When searching files based on permissions, use the -perm option. Let’s search for files with 644 permission inside the /var directory 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.
sudo updatedb
  • To locate a file e.g. demofile.txt use the command below.
locate demofile.txt
  • To count the number of files that exists in the search query such as demofile.txt use the -c option
locate -c demofile.txt
  • When searching using locate command, you can ignore case sensitivity by using the -i option
locate -i DeMoFilE.txt
  • Use the -n option to limit the number of search results in the example of demofile.txt
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

Other Tutorial

Setup Nagios Passive Checks with NRDP

Install PostgreSQL on FreeBSD 13

Install PostgreSQL on Rocky Linux

System administrator | Software Developer | DevOps

Leave a Comment