Finding a file and directory in Linux is easier than in Windows. The Linux find command provides many options to help you find any file type in the file system. The find command allows you to find files and directories and take action based on the results, for example, copying, moving, deleting, or changing permissions. The locate command can also be used to find files by their filename. The locate command is lightning-fast because a background process continuously finds new files and stores them in a database.

This post will show you how to find files and directories using the find and locate command.

Step 1 – The Basic Syntax

The basic syntax of the find command is shown below:

find  directory-path option search-term

Where:

  • directorypath – Where you want to find the file.
  • option – It could be file name, type, date of creation, etc.
  • search-term – Where you can specify keywords to search.

Step 2 – Find File by Name

To find a file by name, use the following syntax:

find . -name filename

For example, to find a file named file1.txt in your current working directory, run:

find . -name file1.txt

If you want to perform a case-insensitive search, run:

find . -iname file1.txt

Step 3 – Find a File by Type

You can find the type of file using the -type parameter as shown below:

find -type query

Some example file types are shown below:

  • f – Regular file
  • d – Directory
  • l – Symbolic link
  • c – Character devices
  • b – Block devices

For example, to find a regular file, run the following command:

find . -type f -name "file1"

To find a directory, run:

find . -type d -name "Downloads"

Step 4 – Find a File by Time

You can also find the file based on access time, modified time, and change time.

For example, to find all files modified more than two days ago, run the following:

find /opt -ctime +2

This will find all files inside /opt directory, which was changed two days ago.

To find all files modified less than one day ago, run the following:

find /opt -ctime -1

To find all files modified more than 10 minutes ago, run the following:

find /opt -mmin +10

Step 5 – Find a File by Size

The find command will allow you to find files and filter the results by size. You can use the –size parameter to filter the result.

Some of the most commonly used options with –size is shown below:

  • c – Bytes
  • k – Kilobytes
  • M – Megabytes
  • G – Gigabytes
  • b – 512-byte blocks

For example, to find a file that is exactly 10MB in size, run:

find / -size 10M

To find a file that is greater than 10MB, run:

find / -size +10M

To find a file that is less than 10MB, run the following:

find / -size -10M

Step 6 – Find a File by User and Group

You can use the option –user and –group to find a file owned by user and group.

For example, to find all files owned by user tom, run:

find / -user tom

To find all files owned by group sales, run:

find / -group sales

Step 7 – How to Use Locate Command to Find File

The locate command is an alternative to the find command. It builds a database of files on the system, so searches will be faster.

To install the locate command in CentOS and RHEL, run the following:

yum install mlocate -y

To install the locate command in Debian and Ubuntu, run:

apt-get install mlocate -y

After installing the locate command, update the database using the following command:

updatedb

Now, to use the locate command to find a file named file.txt, run:

locate file.txt

You can also find a file by extension. For example, to find all files ending in .txt, run:

locate *.txt

Conclusion

In the above guide, we explained how to find a file and directory using the find and locate command. You can now quickly find any file using the find and locate command. Try it today on VPS hosting from Atlantic.Net.