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

cat, tac

Print files to the screen, join files together (cat), or print them last line first (tac).

Niveau
RHCSA-examen
Groep
Tekst en zoeken
Root nodig
Nee
Pakket
coreutils
Ook
tac

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

Wat het doet

cat (concatenate) writes the content of one or more files to standard output. With one file you read it; with several files you join them; with a redirect you create a new file.

It is perfect for short files. For long files, use less (scrolling) or head/tail (parts).

tac does the same, but starts with the last line (useful for logs: newest first).

Zo schrijf je het

cat [OPTION]... [FILE]...
tac [OPTION]... [FILE]...

Opties 15

Meest gebruikt

-n, --number
Number all lines.
-b, --number-nonblank
Number only non-empty lines.
-A, --show-all
Show hidden characters: tabs as ^I, line ends as $, others as ^ or M- (same as -vET).
-E, --show-ends
Show $ at the end of each line.
-T, --show-tabs
Show tabs as ^I.
-v, --show-nonprinting
Show non-printable characters with ^ and M- notation.
-e
Same as -vE.
-t
Same as -vT.
-s, --squeeze-blank
Show several empty lines as one.
-u
Ignored.
--help
Show help.
--version
Show the version.

tac

-b, --before
Put the separator before the record instead of after.
-r, --regex
Treat the separator as a regular expression.
-s, --separator=string
Use this separator instead of a new line.

Voorbeelden 12

$ als gewone gebruiker# als root (of met sudo)

  1. 1Show a short file

    cat /etc/hostname
    server1.example.com

    The fastest way to read a small file.

  2. 2Show line numbers

    cat -n /etc/fstab

    Easy to talk about "line 7".

  3. 3Find hidden characters (tabs, Windows line ends)

    cat -A deploy.sh | head
    #!/bin/bash^M$

    ^M$ means Windows line ends: Bash will fail. ^I would show tabs (bad in YAML).

  4. 4Join several files into one

    cat part1.csv part2.csv part3.csv > all.csv

    The files are added one after the other.

  5. 5Create a file with several lines (here-document)

    cat > /tmp/test.conf <<'EOF'
    port=8080
    log=/var/log/app.log
    EOF

    Everything until the line EOF goes into the file.

  6. 6Add lines to a root-owned file

    cat <<'EOF' | sudo tee -a /etc/hosts
    192.168.122.20 server2.example.com server2
    EOF

    sudo tee -a appends as root.

  7. 7Show the newest log lines first

    tac /var/log/dnf.log | head -20

    The last 20 events, newest at the top.

  8. 8Show the OS version

    cat /etc/os-release

    NAME, VERSION_ID and ID of the distribution.

  9. 9Squeeze empty lines while reading

    cat -s notes.txt

    Several empty lines become one.

  10. 10Read kernel information

    cat /proc/cpuinfo | grep "model name" | head -1

    /proc files are created by the kernel when you read them.

  11. 11Show a file only if it exists

    [ -f /etc/motd ] && cat /etc/motd

    No error message in a script when the file is missing.

  12. 12Copy a disk image to a USB stick (as a simple dd)

    cat rhel-10.iso > /dev/sdX

    Works like dd for this job. Check the device name three times: it overwrites the whole disk.

Let op

  • cat bigfile floods the screen; use less.
  • cat file > file empties the file before reading it. Never read and write the same file in one command.
  • "Useless use of cat": cat file | grep x works, but grep x file is simpler.

Tips om te onthouden

  • cat = show/join, tac = backwards, cat -A = invisible characters, cat > file <<'EOF' = new file.

Volledige handleiding

Deze pagina legt het commando uit in eenvoudige woorden. De originele handleiding heeft elk detail.

In je terminal: man cat