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 patternworks everywhere, even where other tools don’t take stdin.
catis for files that fit on a screen. For anything longer, reach forless, 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
lessis 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, usetee -aor 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
-nnumbers every line;-bnumbers 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
-Amakes 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
taciscatspelled backwards: it prints the file from the last line up. Great for reading logs newest-first without piping throughtail -r.
8. Or Append with tee
cat notes.txt | tee -a notes.txt
- More useful form:
echo "text" | tee -a log.txtwrites to both the terminal and the file at once. Seetee.
Common Pitfalls
caton huge files floods the terminal. Uselessorhead/tailinstead.cat >silently overwrites. There’s no confirmation. Double-check the target filename, or test withcat file | lessfirst.catmangles binary files. Dumping an image or archive prints garbage and can spam your terminal with escape codes. Runresetif that happens.cat a bdoesn’t tell you where one file ends. If boundaries matter,cat -nor splitting the files first is safer.
Related Commands
| Command | Description |
|---|---|
| less | Scroll through long files page by page |
| head | Print the first lines of a file |
| tail | Print the last lines of a file |
| diff | Compare two files line by line |
| tac | Print a file in reverse |
cat Cheatsheet
| Command | What It Does |
|---|---|
cat file | Print a file |
cat a b > out | Concatenate files into a new one |
cat >> file | Append typed input to a file |
cat > file | Create or overwrite a file (Ctrl+D to end) |
cat -n file | Print with line numbers |
cat -A file | Show hidden characters (tabs, line endings) |
cat file | less | View safely, page by page |