Searching for files by name and/or extension in Linux Operating System

When we have many files available for viewing, sometimes we need to find a file among many without using the GUI searching tools or provide a better set of granular filters to reduce returned results. 

We can use few commands to search a file on the command line:

Locate command

The locate command is known as sibling of updatedb command. It helps to find or locate files using an index of files.

Find command

The most used command to find the file is find command. it helps to find files with their attributes, extensions, and names within a specific directory or whole system.

The find command is the most frequently used command by Linux Administrator or engineers in daily life as it is more suitable for Linux terminal or command line. But locate command is commonly used in any system (Desktop, laptop, or servers) by any users. The locate command is recursively indexing all the files in the system, which helps it find the file quickly.

We can update the file index using the following command:

$ sudo updatedb

The locate command can be used to test for a file’s existence before reporting its location (the database may be out of date) and limit the number of entries returned.

As noted, find does not have a fancy database, but it has many user-configurable flags, which can be passed to it at the time of execution.

Some of the most commonly used flags used with the find command are as follows:

-type: This is used to specify the type of file, which can be either file or directory
-delete: This is used to delete files, but may not be present, which means that exec will be required
-name: This is used to specify searching by name functionality
-exec: This is used to specify what else to do upon match
-{a,c,m}time: This is used to search for things such as time of access, creation, and modification
-print, -print0: These commands are used to print the name of the current file on a standard output!, 
-not: This is used to specify logical operations such as match everything, but not on this criteria
-i: This is used to specify user interaction on a match such as -iname test

Getting ready

Before start the lab section you need your terminal ready; if your system hasn’t installed or locate command not working, you can install this utility using the following command:

$ sudo apt-get install locate manpages manpages-posix
$ sudo updatedb


If a file is not found using the locate command, the database might be simply out of date and needs to be re-run. Updatedb is maybe also not indexing partitions such as those contained on removable media (USB sticks), and the file may be present there instead of the regular system partitions. 

How to use it…

Open a terminal and run the following commands in order to understand the locate command:

$ locate test.txt
$ sudo touch /test01.txt /test02.txt
$ sudo echo "My name is Satish Kumar" > test02.txt
$ locate test02
$ sudo updatedb
$ locate test02

Next, run the following commands to demonstrate some of the power of find:

$ sudo find${HOME}-name".*"-ls
$ sudo find / -type d -name ".git"
$ find ${HOME} -type f \( -name "*.sh" -o -name "*.txt" \)

we can chain the find commands together with && and ultimately perform an exec instead of piping the output to another process, command, or script. Try the following:

$ find . -type d -name ".txt" && find . -name ".sh" && find . -name ".jpeg"
$ sudo find / -type f -exec grep -Hi 'My name is Satish Kumar' {} +

Finally, one of the most common uses of find is to delete files using either the built-in -delete flag or by using exec combined with rm -rf:

$ find ~/commondir -type d -empty -delete
$ find commondir -name ".txt*" -exec rm -rf {} \;


Submit a Comment

Your email address will not be published. Required fields are marked *

nine − 5 =

Related Articles