Write a short file without an editor
Quoting EOF turns off expansion: $HOME goes into the file as plain text. >
overwrites the file, >> appends to it.
The cat command prints a file's contents to the terminal and joins several files into one stream.
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
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:
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.
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.
Quoting EOF turns off expansion: $HOME goes into the file as plain text. >
overwrites the file, >> appends to it.
^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.
cat file | grep error works, but the extra process isn't needed: grep reads
the file itself — grep error file.
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.
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.
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.
cat doesn't stop at each screen. Read with less file; for the last lines use
tail -n 50 file.
cat > file wipes the file before reading itcat 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.
Found an inaccuracy? Report an error in the article