Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

The cat Commands You Will Use Most

cat /etc/os-release
cat -n deploy.sh
cat part1.sql part2.sql > full.sql
cat -A config.yml

cat reads one or more files and writes them to the screen, one after the other; the name comes from "concatenate". The first line prints a short file, the second adds line numbers, the third joins two files into a new one, and the fourth shows characters you cannot normally see. cat is part of every Linux distribution, so there is nothing to install.

Read a Short File

cat /etc/hostname
sudo cat /etc/ssh/sshd_config

cat prints the whole file at once and returns to the prompt. That suits configuration files and short notes. Files that only root can read, such as many under /etc and /var/log, need sudo; without it, cat stops with "Permission denied". If a file is longer than your terminal window, the start scrolls away, and the next section shows better tools for that.

Large Files and Logs: less, head, or tail

CommandWhen to use it
less app.logPage through a long file; / searches, q quits.
head -n 20 app.logThe first 20 lines.
tail -n 50 app.logThe last 50 lines.
tail -f app.logFollows new lines as they arrive.
zcat app.log.2.gzReads a compressed, rotated log.

Running cat on a log of several gigabytes floods the terminal and can keep an SSH session busy for minutes. The tail command guide covers following logs. To search a file, run grep on the file directly instead of cat file | grep; see how to grep for multiple strings.

Number the Lines

cat -n nginx.conf
cat -b nginx.conf

-n numbers every line, which helps when an error message names a line number. -b numbers only the lines that are not empty. -s squeezes several empty lines in a row into one.

Show Tabs, Line Endings, and Hidden Characters

cat -A config.yml

-A makes invisible characters visible: each tab appears as ^I, the end of each line as $, and a carriage return as ^M. That solves two common problems. A YAML file that fails to load often has a tab where spaces belong. A script that fails with "bad interpreter" usually shows ^M$ at the end of its lines, because it has carriage-return line endings from another system. A space before the $ reveals trailing whitespace.

Join Files and Append to a File

cat header.html body.html footer.html > page.html
cat new-hosts.txt >> hosts-list.txt
cat access.log.1 access.log > combined.log

> creates the output file, or empties it first if it already exists. >> adds to the end and keeps what is there. The files are joined in the order you list them.

Warning: never use the same file as input and output. In cat file.txt > file.txt the shell empties file.txt before cat starts, so the content is gone. Write to a new name, check the result, then rename it with mv.

Write a File with a Here-Document

cat > app.env <<'EOF'
APP_ENV=production
APP_PORT=8080
EOF

Every line between the first line and the closing EOF goes into the file. The quotes around the first 'EOF' stop the shell from replacing $VARIABLES in the text; leave them off when you want variables filled in. The closing EOF must stand alone on its line. For files under /etc, the redirect runs as your user, so use sudo tee instead, as shown in how to create a file in Linux.

Read a File Backwards with tac

tac app.log | head -n 20

tac is cat spelled backwards and prints the last line first. Combined with head, it puts the newest log entries at the top.

Common Problems

  • The terminal shows garbage: you ran cat on a binary file. Type reset and press Enter. Check a file's type first with file name.
  • No such file or directory: the path or the case of a letter is wrong; see No such file or directory.
  • cat waits and prints nothing: you gave no file name, so cat reads from the keyboard. Press Ctrl+C.
Was this answer helpful?
Loading...