Advanced
Searching inSummaries
How to search
  • Words find lesson titles, text and code. Filter by type (guides, courses, programs, books) or stay in this book.
  • book:rhcsa and ch:8 narrow the search; #code finds a word only in commands of the lessons.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

Chapter 6Name in the book: Redirection

Redirect input and output, build pipelines

This is maybe the most powerful idea of the command line: the output of a command can go to a file, and it can become the input of the next command. Small programs that do one job well are joined with the pipe | into one strong tool.

  • 10 Lessons
  • 12 Commands
  • 1 Labs

$ run as a normal user# run as root (or with sudo)

Skills in this chapter

  • Redirect output and errors, and join commands with pipelines

6.1

Know stdin, stdout and stderr

Every program has three standard streams. Each one has a number (a file descriptor).

NumberNameDefault
0Standard input (stdin)The keyboard
1Standard output (stdout)The screen
2Standard error (stderr)The screen
  • Results go to stdout. Error messages go to stderr. Both appear on the screen, so they look the same, but they are two different channels.
  • Redirection sends a stream to another place: a file or another program.

6.2

Send output to a file

> writes stdout to a file and replaces what was there. >> adds to the end of the file.

ls -l /usr/bin > ls-output.txt
ls -l ls-output.txt
-rw-r--r--. 1 me me 167878 Nov  8 ls-output.txt
ls -l /bin/usr > ls-output.txt
ls: cannot access '/bin/usr': No such file or directory
ls -l ls-output.txt
-rw-r--r--. 1 me me 0 Nov  8 ls-output.txt
ls -l /usr/bin >> ls-output.txt
> empty.txt
  • The error message still came to the screen: > only redirects stdout.
  • The file was emptied anyway: > truncates the file before the command starts.
  • > file with no command makes a new empty file or empties an existing one.

Watch out

sort file > file destroys the file: the shell empties it before sort can read it. Write to a new file instead.

6.3

Send errors to a file

Put the stream number before the sign: 2> redirects standard error.

ls -l /bin/usr 2> ls-error.txt
cat ls-error.txt
ls: cannot access '/bin/usr': No such file or directory

Commands in this lesson

6.4

Send output and errors to the same file

&> sends stdout and stderr to one file. The old way is > file 2>&1, and the order is important.

ls -l /bin/usr &> ls-output.txt
ls -l /bin/usr &>> ls-output.txt
ls -l /bin/usr > ls-output.txt 2>&1

Watch out

2>&1 > file (other order) does not work: stderr goes to the screen, because it was copied before stdout was moved to the file.

Commands in this lesson

6.5

Throw away output with /dev/null

/dev/null is a special file that accepts everything and keeps nothing: the "bit bucket".

ls -l /bin/usr 2> /dev/null
find / -name "*.conf" 2> /dev/null

At work

2> /dev/null hides "Permission denied" messages when you search the whole disk as a normal user.

6.6

Read input from a file

< makes a file the standard input of a command. cat reads its input and writes it to stdout.

cat movie.mpeg.0* > movie.mpeg
cat > lazy_dog.txt
The quick brown fox jumped over the lazy dog.
cat < lazy_dog.txt
The quick brown fox jumped over the lazy dog.
  • cat = "concatenate": it joins files. cat a b c > all makes one file.
  • cat > file reads from the keyboard. End the input with Ctrl-D.
  • With no file name, cat reads stdin; < file gives it a file as stdin.

Commands in this lesson

6.7

Join commands with pipelines

command1 | command2 sends the stdout of command1 into the stdin of command2.

ls -l /usr/bin | less
ls /bin /usr/bin | sort | less
ls /bin /usr/bin | sort | uniq | wc -l

Remember

> joins a command with a file. | joins a command with another command.

Watch out

command > less does not open the pager: it makes a file called less. As root, ls > less in /usr/bin would overwrite the real less program.

6.8

Use filters in a pipeline

A filter reads input, changes it and writes output. sort, uniq, wc, grep, head and tail are filters.

CommandWhat it doesUseful options
sortSort lines-n numbers, -r reverse
uniqRemove repeated lines (input must be sorted)-d only the repeated, -c count
wcCount lines, words, bytes-l only lines
grepShow lines that match a pattern-i ignore case, -v invert
headFirst 10 lines-n 5 first 5
tailLast 10 lines-n 5, -f follow a growing file
ls /bin /usr/bin | sort | uniq -d | less
wc ls-output.txt
  7902  64566 503634 ls-output.txt
ls /bin /usr/bin | sort | uniq | grep zip
ls /usr/bin | tail -n 5
tail -f /var/log/messages

6.9

Save and see at the same time with tee

tee reads stdin and writes it to a file and to stdout. It is like a T-piece in a water pipe.

ls /usr/bin | tee ls.txt | grep zip
bunzip2
bzip2
gunzip
gzip
unzip
zip

At work

command | sudo tee /etc/file writes as root into a file. sudo command > /etc/file does not work, because the redirection runs as your user.

6.10

Summary of the chapter

> >> 2> &> < and | connect programs to files and to each other.

1How do you send both the output and the errors of a command to all.log?

command &> all.log (or command > all.log 2>&1).

2Why does uniq need sorted input?

It only removes repeated lines that are next to each other. sort puts equal lines together.

3Which command shows the result on the screen and saves it in a file at the same time?

tee, for example ls | tee list.txt.

Practice

All commands in this chapter