You are here

Searching Techniques in Linux

Here I shall cover the various techniques that can be used for searching.

Searching for files

"locate" works by maintaining a database of all the files which are present on the hard disk. When a search is performed for a specific file, this information is retrieved from the database. In order to update the database run the command "updatedb". For example,

# updatedb
# locate 'rc.firewall'

"find" command is used for the same purpose as "locate" but the difference is that instead of maintaining a database it directly searches. For example,
# find /etc/ -name 'rc.firewall'

Searching for files within packages

You can either use "dpkg" or "apt-file" but for dpkg it is necessary that the package be installed and for apt-file you have to perform update regularly.
# dpkg -S 'apt.conf'
(if the package containing apt.conf is installed then only will it return the result)
OR
# apt-file update
# apt-file search 'apt.conf'

This search is useful if you want to know to which package a particular file / command belongs to.

Searching (and replacing text) in multiple files

"grep" is a very popular command which can be used to search for text in directories recursively.
# grep -R 'search_string' *

"rpl" (package installable via apt) is a good utility for replacing text in multiple files. For example,
# rpl -vRa 'old_string' 'new_string'
(use option -s for simulation run)

Category: 

Add new comment