...

/

Pipes and Redirects

Pipes and Redirects

In this lesson you'll look at basic redirection and pipes. You'll also learn about file descriptors, 'standard out' and 'standard error' and 'special' files like '/dev/null'.

Pipes and redirects are used very frequently in bash and by all levels of user.

This can cause a problem. They are used so often by all users of bash that many don’t understand their subtleties, how they work, or their full power.

How Important is this Lesson?

This lesson is essential.

Basic Redirects

Start off by creating a file:

echo "contents of file1" > file1
Terminal 1
Terminal
Loading...

Note: continue to use this terminal during this lesson. Throughout this course it is assumed that you complete each lesson by typing in the commands in the provided terminal for that lesson in the order the commands are given.

The > character is the redirect operator. This takes the output from the preceding command that you’d normally see in the terminal and sends it to a file that you give it. Here it creates a file called file1 and puts the echoed string into it. Try cating the file if you want to check by running:

cat file1

There’s a subtlety here which we’ll get to: sometimes not all the output you see in the terminal would get redirected by this, but don’t worry about this yet.

Basic Pipes

Now type this in:

cat file1 | grep -c file

Note: If you don’t know what grep is, you will need to learn. This is a good place to start: https://en.wikipedia.org/wiki/Grep

Normally you’d run a grep with the filename as the last argument, but instead here we pipe the contents of file1 into the grep command by using the ‘pipe’ operator: |. The resulting output of 1 indicates the number lines that matched the word file in the the file called file1.

A ...