How to Use Grep Command to Search Files in Linux

Grep is a powerful command-line search utility for Linux and other Unix-like operating systems. Its name stands for Global regular expression print and you can use it to search for patterns in files across your system.

 

In this article we will take a detailed look at how to use the grep command to search files in Linux and provide practical real-world examples.

 

While this guide is focused towards Linux you will be able to use the same commands on other popular Unix-like operating systems such as macOS.

 

Grep Syntax

The grep command syntax firstly takes a list of options, the pattern and then the files to search.

 

grep [options] pattern [file...]

 

  • options - an optional list of flags that control the behaviour of the grep command.
  • pattern - the search Regex pattern
  • file - an optional list of files to search

 

 

Basic Grep Usage

The most basic way to use grep is to match a literal text pattern in a single file.

 

grep "vue" package.json

 

Grep will return every line that contains the exact text vue in the file package.json. Depending on your system the pattern may be highlighted in the output.

 

"vue-template-compiler": "^2.6.11"
"@ckeditor/ckeditor5-vue": "^1.0.1",
"@vue/cli": "^4.4.1",
"vue": "^2.6.11",
"vue-gallery": "^2.0.1",
"vueify": "^9.4.1"

 

 

Commonly Used Flags

To change the way grep behaves and thus the results returned you can add optional flags before the pattern.

 

Case Insensitive

The -i flag will make the grep command case insensitive. This is useful when you don't know whether the text you are searching for will contain capital letters.

 

grep -i "vue" package.json

 

This will return lines contain matches of vue Vue and VUE

 

VUE packages
"vue-template-compiler": "^2.6.11"
"@ckeditor/ckeditor5-vue": "^1.0.1",
"@vue/cli": "^4.4.1",
"vue": "^2.6.11",
"vue-gallery": "^2.0.1",
"vueify": "^9.4.1"
Vue js

 

If packages.json contained vUE that would be returned also.

 

Show Lines That Don't Match

You can invert the matching functionality of grep to return lines that don't contain matches of a pattern by adding the -v flag.

 

grep -v "vue" package.json
 "devDependencies": {
   "@fortawesome/fontawesome-free": "^5.13.0",
   "axios": "^0.19",
   "cross-env": "^7.0",
   "mix": "^5.0.1",
   "lodash": "^4.17.13",
   "resolve-url-loader": "^3.1.0",
   "sass": "^1.15.2",
   "sass-loader": "^8.0.0",
 },
Vue js

 

The line with Vue js is still returned as the -i flag was not added.

 

 

Show Line Numbers in Output

If the file you are searching has a lot of lines it is really handy to know what line number each match is one. Grep will provide this information in the output if we pass in the -n flag.

 

grep -i -n "dev" package.json
4:    "dev": "npm run development",
6:    "watch": "npm run development -- --watch",
12:  "devDependencies": {
29:    "@ckeditor/ckeditor5-dev-utils": "^20.0.0",
30:    "@ckeditor/ckeditor5-dev-webpack-plugin": "^20.0.0",

 

 

Pipe Grep into the Output of Another Command

Grep is really powerful when used with piping. In the following example, we are going to tail the output of an access log and pipe the output to grep, which will only return entries containing example.com

 

sudo tail -f /var/log/nginx/access.log | grep "example.com"

 

You can pipe grep as many times as you wish into your command to make the output more precise. We can chain another grep command to only show lines containing POST from the example.com access log entries.

 

sudo tail -f /var/log/nginx/access.log | grep "example.com" | grep "POST"

 

Recursively Searching Directories

 

Another useful feature of grep is the ability to recursively search files in directories. This is done by using the -r flag followed by the pattern and the directory to search. Below we are searching for any lines containing logo_new.svg in all files located in the resources directory.

 

grep -r -n logo_new.svg resources/

 

The output will show the path to the file and the content of the line found. In this example, it also shows the line number since -n was included in the grep command.

 

resources//assets/sass/theme.scss:63: background: url('/media/style/logo_new.svg') center no-repeat;

 

To search through symbolic links use the -R flag instead of -r.

 

Match Full Words Only

Be default grep does not consider words and simply looks for the string you specified.

 

Lets say we have a file containing the following words:

 

ubuntu software
safari software
ios software
ios6 software
linux software
nux software
ios5 software
safari software
linux software
safari software

 

If we want to return lines that only contain the exact word nux, we can use the -w flag.

 

grep -w example.txt
nux software

 

Only Show Filename

 

If you just need to know the name of the file where are pattern has been matched pass in the -l flag.

 

grep -r -l logo_new.svg resources/

 

Pro tip - multiple flags can be added without adding a space and a dash each time. The above command could be rewritten as:

 

grep -rl logo_new.svg resources/

 

Regular Expressions

With grep you can use regular expression characters or "anchors". Let's have a look at three common regular expression characters to control pattern matches in finer detail.

 

The ^ (caret) symbol finds matches at the start of the line. In the following example nginx will only be returned as a match if it is located at the very beginning of the line:

 

grep "^nginx" example.txt

 

 

The $ (dollar) symbol finds matches at the end of the line. In the following example nginx will only return as a match if it is located at the very end of a line:

 

grep "nginx$" example.txt

 

The . (dot) character can be used to represent any character. If we wanted to find any string that has at least two characters and ends with ing we could use the following command:

 

grep "..ing" example.txt

 

 

Use [] (brackets) to match any one of the characters contained within the brackets. If we wanted to match both too and two we could use the following command:

 

grep "t[wo]o" example.txt

 

 

A ^ (caret) located inside of the brackets can be used to exclude a character from being matched. Lets say we want to match strings that ended in ode that are not node, we could use the following command:

 

grep "[^n]ode" example.txt

 

We would get matches for strode triode but not encode decode recode.

 

 

Brackets can also be used to specify a range of characters. If we wanted to find all lines that don't start with a capital letter we could use the following command:

 

grep "^[a-z]" example.txt

 

 

Conclusion

You now know the basic functions of grep and how to use common regular expressions to match patterns in files. Grep is a powerful way to search for files on your system and as you develop a greater understanding of Regular Expression it will become an ever more useful tool.

 

terminal file search regex