cat Command in Linux with Examples

By 

Updated on

6 min read

Linux Cat Command Examples

The cat command prints file contents to standard output. It is commonly used to view small text files, combine multiple files, create quick text files, and send file contents into pipelines.

When no file is given, cat reads from standard input. When multiple files are given, it prints them in the same order, which is why the name comes from concatenate.

This guide explains the most common cat command examples in Linux, including redirection, line numbers, hidden characters, and when to use tools such as less, head, or tail instead.

cat Command Syntax

The basic cat syntax is:

Terminal
cat [OPTIONS] [FILE_NAMES]
  • OPTIONS - Optional flags that change how cat prints the content.
  • FILE_NAMES - One or more files to read. If no file is given, cat reads from standard input.

Use cat --help or the cat manual page to view all available options.

Displaying File Contents

The most basic and common usage of the cat command is to read the contents of files.

For example, the following command will display the contents of the /etc/issue file on the terminal:

Terminal
cat /etc/issue
cat display contents of file

Redirect Contents of File

Instead of displaying the output to stdout (on the screen), you can redirect it to a file.

The following command will copy the contents of file1.txt to file2.txt using the (>) operator:

Warning
The > operator overwrites the destination file. Use >> when you want to append instead.
Terminal
cat file1.txt > file2.txt
Info
Normally you would use the cp command to copy a file.

If the file2.txt file does not exist, the command will create it. Otherwise, it will overwrite the file.

Use the (>>) operator to append the contents of file1.txt to file2.txt:

Terminal
cat file1.txt >> file2.txt

Same as before, if the file is not present, it will be created.

To display file contents with line numbers, use the -n option:

Terminal
cat -n /etc/os-release
output
1	NAME="Ubuntu"
2	VERSION="24.04 LTS (Noble Numbat)"
3	ID=ubuntu
4	VERSION_CODENAME=noble

The -b option is similar, but it numbers only non-empty lines:

Terminal
cat -b file.txt

Suppress Repeated Empty Lines

Use the -s option to squeeze repeated empty lines into a single empty line:

Terminal
cat -s file.txt

Display Hidden Characters

The cat command can show characters that are usually invisible in the terminal.

To display tab characters as ^I, use -T:

Terminal
cat -T /etc/hosts
output
127.0.0.1^Ilocalhost
127.0.1.1^Ilinuxize

The TAB characters will be displayed as ^I.

To show $ at the end of each line, use -E:

Terminal
cat -E /etc/os-release
output
NAME="Ubuntu"$
VERSION="24.04 LTS (Noble Numbat)"$
ID=ubuntu$
VERSION_CODENAME=noble$

To show tabs, line endings, and other non-printing characters together, use -A:

Terminal
cat -A file.txt

Concatenating Files

When passing two or more file names as arguments to the cat command, the contents of the files will be concatenated. cat reads the files in the sequence given in its arguments and displays the file’s contents in the same sequence.

For example, the following command will read the contents of file1.txt and file2.txt and display the result in the terminal:

Terminal
cat file1.txt file2.txt

You can concatenate two or more text files and write them to a file.

The following command will concatenate the contents of file1.txt and file2.txt and write them to a new file combinedfile.txt using the (>) operator:

Terminal
cat file1.txt file2.txt > combinedfile.txt

If the combinedfile.txt file does not exist, the command will create it. Otherwise, it will overwrite the file.

To concatenate the contents of file1.txt and file2.txt and append the result to combinedfile.txt, use the (>>) operator:

Terminal
cat file1.txt file2.txt >> combinedfile.txt

If the file is not present, it will be created.

You can combine concatenation with options from the previous section, for example cat -n file1.txt file2.txt to number lines in the combined output.

Creating Files

Creating small files with cat is often faster than opening a text editor.

To create a new file, use the cat command followed by the redirection operator (>) and the name of the file you want to create. Press Enter, type the text, and once you are done, press CTRL+D to save the file.

In the following example, we are creating a new file named file1.txt:

Terminal
cat > file1.txt

If a file named file1.txt exists, it will be overwritten. Use the >> operator to append the output to an existing file.

Terminal
cat >> file1.txt

Use CTRL+D on a new line to finish writing input and return to the shell.

Using cat with Pipes

The cat command is often used with pipes to pass file contents to another command. For example, you can display a log file and search for a pattern with grep :

Terminal
cat /var/log/syslog | grep "error"

For a single file, this shorter form is usually better:

Terminal
grep "error" /var/log/syslog

To count the number of lines in a file, you can pipe cat to wc :

Terminal
cat file.txt | wc -l

The direct form is simpler when you do not need a pipeline:

Terminal
wc -l file.txt

Viewing Large Files

While cat is useful for small files, it is not ideal for large files because it prints all content to the terminal at once.

For large files, consider using:

  • less - View file contents one screen at a time with scrolling
  • more - Similar to less but with fewer features
  • head - Display the first lines of a file
  • tail - Display the last lines of a file
Terminal
less /var/log/syslog
head -20 file.txt
tail -50 file.txt

The tac command is the reverse of cat. It prints the file contents in reverse order, starting from the last line:

Terminal
tac file.txt

This is useful when you want to view log files with the most recent entries first.

Troubleshooting

Permission denied
Check the file permissions with ls -l file.txt. If the file requires elevated privileges, read it with sudo cat file.txt.

The output is too long
Use less, head, or tail instead of printing the entire file to the terminal.

The terminal shows strange characters
The file may be binary or contain non-printing characters. Check it with the file command before printing it.

A file was overwritten
The > operator overwrites the destination file. Use >> to append, or enable shell noclobber if you want protection against accidental overwrites.

Hidden characters break a script
Use cat -A file or cat -vET file to show tabs, carriage returns, and other non-printing characters.

Quick Reference

For a printable quick reference, see the cat cheatsheet .

CommandDescription
cat file.txtDisplay file contents
cat file1 file2Display multiple files in order
cat -n file.txtDisplay all lines with line numbers
cat -b file.txtNumber only non-empty lines
cat -s file.txtSuppress repeated empty lines
cat -A file.txtShow hidden characters
cat file1 > file2Copy file contents and overwrite destination
cat file1 >> file2Append to file
cat > file.txtCreate or overwrite a file
cat >> file.txtAppend typed input to a file
tac file.txtDisplay file in reverse

Conclusion

The cat command is best for viewing small text files, combining files, and sending file contents into simple pipelines. For long files, use less , head , or tail so the output stays manageable.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page