Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
Vaardigheden in dit hoofdstuk
- Uitvoer naar bestanden en andere commando's sturen
Know the three data channels
Every program has three channels, each with a number (a file descriptor).
| Number | Name | Default |
|---|---|---|
| 0 | stdin (standard input) | The keyboard |
| 1 | stdout (standard output) | The screen |
| 2 | stderr (standard error) | The screen |
Normal results go to channel 1, error messages to channel 2. Both appear on the screen, so they look the same — but you can send them to different places.
Send output to a file
> writes the output to a file (the file is emptied first). >> adds it to the end.
date > /tmp/report.txt
uptime >> /tmp/report.txt
df -h >> /tmp/report.txt
cat /tmp/report.txt
> /tmp/empty.txt> FILE alone, without a command, makes an empty file or empties an existing one.
Let op
> on the wrong file destroys it at once — even /etc/passwd. Turn on protection with set -o noclobber: then > refuses to overwrite, and >| forces it.
Handle error messages
2> sends only the errors somewhere else. /dev/null is a black hole: data sent there is gone.
find / -name "*.repo" 2> /dev/null
find / -name "*.repo" 2> /tmp/errors.txt
find / -name "*.repo" > /tmp/found.txt 2> /tmp/errors.txt
find / -name "*.repo" > /tmp/all.txt 2>&1
find / -name "*.repo" &> /tmp/all.txt
backup.sh >> /var/log/backup.log 2>&1| Sign | What it does |
|---|---|
2> FILE | Errors to a file |
2>> FILE | Errors added to a file |
2> /dev/null | Throw the errors away |
> FILE 2>&1 | Output and errors to the same file (the order matters!) |
&> FILE | Output and errors to the same file (short form) |
&>> FILE | Output and errors added to the same file |
Onthoud
2>&1 means "send channel 2 to where channel 1 goes now". Write it after > FILE. cmd 2>&1 > file puts the errors on the screen, not in the file.
Read input from a file
< gives a file to a command as its input, as if you typed it.
Connect commands with pipes
The pipe | sends the output of the left command into the input of the right command. Many small tools build one big answer.
ps aux | grep httpd
ls -l /etc | less
cat /etc/passwd | cut -d: -f1 | sort
dnf list --installed | wc -l
journalctl -p err -b | tail -n 20
ss -tln | grep ':22 'Typical "filter" commands in a pipe:
| Command | Job in the pipe |
|---|---|
grep WORD | Keep only lines with WORD |
sort / sort -n / sort -h | Sort as text / as numbers / as sizes like 4K, 2G |
uniq -c | Count lines that repeat (sort first!) |
wc -l | Count lines |
head / tail | Keep the first / last lines |
cut -d: -f1 | Keep one column |
awk '{print $1}' | Keep one field (split by spaces) |
tr a-z A-Z | Change characters |
less | Read the result page by page |
Example: the 5 IP addresses with the most failed SSH logins.
grep "Failed password" /var/log/secure | awk '{print $(NF-3)}' | sort | uniq -c | sort -rn | head -n 5
41 203.0.113.50
12 198.51.100.7
3 192.168.122.20Op het werk
Build a pipe step by step. Run the first part, look at the output, add the next |, look again.
Notitie
A pipe only passes channel 1. To pipe errors too, use 2>&1 | or |&.
Save and show at the same time with tee
tee FILE writes its input to a file and passes it on — a "T" in the pipe.
Join output with command substitution
$(command) puts the output of a command into another command line.
echo "This host: $(hostname), kernel $(uname -r)"
cp /etc/fstab /root/fstab.$(date +%F)
kill $(pgrep -u anna sleep)
rpm -qf $(which ss)
iproute-6.11.0-1.el10.x86_64Onthoud
Redirection (>, <, 2>) connects a command with a file. A pipe (|) connects a command with another command. $( ) puts output into a command line.