In this lesson, we will cover how to search the contents of files as well as what pipes are and how to use them.

The grep command

To look for text within a file, use the grep command. A typical format of this command is grep followed by a search pattern and the file that you are searching for the pattern in.

grep <pattern> <file>

The grep options

-iPerform a case insensitive search (ignoring case).
-cCount the number of occurrences in a file.
-nPrecede output with line numbers.
-vInvert Match. Print lines that do not match the search pattern.


Let's look at the content of the file test.txt:

$ cat test.txt
Hello world line 1
firstname: Tom
lastname: South
last line

To search this file with the grep command: $ grep Tom test.txt firstname: Tom

To search all occurrences that match o:

$ grep o test.txt
Hello world line 1
firstname: Tom
lastname: South

Inverse match

$ grep -v o test.txt
last line

It is matching case by default. To ignore case, use -i:

$ grep Firstname test.txt 
$ grep -i Firstname test.txt
firstname: Tom

You can also combine options, for example, combine -c for count and -i for ignore case. And grep says there is only 1 line that matches Firstname.

$ grep -ci Firstname test.txt

Combine -n and -i will display which line number that the match occurs on.

$ grep -ni Firstname test.txt
2:firstname: Tom

The file command

There are some clues to guess what a file might contain. For example, some files will have an extension. If the file ends in .txt , it has large changes that it is a text file.

If a file has executable permissions set, it might be an executable programe. An easy way to determine the type of a file is to run the file command against that file.

$ file <filename>

If it is a text file, it will say that is text in the output of the file command.

$ file  ASCII text
$ file *
Applications: directory
Documents:    directory
Downloads:    directory
Dropbox:      directory
Library:      directory
Pictures:     directory
Public:       directory
node_modules: directory
test.txt      ASCII text

Searching for text in Binary Files using strings command

A binary file is a file that is in machine readable format and not a human readable format. If you run grep against a binary file it will simply display whether or not that informaiton is found in the file, but it will not display the text.

To look at the textual data within a binary file, use the strings command.

Pipe (|)

The vertical bar command (|) is called pipe. You can chain two commands together with a pipe.

Pipe takes the standard output from the preceding command and passes it as the standard input to the following command. If the first command displays error messages, those will not be passed to the second command by default. Error messages are displayed on standard error. If you want standard error to be passed in as input to the command following the pipe, you can redirect standard error to standard input.

The cut command

The cut command allows you to select portions of a file or to cut out pieces of a file.