Linux grep command β Part 2
In the previous post about the grep command, I have showed you how use the command and the -i, -c, -n, -o operators. Now let’s see what other tricks this command has up its sleeve.
grep -l
The -l (lowercase L) operator will print only the filenames that containing the searched word or string.
Let’s say you need to find the documents that contain the word linux (I have a habit of creating files, pasting some stuff in them and forget about them, so this comes in handy sometimes).

I’ve added the asterisk in order to force the grep command to search all files within the current directory.
grep -r
grep -r (recursive) comes in handy when you need to search for a word or string in files that are in a directory hierarchy.
For this example, I have created the directory misc that contains subdirectories and files.

Like in the previous example, let’s search for the documents that contain the word linux.

As we can see, it will search through the misc directory and will print the documents that contain the word/string we searched for in the main directory and subdirectories.
grep -v
The -v operator is used when we want to print the lines that don’t contain the word/string that we are searching for.

grep -v printed out the lines that do not contain the word linux.
Please keep in mind that the results are case sensitive and you might want to use the -i operator as well.
grep -w
For this example I have created a file which contains a list of users. If I use grep “user” default.txt it will print all the lines where user is part of other words as well (for example user1).

Using the -w option will return only the lines where user is a whole word:

grep -e
The -e (pattern) option allows you to use multiple search terms.

-e operator can be used for searching multiple terms in different files.

Thank you for reading. I hope you enjoyed this post and found some helpful information.
