Where to start First steps track · 15 stages
Linux Labs

cat — print a whole file

5 min Ubuntu 24.04 · GNU coreutils 9.4

The cat command prints a file's contents to the terminal and joins several files into one stream.

Cheat sheet

8 commands Printable version
  • cat /etc/hostname print a file
  • cat -n app.conf with line numbers
  • cat a.txt b.txt two files one after another
  • cat a.txt b.txt > all.txt join them into a new file
  • cat -A file show tabs (^I), line ends ($), \r (^M)
  • cat -s file squeeze repeated blank lines
  • tac file lines in reverse order
  • sudo cat /etc/shadow a file only root can read

Overview

cat (concatenate) reads files one after another and prints them to standard output. Most often it's used to look at a short file in full:

text
student@ubuntu:~$ cat /etc/hostname
ubuntu
student@ubuntu:~$ cat -n /etc/hosts
     1	127.0.0.1 localhost
     2	127.0.1.1 ubuntu
     3
     4	# The following lines are desirable for IPv6 capable hosts

Its second job is joining: cat part1 part2 > whole writes both files into one. Without arguments cat reads the keyboard until Ctrl+D — a quick way to create a small file.

A long file scrolls past the screen. Page through it with less, see the start with head and the end with tail.

Options

6

cat has few options, and they all change how output looks. Full list — man cat.

Option What it does Example
-n Number all output lines. cat -n app.conf
-b Number only non-empty lines. cat -b app.conf
-A Show the invisible — tabs as ^I, line ends as $, \r as ^M. cat -A deploy.sh
-s Squeeze repeated blank lines into one. cat -s notes.txt
-E Put $ at the end of each line — trailing spaces become visible. cat -E app.conf
-T Show tabs as ^I. cat -T Makefile

Nothing found. Try a shorter query.

Recipes

4

Write a short file without an editor

bash
cat > notes.txt <<'EOF'
first line
second line
EOF

Quoting EOF turns off expansion: $HOME goes into the file as plain text. > overwrites the file, >> appends to it.

Find invisible characters

text
student@ubuntu:~$ cat -A deploy.sh
#!/bin/bash^M$
echo ok^M$

^M$ at line ends means Windows line breaks (\r\n). They make a script fail with bad interpreter. Strip them: sed -i 's/\r$//' deploy.sh.

Join parts and check the result

bash
cat backup.tar.part-* > backup.tar   # parts in name order
ls -l backup.tar                     # size is the sum of the parts

Search a file without cat

cat file | grep error works, but the extra process isn't needed: grep reads the file itself — grep error file.

Troubleshooting

5

cat: /etc/shadow: Permission denied

Only root or the file's group can read it. Check the permissions: ls -l /etc/shadow. If you really need it — sudo cat /etc/shadow.

Permission denied with sudo cat > /etc/file

The > redirection is done by your shell, without root rights, before sudo even starts. Use tee: echo '127.0.0.1 app' | sudo tee -a /etc/hosts — see tee.

cat: /etc: Is a directory

The argument is a directory. ls lists its contents; find searches the files inside.

The terminal prints garbage after cat

You printed a binary file, and control bytes switched the terminal's mode. Type reset blind and press Enter. file name tells the file type in advance — see file.

The file flew by, only the end is visible

cat doesn't stop at each screen. Read with less file; for the last lines use tail -n 50 file.

Danger zone

1

cat > file wipes the file before reading it

cat a.txt > a.txt leaves an empty file: the shell truncates a.txt for writing before cat starts reading, and no error is printed. The same goes for any > onto an existing file. Append with >>, or write to a new file and then rename it with mv.

See also

  • less — page through a long file with search.
  • head — the first lines of a file.
  • tail — the last lines and following a log.
  • tee — write output to a file with root rights.
  • grep — find the lines you need instead of printing everything.
  • files-basics — an overview of the basic file commands.
Loading…