Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

cat Command Guide for Printing and Concatenating Files

cat prints the contents of a file to the terminal — and just as usefully, it concatenates files, in either direction, to build new ones.


What is cat, and Why Use It?

cat (“concatenate”) is one of the oldest and most-used commands in Linux. Point it at a file and it dumps the contents to standard output. Give it several files and it prints them one after another — the original reason for the name.

It also has a writing side: with redirection, cat becomes a quick way to create or overwrite small files straight from the keyboard.

Why Learn cat?

  • You want to read a short file without paging through it.
  • You’re joining files together — combining logs, merging configs, splitting headers off data.
  • You’re creating a small file in a script or one-liner (cat > notes.txt).
  • It’s the glue in pipelines: cat file | grep pattern works everywhere, even where other tools don’t take stdin.

cat is for files that fit on a screen. For anything longer, reach for less, which lets you scroll without flooding your terminal.


1. Print a Whole File

cat filename.txt

Example:

cat /etc/hostname
  • The file scrolls past as fast as your terminal prints it. On a long file this is rarely useful — that’s what less is for.

2. Print Several Files One After Another

cat part1.txt part2.txt

Example:

cat intro.txt body.txt outro.txt > full-document.txt
  • The output order follows the argument order, not the filesystem. This is the “concatenate” in cat — combine as many files as you like.

3. Create or Overwrite a Small File

cat > newfile.txt

Example:

cat > config.ini
server=example.com
port=8080
  • Type the content, then press Ctrl+D (or Enter followed by Ctrl+D on some systems) to finish.
  • > overwrites the file if it already exists — there is no undo. For appending instead, use tee -a or plain >> redirection.

4. Append to an Existing File

cat >> log.txt
  • Same as the previous step, but >> adds to the end instead of replacing what’s there.

5. Number the Lines

cat -n script.sh

Example:

cat -n /etc/passwd | less
  • -n numbers every line; -b numbers only non-empty lines. Handy when you need to refer to “line 42” in a discussion or bug report.

6. Show Hidden Characters

cat -A file.txt
  • -A makes tabs, end-of-line markers, and non-printing characters visible. Essential when debugging Windows line endings (^M$), stray tabs, or files that “look fine” but break scripts.

7. Concatenate Into a New File (Reverse Order Too)

cat file1 file2 > combined.txt
tac file.txt
  • tac is cat spelled backwards: it prints the file from the last line up. Great for reading logs newest-first without piping through tail -r.

8. Or Append with tee

cat notes.txt | tee -a notes.txt
  • More useful form: echo "text" | tee -a log.txt writes to both the terminal and the file at once. See tee.

Common Pitfalls

  • cat on huge files floods the terminal. Use less or head / tail instead.
  • cat > silently overwrites. There’s no confirmation. Double-check the target filename, or test with cat file | less first.
  • cat mangles binary files. Dumping an image or archive prints garbage and can spam your terminal with escape codes. Run reset if that happens.
  • cat a b doesn’t tell you where one file ends. If boundaries matter, cat -n or splitting the files first is safer.

CommandDescription
lessScroll through long files page by page
headPrint the first lines of a file
tailPrint the last lines of a file
diffCompare two files line by line
tacPrint a file in reverse

cat Cheatsheet

CommandWhat It Does
cat filePrint a file
cat a b > outConcatenate files into a new one
cat >> fileAppend typed input to a file
cat > fileCreate or overwrite a file (Ctrl+D to end)
cat -n filePrint with line numbers
cat -A fileShow hidden characters (tabs, line endings)
cat file | lessView safely, page by page