Hosting or Servers Logo

Searching a File Using the Linux grep Command

Linux grep Command in Monitor

Grep is a useful command-line tool used to look for patterns within text files or streams. It can quickly search through files and folders to locate strings of text that match a given pattern.

In its simplest form it can look for a word in a text file. e.g the word salmon in the file test.txt

grep salmon test.txt

Linux grep Command Single Word

Figure 1.


In Figure 1. You can see the result is displayed highlighted in color. If this is not the case for you then the following command line will switch this function on (See Figure 2.)

grep - - color=always salmon test.txt

Linux grep Command Always Color

Figure 2.


If you need to search for more than one word in a file, then use the -e option as in the example below (See Figure 3.)

grep -e salmon -e otter test.txt

Linux grep Command Multiple Words

Figure 3.


The Linux grep command has many different options to bring out the best of its abilities. Below there is a rundown of these and their basic usage.

Pattern selection and interpretation:

-E, --extended-regexp PATTERNS are extended regular expressions

-F, --fixed-strings PATTERNS are strings

-G, --basic-regexp PATTERNS are basic regular expressions

-P, --perl-regexp PATTERNS are Perl regular expressions

-e, --regexp=PATTERNS use PATTERNS for matching

-f, --file=FILE take PATTERNS from FILE

-i, --ignore-case ignore case distinctions in patterns and data

--no-ignore-case do not ignore case distinctions (default)

-w, --word-regexp match only whole words

-x, --line-regexp match only whole lines

-z, --null-data a data line ends in 0 byte, not newline


Miscellaneous:

-s, --no-messages suppress error messages

-v, --invert-match select non-matching lines

-V, --version display version information and exit

--help display this help text and exit


Context control:

-B, --before-context=NUM print NUM lines of leading context

-A, --after-context=NUM print NUM lines of trailing context

-C, --context=NUM print NUM lines of output context

-NUM same as --context=NUM

--color[=WHEN],

--colour[=WHEN] use markers to highlight the matching strings;

WHEN is 'always', 'never', or 'auto'

-U, --binary do not strip CR characters at EOL (MSDOS/Windows)

Output control:

-m, --max-count=NUM stop after NUM selected lines

-b, --byte-offset print the byte offset with output lines

-n, --line-number print line number with output lines

--line-buffered flush output on every line

-H, --with-filename print file name with output lines

-h, --no-filename suppress the file name prefix on output

--label=LABEL use LABEL as the standard input file name prefix

-o, --only-matching show only nonempty parts of lines that match

-q, --quiet, --silent suppress all normal output

--binary-files=TYPE assume that binary files are TYPE;

TYPE is 'binary', 'text', or 'without-match'

-a, --text equivalent to --binary-files=text

-I equivalent to --binary-files=without-match

-d, --directories=ACTION how to handle directories;

ACTION is 'read', 'recurse', or 'skip'

-D, --devices=ACTION how to handle devices, FIFOs and sockets;

ACTION is 'read' or 'skip'

-r, --recursive like --directories=recurse

-R, --dereference-recursive likewise, but follow all symlinks

--include=GLOB search only files that match GLOB (a file pattern)

--exclude=GLOB skip files that match GLOB

--exclude-from=FILE skip files that match any file pattern from FILE

--exclude-dir=GLOB skip directories that match GLOB

-L, --files-without-match print only names of FILEs with no selected lines

-l, --files-with-matches print only names of FILEs with selected lines

-c, --count print only a count of selected lines per FILE

-T, --initial-tab make tabs line up (if needed)

-Z, --null print 0 byte after FILE name

You Are Leaving Us

This link is being provided as a convenience and for informational purposes only it is not an endorsement or an approval of any of the products, services or opinions of the corporation or organization or individual.

Hostingorservers.com bears no responsibility for the accuracy, legality or content of the external site or for that of subsequent links.