Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

Hoofdstuk 9Naam in de cursus: Redirecting Shell Input and Output

Redirect input and output in the shell

Linux tools are small and do one job well. The power comes when you connect them: send the output of one command to a file, hide error messages, or pass the result into the next command with a pipe. This chapter explains the three data channels and all the redirect signs.

  • 7 Lessen
  • 31 Commando's
  • 2 Labs

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

9.1

Know the three data channels

Every program has three channels, each with a number (a file descriptor).

NumberNameDefault
0stdin (standard input)The keyboard
1stdout (standard output)The screen
2stderr (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.

ls /etc/hosts /nothing
ls: cannot access '/nothing': No such file or directory
/etc/hosts
ls /etc/hosts /nothing 2> /dev/null
/etc/hosts
ls /etc/hosts /nothing > /dev/null
ls: cannot access '/nothing': No such file or directory

Commando's in deze les

9.2

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.

9.3

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
SignWhat it does
2> FILEErrors to a file
2>> FILEErrors added to a file
2> /dev/nullThrow the errors away
> FILE 2>&1Output and errors to the same file (the order matters!)
&> FILEOutput and errors to the same file (short form)
&>> FILEOutput 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.

Commando's in deze les

9.4

Read input from a file

< gives a file to a command as its input, as if you typed it.

wc -l < /etc/passwd
38
sort < names.txt
mail -s "Disk report" admin@example.com < /tmp/report.txt
while read host; do ping -c1 -W1 "$host" > /dev/null && echo "$host up"; done < hostlist.txt

9.5

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:

CommandJob in the pipe
grep WORDKeep only lines with WORD
sort / sort -n / sort -hSort as text / as numbers / as sizes like 4K, 2G
uniq -cCount lines that repeat (sort first!)
wc -lCount lines
head / tailKeep the first / last lines
cut -d: -f1Keep one column
awk '{print $1}'Keep one field (split by spaces)
tr a-z A-ZChange characters
lessRead 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.20

Op 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 |&.

9.6

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.

dnf check-update | tee /tmp/updates.txt
ls -l /etc | tee /tmp/etc.txt | wc -l
make 2>&1 | tee build.log
echo "10.0.0.5  db1" | sudo tee -a /etc/hosts
  • tee -a adds to the file instead of overwriting it.
  • sudo tee is the right way to write a root-only file from a normal user's pipe.

9.7

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_64

Onthoud

Redirection (>, <, 2>) connects a command with a file. A pipe (|) connects a command with another command. $( ) puts output into a command line.

Oefenen

Alle commando's in dit hoofdstuk