Table of Contents

This is part 11 of the series Linux Command Line Interface. Please check earlier articles to get the grasp of current one.

Introduction

Searching for a file or directory is difficult when there are hundreds or thousands of files, especially if we want to search based on its property. Linux got the following tools for these requirements.

  • grep
  • locate
  • find
File Searching in Linux
File Searching in Linux

Searching with grep command

grep searches for a word in a given file.

[email protected] ~ $ ls /usr/share > test.txt
[email protected] ~ $ grep mode test.txt
recovery-mode
usb_modeswitch

Instead of redirecting the output from ls /usr/share to test.txt file, the same output can be sent as input to the grep command. This process is called pipelining.

[email protected] ~ $ ls /usr/share | grep mode
recovery-mode
usb_modeswitch

Searching with locate command

locate command searches for a word in a database, which consists of all file locations.

[email protected] ~ $ locate zip

The above command will give a large result. If it won’t stop, press Ctrl + C. This result usually consists of all file locations which has string zip. This may be a file name or a directory name.

[email protected] ~ $ locate bin/zip
/usr/bin/zip
/usr/bin/zipcloak
/usr/bin/zipdetails
/usr/bin/zipgrep
/usr/bin/zipinfo
/usr/bin/zipnote
/usr/bin/zipsplit

Above result displays all file locations, which consists of string bin/zip.

Here is another example.

[email protected] ~ $ locate sr/bin/z
/usr/bin/zdump
/usr/bin/zenity
/usr/bin/zip
/usr/bin/zipcloak
/usr/bin/zipdetails
/usr/bin/zipgrep
/usr/bin/zipinfo
/usr/bin/zipnote
/usr/bin/zipsplit
/usr/bin/zjsdecode
/usr/bin/zlib-flate
/usr/bin/zsoelim

Newly downloaded or created files/folders may not be displayed in the result when we search using locate program. Let’s download a file and check this operation.

[email protected] ~ $ wget https://upload.wikimedia.org/wikipedia/en/8/86/Einstein_tongue.jpg -O einstein.jpg

This will download an image einstein.jpg in home folder.

[email protected] ~ $ locate einstein.jpg

Above command displays nothing if it can’t find the file in database. Database has to be updated to display the file in result.

[email protected] ~ $ sudo updatedb
[email protected] ~ $ locate einstein.jpg
home/me/einstein.jpg

Searching for files using find command

To display all your files in your home folder :

[email protected] ~ $ find ~

Above result includes hidden files too. That’s the reason the list is so long.

To count total number of files in your folder -

[email protected] ~ $ find ~ | wc -l
15378

wc -l command counts total number of lines in the result. Each line represents a file location.

To count total number of directories:

[email protected] ~ $ find ~ -type d | wc -l
347

To count total number of regular files:

[email protected] ~ $ find ~ -type f | wc -l
15028

Using find command with -name option

To find all jpg image files:

[email protected] ~ $ find ~ -type f -name "*.jpg"
/home/me/einstein.jpg

Try command [email protected] ~ $ find ~ -type f -name "*.png". This will give all default png images in your home directory (possibly in hidden folders).

Here is an example if we remember only part of file name:

[email protected] ~ $ find ~ -type f -name "*stein*"
/home/me/einstein.jpg

Operators can also be used when we want to do one or more operations using find. General operators are and, or and not.

[email protected] ~ $ find ~ \( -type f -name "*stein*" \) -or \( -type f -name "*.txt" \)
/home/me/.config/chromium/chrome_shutdown_ms.txt
/home/me/.pki/nssdb/pkcs11.txt
/home/me/einstein.jpg
/home/me/yoyo.txt
/home/me/yoyoyo.txt
/home/me/.mozilla/firefox/mwad0hks.default/SiteSecurityServiceState.txt
/home/me/test.txt
/home/me/yo.txt