cat: show a file
cat prints the contents of a file straight into the terminal. It is the quickest way to look at a short file:
cat shopping.txtIn the terminal
student@vulntech:~$ cat shopping.txt eggs milk bread apples cheese
The name comes from concatenate, which means joining things together: give cat several files and it prints them one after another:
cat shopping.txt poem.txtIn the terminal
student@vulntech:~$ cat shopping.txt poem.txt eggs milk bread apples cheese The terminal waits, a blinking light, for words typed in by day or night. A single line, a little key, and all the files are there to see.
Line numbers with -n
cat -n poem.txtIn the terminal
student@vulntech:~$ cat -n poem.txt 1 The terminal waits, a blinking light, 2 for words typed in by day or night. 3 A single line, a little key, 4 and all the files are there to see.
Reading system files
Remember that Linux settings are plain text. cat lets you read them:
cat /etc/hostname
cat /etc/os-releaseIn the terminal
student@vulntech:~$ cat /etc/hostname vulntech student@vulntech:~$ cat /etc/os-release NAME="Alpine Linux" ID=alpine VERSION_ID=3.21.0 PRETTY_NAME="Alpine Linux v3.21" HOME_URL="https://alpinelinux.org/" BUG_REPORT_URL="https://gitlab.alpinelinux.org/alpine/aports/-/issues"
tac: backwards
tac (cat spelled backwards) prints the last line first:
tac shopping.txtIn the terminal
student@vulntech:~$ tac shopping.txt cheese apples bread milk eggs
Common mistake:
catprints the whole file at once. On a file with thousands of lines, everything scrolls past and you only see the end. For long files, useless(next lesson).
Remember:
cat fileprints a file.cat -nadds line numbers.