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.

9.23 12:34pm [info] : Everything is fine
9.23 2:56pm [info] : Still good
9.23 3:00pm [fatal] : Oh sh*t. this ain't good
9.23 3:15pm [warn] : Getting back to normal
9.23 4:40pm [info] : We're back guys
9.23 5:00pm [fatal] : jk we're going down again

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

grep ‘phrase’ logs_9-23.txt
9.23 3:00pm [fatal] : Oh sh*t. this ain't good
9.23 5:00pm [fatal] : jk we're going down again

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.

grep -r ‘fatal error’ /system/logs

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.

grep -n -r ‘fatal error’ system/logs

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.

find /documents/my_project -type f -name 'config.yaml'

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 .

find /documents/my_project -type f -name '*.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.

find /documents/my_project -name '*.jpg'

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.

find /documents/my_project -type d -name 'MyCoolProject'