Searching within files
I’ve mentioned this in another post, but grep is the go to for searching within files. Using the basic functionality of grep is straightforward, just pass the phrase you want to find and the file where you want grep to search for that phrase.
Say I have a log file with thousands of debugger statements and I want to search for instances of fatal errors.
Since I know my program will output fatal errors tagged with the word “fatal”, I can use that as the search phrase in my grep statement
Add the -r flag to signal a recursive search. A recursive search will look for instances of the given phrase in all files within a directory.
Use the -n flag to have grep output the line number which the phrase was found (I almost always find this useful). And remember you can combine multiple command line flags.
Some other useful flags for the grep command
- -i ignore case of the matcher phrase
- -w search for full words. e.g. given grep -w ‘fatal’ file.txt the command will only return instances where the word “fatal” is by itself as a full word, instead of returning matches such as ‘fatal_23’ or ’fatality’. If we were to use the -w flag in our log file example above, grep would return no results because the word ‘fatal’ was surrounded by square brackets and not a standalone word.
Searching for files by filename
Sometimes you have a project with a large amount of files and you just can’t f*&king remember where you put that config.yaml file in your Pan’s Labyrinth of directories (ok maybe that’s a contrived example, but bear with me). Luckily there is a solution, the find command.
Unlike grep, the find command is recursive by default so it will search through the given directory for files that match your given search term.
Additionally, find can take a wildcard matcher as part of your search phrase. So if I wanted to search for all files in a directory with a given file extension I would use the search term *.jpg .
By default, find searches for both directories and files. With this in mind you could just leave out the -type f if you didn’t care about getting directories mixed in with your search results.
Searching for directories by directory name
I don’t often find myself searching for directories by name, but that doesn’t mean every so often the need arises. Fortunately, we can use the find command for this as well. Simply tell it to search for directories instead of files by using the -type d flag.