Atlantic.Net Blog

Find a File with Find and Locate Commands in Linux

Finding a file and directory in Linux is easier than in Windows. The Linux find command provides a lot of options designed to help you find any type of file 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.

In this post, we will show you how to find files and directories using the find and locate command.

Prerequisites

  • A Linux server on the Atlantic.Net Cloud Platform
  • A root password configured on your server

Step 1 – Create Atlantic.Net Cloud Server

First, log in to your Atlantic.Net Cloud Server. Create a new server, choosing Ubuntu, CentOS, or Rocky Linux as the operating system with at least 2GB RAM. Connect to your Cloud Server via SSH and log in using the credentials highlighted at the top of the page.

Once you are logged in to your server, run the following command to update your base system with the latest available packages.

apt-get update -y

Or

dnf update -y

Step 2 – 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 3 – 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 4 – 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 5 – 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 2 days ago, run:

find /opt -ctime +2

This will find all files inside /opt directory, which was modified more than 2 days ago.

To find all files modified less than 1 day ago, run:

find /opt -ctime -1

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

find /opt -mmin +10

Step 6 – 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 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:

find / -size -10M

Step 7 – 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 8 – 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:

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.

Get started with 12 months of free cloud VPS hosting

Free Tier includes:
G3.2GB Cloud VPS Server Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year