$ run as a normal user# run as root (or with sudo)
Skills in this chapter
- Redirect output and errors, and join commands with pipelines
Know stdin, stdout and stderr
Every program has three standard streams. Each one has a number (a file descriptor).
| Number | Name | Default |
|---|---|---|
| 0 | Standard input (stdin) | The keyboard |
| 1 | Standard output (stdout) | The screen |
| 2 | Standard 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.
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. > filewith 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.
Send errors to a file
Put the stream number before the sign: 2> redirects standard error.
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.
Throw away output with /dev/null
/dev/null is a special file that accepts everything and keeps nothing: the "bit bucket".
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 > allmakes one file.cat > filereads from the keyboard. End the input withCtrl-D.- With no file name,
catreads stdin;< filegives it a file as stdin.
Join commands with pipelines
command1 | command2 sends the stdout of command1 into the stdin of command2.
Remember
> joins a command with a file. | joins a command with another command.
Use filters in a pipeline
A filter reads input, changes it and writes output. sort, uniq, wc, grep, head and tail are filters.
| Command | What it does | Useful options |
|---|---|---|
sort | Sort lines | -n numbers, -r reverse |
uniq | Remove repeated lines (input must be sorted) | -d only the repeated, -c count |
wc | Count lines, words, bytes | -l only lines |
grep | Show lines that match a pattern | -i ignore case, -v invert |
head | First 10 lines | -n 5 first 5 |
tail | Last 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/messagesgrep -i zipignores upper and lower case.grep -v zipshows the lines without zip.tail -fkeeps watching a log file. Stop it withCtrl-C.
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.
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.
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.