How to Use the cat Command in Linux

By 

Updated on

5 min read

Linux Cat Command Examples

The cat command is one of the most widely used commands in Linux. The name of the cat command comes from its functionality to concatenate files. It can read, concatenate, and write file contents to the standard output. If no file is specified or the input file name is specified as a single hyphen (-), it reads from the standard input.

cat is most commonly used to display the contents of one or multiple text files, combine files by appending one file’s contents to the end of another file, and create new files.

In this article, we will show you how to use the cat command through practical examples.

cat Command Syntax

Before going into how to use the cat command, let’s start by reviewing the basic syntax.

The cat command syntax takes the following form:

Terminal
cat [OPTIONS] [FILE_NAMES]
  • OPTIONS - cat options . Use cat --help to view all available options.
  • FILE_NAMES - Zero or more file names.

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:

Terminal
cat file1.txt > file2.txt
Info
Normally you would use the cp command to copy a file.

If the file2.txt file doesn’t 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 contents of a file with line numbers, invoke cat with 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

Suppress Repeated Empty Lines

Use the -s option to omit the repeated empty output lines:

Terminal
cat -s file.txt

Display TAB characters

The -T option allows you to visually distinguish between tabs and spaces.

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

The TAB characters will be displayed as ^I.

Display End of Lines

To display the invisible line ending character use the -e argument:

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

The line endings will be displayed as $.

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 doesn’t 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.

When concatenating files with cat, you can use the same arguments as shown in the previous section.

Creating Files

Creating small files with cat is often easier than opening a text editor such as nano , Vim, Sublime Text , or Visual Studio Code .

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

Using cat with Pipes

The cat command is often used in combination with other commands through pipes. For example, to display the contents of a file and search for a specific pattern, you can pipe cat to grep :

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

To count the number of lines in a file:

Terminal
cat file.txt | wc -l

Viewing Large Files

While cat is useful for small files, it’s not ideal for large files because it dumps all the 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.

Quick Reference

CommandDescription
cat file.txtDisplay file contents
cat -n file.txtDisplay with line numbers
cat -s file.txtSuppress repeated empty lines
cat file1 file2Concatenate multiple files
cat file1 > file2Copy file contents
cat file1 >> file2Append to file
cat > file.txtCreate a new file
tac file.txtDisplay file in reverse

For a printable quick reference, see the cat cheatsheet .

Conclusion

The cat command is used to display, concatenate, and create files in Linux. For viewing large files, use less or head/tail instead.

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