← Linux and the Command Line Lesson 12 of 51 · 10 XP

Pipes and redirection

Theory and problem statement

Machine translation, not yet reviewed by a native speaker.

The power of the command line is in connecting small programs. Every program reads standard input (stdin) and writes to standard output (stdout), and errors go to stderr.

A pipe | passes the output of one command to the input of another:

cat access.log | grep 404 | wc -l      # how many lines have a 404 error
ls | sort -r | head -3                 # the three names that come last alphabetically

Redirection — into files and out of files:

> file output into a file (overwrite)
>> file append to the end
< file take the input from a file
2> file errors into a file
2>&1 errors to the same place as the output
> /dev/null throw the output away (a "black hole")
find / -name "*.conf" 2> /dev/null     # without the "Permission denied" messages
make > build.log 2>&1                  # both the output and the errors — into the log

This is the Unix philosophy: "write programs that do one thing and do it well, and that work together".

Task. The script gets a list of words as its input, one per line. Connect tr a-z A-Z (convert to upper case) and sort into a pipe. Write the result to the file out.txt, and then print that file.

Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.