Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. Bash Dictionary
  3. cat / tac

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 / OptionDescription
cat filePrints the contents of a file as-is.
cat file1 file2Concatenates multiple files and prints them together.
cat -n filePrints each line with a line number, including blank lines.
cat -b filePrints line numbers for non-blank lines only.
cat -A fileShows tabs as ^I and line endings as $. Useful for inspecting invisible characters.
cat -s fileSqueezes consecutive blank lines into a single blank line.
cat > fileWrites standard input to a file (press Ctrl+D to finish).
cat >> fileAppends standard input to an existing file.
tac filePrints 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

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 .