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 para­met­ers.

What is grep?

Grep stands for ‘global regular ex­pres­sion print’. Since the program is included in the Ubuntu essential package, you don’t have the extra work of in­stalling it. The grep command is used to search for strings and patterns. It thus makes it possible to, for example, filter out the in­form­a­tion 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 al­tern­at­ively ‘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 ex­pres­sions as the basis for grep

The basis for the Linux grep command are so-called ‘regular ex­pres­sions’, which can be either ‘basic’ or ‘extended’. It’s extended regular ex­pres­sions which are relevant for the grep command. Regular ex­pres­sions can be used to search for in­di­vidu­al char­ac­ters or strings. If the character is a letter or digit, it can be iden­ti­fied 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 char­ac­ters that serve a specific purpose in the grep command. For example, the dollar sign ‘$’ finds the end of a line. But re­gard­less 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 char­ac­ters (so-called ‘bracket ex­pres­sions’) 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 it­er­a­tions 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 cap­it­al­ised and lowercase, as well as words like ‘endorse’ or ‘distend’.

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

The grep command offers a number of pre­defined lists which can save you time and extra steps. Each of these lists is inserted into an ad­di­tion­al set of brackets - [[:example:]]. The pre­defined 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 char­ac­ters
  • [:digit:]: Includes all digits from 0 to 9
  • [:graph:]: Includes all graphic char­ac­ters [:alnum:] and [:punct:]
  • [:lower:]: Includes all lowercase letters
  • [:print:]: Includes all printable char­ac­ters [:alnum:], [:punct:] and [:space:]
  • [:punct:]: Includes all punc­tu­ation marks and special char­ac­ters
  • [:space:]: Includes all char­ac­ters 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 para­met­ers you can use to make your search more specific, so that it only returns the char­ac­ters or lines that you need. These para­met­ers 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 in­sens­it­ive. 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 cor­res­ponds to ‘grep -E’ and treats patterns as extended regular ex­pres­sions. fgrep cor­res­ponds to ‘grep -F’ and searches for lines that match a pattern. Char­ac­ters that are otherwise read as part of a regular ex­pres­sion are in­ter­preted by fgrep solely as char­ac­ters and not as having an ad­di­tion­al function. Examples of such char­ac­ters are ‘$’, ‘*’, and ‘\’. rgrep cor­res­ponds to ‘grep -r’ and re­curs­ively searches all dir­ect­or­ies, meaning that sub­dir­ect­or­ies are also searched.

Go to Main Menu