Regular Expressions
Before we progress, one should at least be familiar with the concept of regular expressions. Basically, it is a representation language for strings. For example "Tom" represents a single specific string. What if we want to represent both "Tom" and "Tod" at the same time? RE enable us to just use the expression "To[md]" to mean either of them. The square brackets mean any one of 'm' or 'd'. It is immediately apparent why this is useful. You can run a command for a multitude of strings with just one go. There are scores of tutorials online for learning RE. Though are not essential at the beginner level, but you would do well to start picking it up in small steps as you progress to a power user.
Find
Find can be used to search for any filename(including RE syntax) under a given directory. The syntax is :
find /directory/path -name filename
Linux commands tend to have a lot of customizations possible. To maintain sanity, it is best to get familiar with just a few useful features and improvise. If you don't provide any directory-path, the current-directory is defaulted, which is convenient.
Locate
Locate is as simple as:
locate filename
This is blindingly fast, but a drawback is that you can't specify a directory specific search, the results are presented from all over your file-system. Actually, locate builds up an index of the whole system(in background), so it can shoot results quickly. It might miss out some files if they have been recently generated. In order to include them, you need to update its database:
sudo updatedb
This database updating facility also doubles as a new-database creation system, which can be used to enable directory specific searches!! For ex.
I want to be able to search for files limited to my home directory. I run the command
sudo updatedb --database-root ~/ -o ~/homedb.db
This creates a new database by the name of homedb.db in the root(~/) directory. Now while running searches, I can ask locate to use this file instead of its default:
locate --database homedb.db filename
And we are done! However, the flexibility of this arrangement is limited, as we need a database file for every folder we want to localise search in. Better to limit this to some of your important directories, and use the find command elsewhere.
For ease of use, I have added 2 aliases to my ~/.bashrc file for searching within home, so that I don't need to type the long-ish commands everytime:
alias hlocate='locate --database ~/homedb.db'
alias hupdatedb='sudo updatedb --database-root ~/ -o ~/homedb.db'
Grep
Finally we reach grep. This is one of the most iconic commands of unix, and the real power of it resides in the use of Regular expressions. However, even without, it is a terribly useful tool.
It won't be always that you know the exact file you are looking for. Typical example of this includes when you are programming and wish to search that in which files you have used a particular function. Grep is hand-crafted for these tasks. Give it an expression, and it will search for it in all the files within the search-path, and give you the files along with the line numbers where the expression occurs. Ex:
grep -r 'main()' *.cpp
This will search in all your .cpp files(in the current directory) for the main() function, and give you the results.
Now we have discussed the grep, locate and find commands for searching for a file, and can see the power of the linux command line. We also saw the advantages and disadvantages of the specific tools and identified the situations where each is best suited. Familiar in the broad sense, now you can quickly broach the man-pages for any specific feature you require at any time.
