cat / tac
cat is a command that prints file contents to standard output. The name comes from its ability to concatenate multiple files. tac does the reverse — it prints lines from the last line to the first.
Syntax
cat [options] [file...] tac [options] [file...]
Options
| Command / Option | Description |
|---|---|
| cat file | Prints the contents of a file as-is. |
| cat file1 file2 | Concatenates multiple files and prints them together. |
| cat -n file | Prints each line with a line number, including blank lines. |
| cat -b file | Prints line numbers for non-blank lines only. |
| cat -A file | Shows tabs as ^I and line endings as $. Useful for inspecting invisible characters. |
| cat -s file | Squeezes consecutive blank lines into a single blank line. |
| cat > file | Writes standard input to a file (press Ctrl+D to finish). |
| cat >> file | Appends standard input to an existing file. |
| tac file | Prints the lines of a file in reverse order (last line first). |
Sample Code
The following files are used in the examples below.
~/project/script.sh
#!/bin/bash
echo "Hello, World!"
~/project/numbers.txt
apple
banana
cherry
apple banana cherry
Print the contents of a file.
cat script.sh #!/bin/bash echo "Hello, World!"
Print with line numbers. Blank lines are also numbered.
cat -n script.sh
1 #!/bin/bash
2
3 echo "Hello, World!"
Inspect invisible characters. If Windows-style line endings (\r) are present, they appear as ^M.
cat -A script.sh #!/bin/bash$ $ echo "Hello, World!"$
Concatenate multiple files and write the result to a single file.
cat header.txt body.txt footer.txt > output.txt
Write multiple lines to a file at once using a here document.
cat > config.txt <<'EOF' host=localhost port=8080 debug=true EOF
Print a file in reverse order with $ tac.
tac numbers.txt cherry banana apple
A common use case is to show the most recent log entries first.
tac /var/log/app.log | head -20
Notes
Printing a large file with $ cat will flood the terminal. Use less when you want to scroll through the output. To inspect just the beginning or end of a file, head / tail is more convenient.
$ cat is commonly combined with redirects and pipes. See Redirect (>) for details.
If you find any errors or copyright issues, please contact us.