Be a Grepper. Use Grep -IR to search inside files on Linux and Ubuntu.

Ryan Kopf's Blog

I’ve always found it challenging to remember the specific syntax of commands I don’t use very frequently. You would think GREP might be one of them – as when editing code you often have to find a specific sentence or phrase and GREP is the linux tool for doing so. But It’s generally not a tool I use often because I use an IDE. An IDE has saved me countless hours editing and working on code that it’s unimaginable. Even if it only technically saves me 4 or 5 keystrokes per action, each keystroke has it’s own cost and reduces your working speed.

How to grep inside files.

First, there’s a difference between Grep-ing inside one file versus inside an entire folder of files. Most of the time if you’re looking for a specific phrase to debug something, you could be looking inside of any of hundreds of configuration files. For example if you use wordpress and you are trying to figure out where a specific class is generated, you would need to use grep to do so.

Grepping through a folder.

Here’s the magic.

grep -ir “phrase” .

I’ve remembered this phrase for years by calling it “being a grepper”. grep + ir = grepir or grepper.

The ir combination is useful in some other situations as well when you are searching inside and recursively through a directory.

Note the dot . at the end. This dot refers to your current folder. Don’t forget how folders work in Linux and Ubuntu.

= current folder

~ = your home folder

/var/something/ = the full path of a specific folder

So you could do

grep -ir “class=‘something’” /var/webdev/

I hope this helps you understand!

For more about grep:

Basic Syntax for using Grep to find anything.

The basic grep command syntax is as follows:

grep ‘word’ filename

grep ‘word or phrase’ file1 file2 file3

grep ‘string string’  filename

cat otherfile | grep ‘something’

command | grep ‘something’ (helps you search the output of the command)

command optionA | grep ‘data’

grep —color ‘data’ filename

When searching for a folder, you are searching recursively.

Folder searches.

In order to grep INSIDE multiple files to find one specific thing you are looking for, you are effecively searching recursively, as the operating system will go file by file finding instances of the search term you provide to grep. Note that it can be possible to use complicated regular expressions if you are having trouble finding what you are looking for.

When you are searching for abcde, grep will match all sorts of things, rand7abcde, kbcabcde, abcde123, abcdefire and lots more combinations without obeying word boundaries or spaces or any kind of white space or end stop. You can force the grep command to select only those lines that contain matches to form whole words (those that match only the word you want) by putting it in quotation marks.

NOT the whole “grep something something” but instead grep -ir “something” .

Good luck and happy searching!

Please login to post.