- [Instructor] When working with text on a Linux system, we often need to search for a particular string in a log file, or some code, or a configuration file. And while it's always an option to open up a file and skim through it, there's a much better way, using a tool called grep. Before we look for text in a file, we need some text in a file. So I'll create a file with a few lines here before we get started. (typing) The most basic way to search a file with grep is to write out grep and then a search term, and the file to search within.
I'll search for, let's say, the word line. I know that's in the file I just created. Using grep like this will show us the line of a file that matches whatever we're asking for. The match is highlighted in red here on my system. Depending on your terminal, you might see a highlight, or you might not. To specifically enable color output when you use grep, you can add dash dash color equals auto. And then your search and file.
Or you can make an alias that includes it or add it to your environment. Grep can be helpful for telling us whether a file matches, and gives us a little information about a match. But we can also make the result a little more useful with some options. The dash N option will show you the line number of the match within the file. And the dash C option will show you a given number of lines of context, that is the lines surrounding the match.
Both of these options can be helpful in configuration files especially, where you might have the same name for something, but with a particular position or instance of it makes a difference. And you can use dash capital A and dash capital B to see just that many lines after and before the match as well. Matching in grep is case sensitive, and if you need to perform a case insensitive search, you can use the dash I option. That's especially helpful for searching in log files and source code when you might have a message or a variable with capitalization you're not sure of.
So if I looked for the word this in my text file using the dash I operator, it matches the text on the first line that starts with a capital T even though I didn't specify a capital T in my search. You can also invert the search, that is, to show everything that doesn't match the search term with the dash V option. I'll search first for the term li which occurs in two lines. And now I'll use dash V to invert it.
Now I get the other lines, and not the ones that ordinarily match the search. If you'd like, take a moment to try out these options with some different search terms in the text. We can also pipe the output of commands into grep, which can be useful. Instead of specifying a particular file, we can give grep a path, and it'll search within that path to return matching lines from any file that contains what we're looking for. Let's look for errors, case insensitively, in the var log folder. Now for every match, we see the file name that matches, and the matching line.
And to search recursively into folders within the path, we can use dash capital R. Otherwise, grep will just search files at the first level within the given path. If you want, you can search your whole system for a given piece of text. But that can be pretty time intensive and resource intensive if you have a lot of files. Grep normally only operates on text files, but you can search for text within binary files as well. Sometimes this lets you find text in .pdf files or strings in dialogues and other things like that. But it's not necessarily reliable.
To treat binary files as text, you can use the dash A option. But sometimes you'll get some garbage on the screen, which is a textual representation of the underlying binary data. If you plan to look for text in binaries, it's probably a good idea to run the file through the strings command first, and pass just the text that it pulls up out into grep. Grep is one of the foundational tools that a system administrator or developer needs to be familiar with. Take some time to explore using grep, and try to build it into your daily work.
Note: Because this is an ongoing series, viewers will not receive a certificate of completion.