Skip to content

How to use the Cat command in Linux?

  • by
  • 3 min read

The Linux terminal is by far the most powerful and effective way of doing just about anything on your Linux machine. There are commands and utilities for doing anything from checking up on files to manipulating processes running on your computer.

The cat command is one of the most popular commands used on Linux systems. In this article, we’re going over what the cat command is and how can you use it on your Linux machine. 

Also read: Debian vs Ubuntu: Which Linux distro should you pick?


What is the cat command?

The cat command is used to view file contents and merge multiple files by appending them into one another. You can also use this command to create new files.

The basic syntax for the command is also rather easy. Type cat followed by the file you want to view. 

cat file1.txt
How to use the Cat command in Linux? | Candid.Technology

The above command will show the contents of the text file named file1.

Also read: How to find large files in Linux?


Using the cat command in Linux

Aforementioned, the basic cat syntax is as follows.

cat [flag] [filename]

How to view files with cat command in Linux?

To view the contents of a file, use this command.

cat file1.txt

Here are a few flags you can use the command when viewing files. 

  • >: Using this flag along with a second file’s name will copy one file’s contents into the specified file. If used with a single file name, it’ll create the file. 
  • >>: Using this flag along with a second file’s name will append one file’s contents into the specified file. 
  • -n: Using this flag will show line numbers in the file. 
  • -s: Using this flag will suppress repeating empty lines. 
  • -T: Using this flag will show tab characters as ^I instead of a tab space. 
  • -e: Using this flag will show you the end of the line as $.
  • | more/ | less: These are parameters you can use with the cat command to show more or fewer lines when printing onto the terminal. 

Also read: How to use Journalctl to read Linux logs?


How to view multiple files in cat

To view multiple files at the same time, simply type their names one after another.

cat file1.txt file2.txt
How to use the Cat command in Linux? | Candid.Technology

If you want to append these files together in a third file, use this command. 

cat file1.txt file2.txt > Combofile.txt

If Combofile.txt exists, the command will overwrite the contents of that file with the combined contents of file1 and file2. Otherwise, it’ll create a new file with the combined contents of the two files. 


How to create files with cat command in Linux?

As mentioned above, you can create files with cat using the flag. 

cat > NewFile1.txt
How to use the Cat command in Linux? | Candid.Technology

After this, type the new file contents and press Ctrl + D to save your changes. Using the cat command to create smaller files is often more convenient than using other programs such as nano, Sublime Text or any other text editor. 

Also read: How to open task manager in Linux?

Yadullah Abidi

Yadullah Abidi

Yadullah is a Computer Science graduate who writes/edits/shoots/codes all things cybersecurity, gaming, and tech hardware. When he's not, he streams himself racing virtual cars. He's been writing and reporting on tech and cybersecurity with websites like Candid.Technology and MakeUseOf since 2018. You can contact him here: yadullahabidi@pm.me.

>