File Searching
Share on:Edit on:Table of Contents
- Introduction
- Searching with grep command
- Searching with locate command
- Searching for files using find command
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
Searching with grep command
grep searches for a word in a given file.
me@linux ~ $ ls /usr/share > test.txt
me@linux ~ $ 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.
me@linux ~ $ 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.
me@linux ~ $ 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.
me@linux ~ $ 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.
me@linux ~ $ 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.
me@linux ~ $ 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.
me@linux ~ $ 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.
me@linux ~ $ sudo updatedb
me@linux ~ $ locate einstein.jpg
home/me/einstein.jpg
Searching for files using find command
To display all your files in your home folder :
me@linux ~ $ 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 -
me@linux ~ $ 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:
me@linux ~ $ find ~ -type d | wc -l
347
To count total number of regular files:
me@linux ~ $ find ~ -type f | wc -l
15028
Using find command with -name option
To find all jpg image files:
me@linux ~ $ find ~ -type f -name "*.jpg"
/home/me/einstein.jpg
Try command me@linux ~ $ 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:
me@linux ~ $ 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.
me@linux ~ $ 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