LearnWithShobhan

Linux pipes and redirection explained: |, >, >>, < and grep

Shobhan Biswas··5 min read
>_From the course CLI Mastery: 57 lessons, lifetime access.

Pipes are the reason experienced developers love the terminal. One line like ps aux | grep node does what would take minutes of clicking. Once you understand where a command's input comes from and where its output goes, you can glue small commands into surprisingly powerful tools.

Every command has an input and an output

Most commands take some input, process it, and produce output:

  • Standard input (stdin) is, by default, your keyboard.
  • Standard output (stdout) is, by default, your terminal window.

This idea comes from the days before monitors, when the input device was literally a typewriter and the output device was a printer.

Diagram: input from a keyboard (stdin) goes into a command, output goes to the terminal window (stdout)
By default: stdin is the keyboard, stdout is the terminal.

You can see stdin in action by running cat with nothing after it. It waits for you to type, and echoes each line back. Press Ctrl + C to stop it.

bash
$ cat
hello
hello
^C

Redirect output to a file: > and >>

The right angle bracket sends a command's output into a file instead of the screen:

bash
$ echo hello > output
$ cat output
hello
$ echo hi > output
$ cat output
hi
$ echo "second line" >> output
$ cat output
hi
second line
OperatorWhat it does
>Write output to a file. Creates the file, or overwrites it if it exists
>>Append output to the end of a file

It works with any command. ls -la > ls-output prints nothing on screen, because the whole listing went into the file.

Feed input from a file: <

The left angle bracket does the opposite: it feeds a file as the input of a command.

cat file-1.txt     # the file path is an argument
cat < file-1.txt   # the file content is the input (stdin)

Same result for cat, but a different mechanism. That difference matters: some commands read stdin and some don't. echo only uses arguments, so echo < file-1.txt prints an empty line.

Pipes: connect one command's output to the next one's input

The pipe | (Shift + backslash on most keyboards) takes the output of the command on the left and makes it the input of the command on the right.

  1. command 1

    cat people.txt

    prints the names

  2. command 2

    sort

    sorts them

  3. command 3

    uniq

    removes duplicates

  4. stdout

    Terminal

    sorted, unique list

Each command does one small job. The pipe connects them.
bash
$ cat people.txt
John
Bob
Bob
Rachel
Doe
$ cat people.txt | sort | uniq
Bob
Doe
John
Rachel

This is the core idea of Unix. Instead of one giant program for every task, Unix ships many small programs that each do one thing well, and the pipe lets you combine them to solve bigger problems.

Watch pipes explained in the free lesson:

Free to watch. Get all 57 lessons →

grep: the pipe's best friend

grep searches text for a pattern and prints only the matching lines. Combined with a pipe, it filters the output of any command.

Find a running process without scrolling through hundreds of lines:

bash
$ ps aux | grep login
root 412 0.0 0.1 /usr/bin/login -pf shobhan
you 9921 0.0 0.0 grep login

(The last line is the grep process itself, which also contains the word "login".)

A few flags you will use constantly:

cat people.txt | grep -i bob     # -i: ignore case (grep is case sensitive by default)
grep -n "error" app.log          # -n: show line numbers
grep -r "TODO" src/              # -r: search every file in a directory
tail -f app.log | grep ERROR     # watch a live log for errors only

/dev/null: the black hole

Sometimes you want a command to run but don't want its output at all, not on screen and not in a file. Redirect it to /dev/null, a special file that discards everything written to it:

cat people.txt > /dev/null           # output disappears
./backup.sh > /dev/null 2>&1         # discard output AND errors (common in cron jobs)

2>&1 means "send errors (stream 2) wherever normal output (stream 1) is going", so both vanish.

Cheat sheet

SyntaxMeaning
cmd > fileOutput to file (overwrite)
cmd >> fileOutput to file (append)
cmd < fileFile as input
cmd1 | cmd2Output of cmd1 becomes input of cmd2
cmd | grep wordKeep only lines containing "word"
cmd > /dev/null 2>&1Throw away all output

Key takeaways

  • ✓stdin defaults to the keyboard, stdout to the terminal.
  • ✓> overwrites a file, >> appends to it, < feeds a file as input.
  • ✓A pipe | turns one command's output into the next command's input.
  • ✓grep filters lines; -i ignores case, -r searches folders, -n shows line numbers.

Get fluent, not just familiar

Pipes are where the terminal starts paying off. CLI Mastery builds up to this step by step, then uses it in shell scripts and on a real AWS server. 57 lessons, lifetime access. Start with the free pipes lesson.

Free download

Linux command line cheat sheet

One printable page: navigation, files, permissions, pipes, processes, scripts and SSH.

Occasional emails with new tutorials and courses. Unsubscribe any time.

Keep reading