Back to Basics Part 4 â€“ using grep in GNU/Linux
One of the really confusing things for users who are new to messing with the command line, can be trying to search with specifics. A useful little tool for aiding in this process, is called grep, or â€œglobal regular expression print,â€ which will search for regular statements in anything you pipe it through, and show you matches for what you looked for (if any exist.)
A rather straightforward example of this, before we continue, would be to use grep to search through the list of processes given with the command ps aux, to search for a specific application.
Grep in GNU/Linux
ps aux | grep spotify
Running this command while I had Spotify running, showed me that indeed Spotify was running, as shown in the image below. You can see Spotify has multiple processes running:
This is just one way that grep can be extremely useful. But, delving a little deeper, there are more options we can add to grep, to enhance our functionality much deeper.
Colour highlighting results
Letâ€™s say that we want to search a document, to see if that document has a specific phrase within it, (perhaps you want to see if phonenumbers.txt has your Aunt Mabelâ€™s phone number in it.)
grep --color Â "Mabel" phonenumbers.txt
Note: Some distroâ€™s have color enabled by default, and do not require its usage.
This command would show the correct line such as, â€œAunt Mabel â€“ 522-111-4321â€ with the text highlighted. However, there is a catch to this string, and thatâ€™s that if I had typed â€œmabelâ€ with a lowercase M, it would have found nothing, assuming that inside phonenumbers.txt itâ€™s spelled â€œAunt Mabel.â€
To get around potential issues like this, we can also use the option -i which means â€˜case insensitive.â€™
grep --color -i "mabel" phonenumbers.txt
Again, assuming the word Mabel exists in the document, this would find and highlight it on that line, regardless of whether the document had the word capitalized or not.
Here are some other use cases for grep:
- grep "search text" filename -- to search a file for the specified string.
- grep "search text" file_pattern -- to search multiple files for the specified string
- grep "Regex" filename -- to use regular expressions to search file contents.
- grep -r "search text" * -- search in all files recursively for the text.
More options can be found on the grep man page, by typing the following into a terminal window, to read the manual for grep: man grep
You can also check out the grep documentation on the GNU website.
This is only the tip of the iceberg for what grep can do, but a good starting point for new users to help aide them in their quest to grow an epic beard, build their own kernel from scratch, and become a mighty guru in the ways of the terminal.
Hmmm . . . from what little I know, the find command works with the grep command for better results.
man pages says grep doesn’t work well on its own because find needs to be used first.
Don’t question mike:he writes on the site and you don’t.
Question me all you want! I’m not perfect lol
Grep and find do different, but similar, things. Find is more feature-rich and can do things that grep can’t such as locate a file by filename, date, etc., and includes some rudimentary programmability.
If what you want to do is just locate something in the contents of a file, you want to use grep instead of find. Grep is easier and faster for this task. If you want to find files based on metadata (name, date, permissions, etc.) or a combination of metadata and content, then you want to use find.
The response by @John Fenderson is correct, and well said.
Grep and find are similar tools, just like mlocate could also be considered somewhat similar, but they serve different purposes, and are best suited for different tasks.
I tend to use find, when trying to locate a file (lets say a specific php file, or a specific .conf file) but I tend to use grep more, when dealing with things like processes or searching the contents of a file.