linux grep command for finding text in files and directories
In a Linux environment there is the Linux grep command that is useful for finding text in a file, or a bunch of files in a directory when it comes to using it recursively. The grep command should not be confused with the find command, which is somewhat similar actually only the find command is used to look for patterns in file names, while the grep command is used to look for patterns in the content of files, or some piped in standard input.
I have been starting to write a few posts on various commands that often are part of Linux, or can be easily added to Linux, and grep is certainly one such command that I should write a quick post on because I am sure it will come in handy now and then with what I often work on when it comes to lengthly collections of text files.
1 - Basic grep commands
In this section I will be starting out with just a few simple basic examples of grep. There is just looking for text in a single file, and other basics that one should be aware of before looking into the many other features of grep, and what can be done with it when it comes to using it with other Linux features and commands.
1.1 - A simple use case example of grep
SO for starters how about just a simple example that involves looking at just one file. In one of my repositories on github I have a collection of markdown files for each of my blog posts here on this site. Say I am at the root path of that repository and I want to fine the id of just one file. I can use grep and pass a string pattern to look for as the first argument followed by a relative path to the file I want to look at.
|
|
1.2 - Know the version
First off there is know what version of grep I am using. To do so I just call grep and pass a capital V to get the version of grep that I ma using on the system.
|
|
2 - more than one file
There are several ways to go about greping over more than one file. There is just greping over files at one level in a folder, and then there is also using grep in a recursive way looking at all the files in all the folders starting as a given root path. So in this section I will be going over some examples of looking at collections of files using Linux grep.
2.2 - Using glob pattens with the file path
One way to look at more than one file is to use glob patterns for the file path. In other words I do not need to give an absolute or relative path to a single file, but a glob pattern for a collection of files.
Say I have a number of files in a path that start with the pattern linux- and say I want to find the id of each file. I can type grep followed by the text pattern for an id in a file that starts with the desired pattern, followed by the glob pattern.
|
|
2.1 - The recursive flag
Another options for looking at more than one file is to use the recursive flag. This is what I will want to use when I want to look for a text pattern that might be in any file in a bunch of nested folders from a given root folder. For example say I want to look for a variable name that might show up in a bunch of source code files in a projects with lots of modules that all make use of something that is this variable name or property name. I can use grep with the recursive option to find all instances of that variable name. This is then one of the major reasons why grep is so usful when it comes to working on a programing project.
|
|
3 - piping from standard input
The Linux grep command can be used with the standard input rather than a file. Here I am using the Linux echo command to just create some standard input to which I am then pipping into greps standard input I am then looking for the pattern Linux and I am also using the o options that will only print matches for the pattern.
|
|
4 - Regular expressions and Linux grep
Grep can be used with regular expressions as a way to match something that can not be expressed as a static fixed pattern. So in this section I will be going over some examples of grep that involve the use of these regular expressions to match not just a fixed text pattern but a pattern that can change a little now and then from one instance to another.
4.1 - Matching something that is at the beginning of a line and contains a set of numbers
So I have this collection of markdown files, and each of these files has a line that is an id number of a single blog post such as this one that you are reading now. Say I want to produce a list of post file names followed by what the id is of that file. I can use grep followed by a regular expression to do just this by making that pattern start with a \^ that will only match the start of a line followed by \’id: \’ that each id starts with at the top of the file followed by [0-9]* that will match any number that follows.
So with that said with the posts folder as the current working path I can do something like this:
|
|
To produce a list of markdown files with the id numbers for each as a file in my home path. Here not only am I using regular expressions but I am also using Linux redirection as a way to direct the standard output of the grep command to a file rather than the console window.
5 - Conclusion
So it goes without saying that the Linux grep command is one command that will come in handy now and then when it comes to looking for certain text patterns in a large collection of files. Say I find that I keep making the same spelling mistake over and over again and then catch wind of it. I can use grep to find all the files where that mistake has happened, and then use it as an aid for making the necessary changes.
There are other commands that come to mind though when it comes to this sort of thing though. The grep command can be used as a way to go about filtering out lines of output that do not fit a given pattern. However if I just want the first few lines of output there is the head command that can be used to do so and be done with it, as well there is the tail command that I can use to get the last few lines from some output.