Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

locate and find commands are used to search a file by name in the directory.

The locate command


searches the files in the database . The find command finds the files in the filesystem .

Finding Files by Name

 / (slash) — search the whole system.


 . (dot) — search from the folder you’re currently working on (current directory).
 ~ (tilde) — to search from your home folder.

In order to find a file by name, simply type:

find -name “File1”

If we want to run a case insensitive search, we can do this:

Find -iname “File1”

What if we only want to return files with names that don’t contain a certain string? Then we will use:

Find -not -name “file”

Finding Files by Type

If you want to search for files by type, you can do so with the following command:

find -type typequery

Some examples of file types are:

 f: Regular file
 d: Directory
 l: Symbolic link
 c: Character devices
 b: Block devices

find -type f

Finding Files by Time

You can find files based on access time (-atime), modified time (-mtime), and change time (-ctime)
flags.
Let’s find a file modified more than 5 days ago:

find . -ctime +5

Less than 1 day ago:

find . -ctime -1

More than 25 minutes ago:find / -mmin +25

Finding Files by User or Group

The -user and -group flags can be used to find a file located by a specific user or group

Find all files owned by user “mc”:

find -user mc

find -group mc -name

Finding Files by Size

Find can filter files based on their size. SImply use the -size flag with the following size conventions:

 c: Bytes
 k: Kilobytes
 M: Megabytes
 G: Gigabytes
 b: 512-byte blocks

In order to find a file that is exactly 1GB in size, simply type in the phrase:

find . -size 1G

If it is greater than 1GB:

find . -size +1G

Less than 1GB:

find . -size -1G

Locate

In some Linux distribution locate command may not work directly then we have to install it using
below command
Sudo apt install mlocate

After mlocate got installed then execute below command

Locate apache

Search for a file with a specific name

locate filename

NOTE: If you have just created a new file, you need to update the locate database as shown before
searching for the file.NOTE: If you have just created a new file, you need to update the locate
database as shown before searching for the file.

sudo updatedb

Limit the output to a specific number

If the output is a little overwhelming and perhaps you want to view the first N search queries, then
execute the command below. Here, we have limited the output to only 20 search results.

$ locate apache -n 20

Get more information about locate database

To gather more insights on the locate database – mlocate.db – run the following command:

$ locate -S

This prints out the number of files and directories indexed by the database, among other details.

For additional command options, visit the man pages as shown.

$ man locate

You might also like