Grep Command in Linux / Unix – Find a File (How To). Grep is a key utility in the Linux/Unix ecosystem that you use to find search patterns and strings of characters within a specific file. This command allows users to search for specific text patterns within files. When grep finds a match, it displays the line with the result. With its powerful search capabilities, it makes it easy to find files containing specific text or phrases. This capability makes it suitable for navigating large codebases, configuration files, or other text based data.
This article discusses the basics of using grep to find files containing a desired text pattern, including navigating directories. It also discusses how to search with case insensitivity, and how to use regular expressions to refine your search.
Let’s start with article Grep Command in Linux / Unix – Find a File (How To).
The items inside the brackets vary depending on what you are trying to find. For the search to happen, the user running the grep command must have read access to the file.
Here are top uses of the grep command:
1. Search for a String in Files
The most common use of the grep command is to search for a string of plain text in a file or directory. To display all the lines of text that contain the string “install” from the “.bash_history” file, run the following command:
Use the grep command to search for lines that do not match a pattern by using the “- v” or “- -invert-match” option. For example, to print the lines in the “.bash_history” that do not contain the string “install” you would use:
Use grep to filter command outputs through piping. For example, if you want to know which processes are actively running on your system as user “githinji” use the following “ps” command:
ps -ef | grep machine1
grep also lets you chain several pipes in one command. The output above has a line that contains the xfwm4 process. If want that line to be excluded pass the output to another grep using this command:
ps -ef | grep machine1 | grep -v xfwm4
4. Recursive Search
To search recursively for a pattern, use grep with the “- -recursive” or “-r” option. When you invoke grep with the “-r” option it searches through every file in the directory you’ve specified, while skipping the symlinks that occur recursively. Alternatively, follow all symbolic links by using the “-R” or “- -dereference-recursive” options.
Here is an example of how to search for the string “bio” in all files within the /home/githinji directory:
grep -r menu /home/githinji/.config/Thunar/
The output shows the matching lines prefixed by the path to the file:
If you use the “-R” option grep follows all symbolic links:
Pick to suppress the default output and only display the names of files that contain the matching pattern. To achieve this use the “- -files-with-matches” or (-l) options. The command below searches through all the files that end with “.conf” in the working directory. After the search only the names of files with the string “Linux” displays:
grep -l -r modules *
6. Case Insensitive Search
grep is case-sensitive by default and treats lowercase and uppercase characters distinctly. To perform a search that ignores the case, use grep with the “- -ignore-case” or the “-i” option. For example, when searching for the string “install” without the “-i” option, the following command won’t display any output.
However, if you use the “-i” option to perform a case-insensitive search, both lower and upper case letters will match:
When searching for a string, grep prints all lines where the string is embedded in bigger strings. If you were to search for “fault”, every line where “fault” is part of a larger word, like “default” matches:
grep fault .xsession-errors2
If you include the “-w” option in the above command, the grep command only returns the lines where gnu is an individual word.
grep -w fault .xsession-errors2
8. Show Line Numbers
The “- -line-number” or “-n” option instructs grep to print the line number of the lines that contain a string that matches the pattern. When you use this option, grep displays the matches to the standard output prefixed with the line number.
Feel free to use the following command to show the lines in the “.xsession-errors2” file that have the string bash prefixed with the matching line number.
grep -n session .xsession-errors2
In the output below, see that the matches are on lines 48, 52, 54, and 59.
GNU grep includes three regular expression feature sets, Basic, Perl-compatible, and Extended.
So, Grep normally interprets the pattern as a basic regular expression, where each character apart from the meta-characters is a regular expression matching themselves. Below is a list of the most popular meta characters:
Use the caret (^) symbol to match expressions at the beginning of a line. In the example below, the string “dbus” will only match if it occurs at the start of a line.
grep ^dbus .xsession-errors
Use the dollar ($) symbol to match expressions at the very end of a line. In the example below, the string “mount” only matches, if it occurs at the end of a line.
grep mount$ .xsession-errors
Use the period (.) symbol to match any individual character. For instance, to match anything that starts with “se” then has 3 characters, and ends with “ng” you could use this pattern:
grep se...ng .xsession-errors
Use [^ ] to match an individual character that you’ve not enclosed in the brackets. The following command matches every combination of strings that contain “co(any_letter_except_l)a, like cobalt. However, it will not match lines containing cola.
To avoid the special meaning of the next character, use the backslash (\) symbol.
The extended regular expressions have all the basic meta-characters, as well as additional meta-characters that generate more powerful and complex search patterns. By using the -”E” or “- -extended-regexp” option, you interpret the pattern as an extended regular expression.
Search for Multiple Patterns
You can combine two or more search patterns by using the -e option. In the following example, we are searching for all occurrences of the strings “activation” and “entry” in the .xsession-errors file:
grep -e activation -e entry .xsession-errors
Print Lines Before or After a Match
To display a specific number of lines of leading context before matching lines, use the “- -before-context” or “-B” option. For example, to print 2 lines of context before matching lines, use the following command:
grep -B 2 update .xsession-errors
The grep command displays a particular number of lines after matching lines. For example, to display 4 lines of trailing context after matching lines, use the following command:
grep -A 3 update .xsession-errors
Print a specified number of lines before and after a match, using the following command:
grep -C 2 update .xsession-errors
Thank you for reading article Grep Command in Linux / Unix – Find a File (How To). We shall conclude the article now.
Grep Command in Linux/ Unix – Find a File (How To) Conclusion
Mastering the grep command is an invaluable skill if you are Linux/ Unix users. This utility provides a handy way to find text hiding in thousands of files in your directory. The above are some of the top uses of the grep command. To learn more about this command, visit the grep man page (man grep).
Dennis is an expert content writer and SEO strategist in cloud technologies such as AWS, Azure, and GCP. He's also experienced in cybersecurity, big data, and AI.
00votes
Article Rating
Subscribe
Login and comment with
I allow to create an account
When you login first time using a Social Login button, we collect your account public profile information shared by Social Login provider, based on your privacy settings. We also get your email address to automatically create an account for you in our website. Once your account is created, you'll be logged-in to this account.
DisagreeAgree
Login and comment with
I allow to create an account
When you login first time using a Social Login button, we collect your account public profile information shared by Social Login provider, based on your privacy settings. We also get your email address to automatically create an account for you in our website. Once your account is created, you'll be logged-in to this account.