cat – concatenate files and print to standard output

cat command
cat command

cat concatenates files and prints them to standard output. It’s the go-to tool for viewing small text files.

Synopsis

cat [OPTIONS] [FILE]...

Common Options

OptionDescription
-nNumber all output lines
-bNumber non-empty lines only
-sSqueeze multiple blank lines into one
-AShow all characters (equivalent to -vET)
-EShow $ at end of each line

Examples

View a file

$ cat /etc/hosts
127.0.0.1   localhost
::1         localhost

Concatenate multiple files

$ cat header.txt body.txt footer.txt > document.txt

Number lines (all)

$ cat -n /etc/passwd | head -3
     1  root:x:0:0:root:/root:/bin/bash
     2  daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
     3  bin:x:2:2:bin:/bin:/usr/sbin/nologin

Number lines (non-empty only)

$ cat -b file-with-blanks.txt
     1  First line
     2  Second line

     3  Fourth line (blank line above not numbered)

Tips

  • For large files, use less or more instead
  • To reverse line order, use tac
  • Create files quickly: cat > newfile.txt then type content, press Ctrl+D to save
  • Combine with pipes: cat access.log | grep ERROR | wc -l

See Also

  • less — View files with scrolling and search
  • more — Simple file pager
  • head — Show first N lines
  • tail — Show last N lines
  • tac — Reverse line order

Tutorials