Table of Contents
grep
grep is a command line text search utility originally written for Unix. The name is taken from a command for the ancient UNIX text editor ed - g/re/p
(global regular expression search and print) which has the same effect. The grep
command searches files or standard input globally for lines matching a given regular expression, and prints them to the program's standard output. It is one of the most essential commands you will need to use in Linux command line.
If you are using a modern version of Linux, chances are that grep
is already installed on your system. If not, it is probably in your distro's official repositories. Consult your Package Manager for more information.
Useful Commands
Using grep
may seem obtuse to new users at first. However, once you know how to structure your commands, it is the absolute fastest way to do many common searches:
Built-in Documentation
grep –help
and man grep
will tell you all the flags which are available to use. We will only detail some of the most important ones below:
Search For Lines in Files
To search within filename.txt for the string INSERTSTRING, and to have grep tell you the line number it is on, use the -n
flag like so:
$ grep -n "INSERTSTRING" filename.txt
Search Files in Current Directory
You can also use grep
to search inside all the files within a folder, and not just one. Just replace the filename with an asterisk. To search the pwd for all files containing INSERTSTRING, format your command as so:
$ grep “INSERTSTRING” *
You can also use the asterisk as a wildcard. So for all files containing INSERT* it would be:
$ grep “INSERT*” *
Or to search for all .txt files containing INSERT* it would be:
$ grep “INSERT*” *.txt
The -R
flag lets you search child directories. So to search your pwd and all child directories recursively for all files containing INSERTSTRING:
$ grep -R "INSERTSTRING" *
You can combine flags as well. To search your pwd and all child directories recursively for all .txt files containing INSERTSTRING, and to make grep tell you the line number that the string is found on, combine the -R
and -n
flags like this:
$ grep -Rn "INSERTSTRING" *.txt
Search Files in Specific Directories
To search a specific directory path for all files containing INSERTSTRING:
$ grep "INSERTSTRING" /path/to/directory/*
The aforementioned flags work on specific directories as well:
$ grep -Rn "INSERTSTRING" /path/to/directory/*
More Command Flags
There are more flags you can combine with these basic grep
commands to fine-tune your search. Play around with them to see how they affect output!
-i
- Searches made withgrep
are case-sensitive. Use this flag to search for both uppercase and lowercase versions of the same string.-l
- Print only the filename(s) of search results. Incredibly useful when using-R
on large directory trees.-w
- Match only whole words.-x
- Match only whole lines.-v
- Inverted search. Returns only non-matching lines.-s
- Suppress error messages. Useful in bash scripts.
Variations
There are countless implementations and derivatives of grep
available for many operating systems, as well as for aiding searches in third-party applications such as EnCase (computer forensic software). Early variants of grep included egrep
and fgrep
. The former applies an extended regular expression syntax that was added to Unix after Ken Thompson's original regular expression implementation. The latter searches for any of a list of 'fixed' strings using the Aho-Corasick algorithm. These variants are embodied in most modern grep implementations as command-line switches (e.g. -E
and -F
respectively in GNU grep). In such combined implementations, grep may also behave differently depending on the name by which it is invoked, allowing fgrep
, egrep
and grep
to be links to the same program.
pcregrep is an implementation of grep that uses Perl regular expression syntax.
Other commands contain the word grep
to indicate that they search (usually for regular expression matches). The pgrep utility, for instance, displays the processes whose names match a given regular expression.
In Perl, grep
is a built-in function that finds elements in a list. In functional programming languages, this higher-order function is typically named “filter” instead.
The DOS, OS/2 and Windows platforms provide the find
command for simple string searches. Windows also provides the findstr
command which approximates much of the functionality of grep
, or you can use the the ported version in Cygwin.
External Links
Find this page online at: https://bestpoint.institute/tools/grep