How to use the Linux grep command

The grep command is a simple and easy way to search or filter Linux files for specific strings. This Linux command adheres to a uniform syntax and supports a number of search parameters.

What is grep?

Grep stands for ‘global regular expression print’. Since the program is included in the Ubuntu essential package, you don’t have the extra work of installing it. The grep command is used to search for strings and patterns. It thus makes it possible to, for example, filter out the information you’re searching for from large protocol files. However, note that you should never use grep on device files, as this can lead to problems.

How is grep used?

The basic syntax of grep looks as follows: ‘grep [options] search-string [file]’ or alternatively ‘grep [options] [-e pattern | -f file] [file]’. A simple use case for grep is searching for a certain word in the text of a code or log file. So if you’re looking for the word ‘test’ in a file named ‘example.txt’, the grep command will look like this: grep ‘test’ example.txt. You’ll then be shown the lines from the file that match the search.

Regular expressions as the basis for grep

The basis for the Linux grep command are so-called ‘regular expressions’, which can be either ‘basic’ or ‘extended’. It’s extended regular expressions which are relevant for the grep command. Regular expressions can be used to search for individual characters or strings. If the character is a letter or digit, it can be identified using a simple input, even if it’s part of a string. For example, the command would find the digit ‘2’ not only when it appears on its own but also in strings like ‘1234’, ‘y2k’, and ‘Number2’.

There are also characters that serve a specific purpose in the grep command. For example, the dollar sign ‘$’ finds the end of a line. But regardless of its function, every character can be searched for in grep. Simply add a back slash ‘\’ before the character to search it in a file. For example, to search for a full stop, enter ‘\.’.

Lists in grep

Lists of different characters (so-called ‘bracket expressions’) can also be searched for in grep using two brackets ‘[]’. For example, if you want to search for ‘e’ in both upper- and lowercase, use the option ‘[Ee]’. On its own, this input will find all iterations of the letter ‘e’. You can also combine it with entire words or text fragments. For example, ‘[Ee]nd’ will return the word ‘end’ both capitalised and lowercase, as well as words like ‘endorse’ or ‘distend’.

It’s also possible to exclude certain characters from a list. Simply add ‘^’ before the characters. For example, ‘^Ee’ will search for all characters except for ‘E’ and ‘e’.

The grep command offers a number of predefined lists which can save you time and extra steps. Each of these lists is inserted into an additional set of brackets - [[:example:]]. The predefined lists are:

  • [:alnum:]: Includes all digits [:digit:] and letters [:alpha:]
  • [:alpha:]: Includes all letters [:upper:] and [:lower:]
  • [:blank:]: Includes all spaces and tabs
  • [:cntrl:]: Includes all control characters
  • [:digit:]: Includes all digits from 0 to 9
  • [:graph:]: Includes all graphic characters [:alnum:] and [:punct:]
  • [:lower:]: Includes all lowercase letters
  • [:print:]: Includes all printable characters [:alnum:], [:punct:] and [:space:]
  • [:punct:]: Includes all punctuation marks and special characters
  • [:space:]: Includes all characters that create space, including spaces and line breaks
  • [:upper:]: Includes all capital letters

Examples for useful grep commands

When used correctly, the Linux grep command can help you search through large files. There are numerous parameters you can use to make your search more specific, so that it only returns the characters or lines that you need. These parameters are marked with a dash ‘-’. Here are some of the most useful examples for grep:

  1. -c: -c or -count will tell you how many lines the search term appears in, rather than returning each of those lines to you. For example, the grep command ‘grep -c ‘test’ example.txt’ will give you a count of how many lines ‘test’ appears in.
  2. -l: If you want to know which files a certain search term was found in, use the option ‘-l’ (lowercase L). The grep command ‘grep -l ‘test’ \*.text’ will output all the files that contain the word ‘test’.
  3. -i: You can use -i to make your search case insensitive. So ‘grep -i ‘test’ example.txt’ will return every line that contains either ‘test’ or ‘Test’.

Other versions of grep

In addition to the regular version of grep, there are three variants. egrep more or less corresponds to ‘grep -E’ and treats patterns as extended regular expressions. fgrep corresponds to ‘grep -F’ and searches for lines that match a pattern. Characters that are otherwise read as part of a regular expression are interpreted by fgrep solely as characters and not as having an additional function. Examples of such characters are ‘$’, ‘*’, and ‘\’. rgrep corresponds to ‘grep -r’ and recursively searches all directories, meaning that subdirectories are also searched.

In order to provide you with the best online experience this website uses cookies. By using our website, you agree to our use of cookies. More Info.
Manage cookies