Important Cat Command Examples in Linux

The cat (short for "concatenate") command is one of the most frequently used command in Linux/Unix like operating systems. cat command allows us to create single or multiple files, view contain of file, concatenate files and redirect output in terminal or files.

Basically, Using it you can read, write, append content from one file to another file, concatenate files and redirect output to terminal.

Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform related Cat Command queries.

In this context, we shall look into how cat commands works along with examples in Linux.


General Syntax of cat Command

First of all you should know the syntax of cat command before going to use it. Below is the basic syntax of cat command:

$ cat [OPTIONS] [FILE_NAMES]

Where,

  • OPTIONS – You can specify the options http://man7.org/linux/man-pages/man1/cat.1.html. Get all available options using cat –help.
  • FILE_NAMES – Specify single or multiple file names.


How to Display Contents of File using cat command ?

The cat command is commonly used to read contents of files. Below example will show the contents of the /etc/timezone file:

$ cat /etc/timezone
Output
Etc/UTC


How to Display Contents of Multiple Files using cat command?

You can view content of multiple files using below command:

$ cat file1.txt file2.txt


How to Redirect Contents of File using cat command?

You can store output to a file instead of displaying to the screen. Below command will copy content from first file to second file:

$ cat file1.txt > file2.txt

Above command will create a file if second file does not exists. If it is available then it will overwrite the content of file.


How to Append Output with Redirection Operator using cat command?

You can append contents of one to another file without overwrite using below command:

$ cat file1.txt >> file2.txt


How to Redirect Standard Input with Redirection Operator using cat command?

When you want to take input from second file to terminal then you can use standard input '<' (less than) symbol as given below:

$ cat < file1.txt


How to Create a File using Cat Command ?

It is very easy to create a file using cat command. To create file using cat command you just have to use redirection operator and the name of the file. Hit the Enter key and now you can type whatever you wants to write. Save the file using Ctrl + D.

$ cat > file.txt

Here we are creating a file name with "file.txt". If file with that name exists then it will be overwritten otherwise it will create a new file.


How to Display Line Numbers using Cat Command ?

If you want to display file content with line number then you can use -n option with cat command as below:

$ cat -n file1.txt
Output
1    This is Line Number One
2    This is Second Line Number


How to Display Tab separated Lines in File using Cat Command ?

You can use -T argument to differentiate tabs and spaces:

$ cat -T /etc/hosts
Output
127.0.0.1^Ilocalhost
127.0.1.1^Ilocal.linuxapt
The TAB characters will be shown as ^I.


How to Display $ at the End of File using Cat Command ?

You can display line ending with '$' also can display if any gap between paragraphs. This options is useful to squeeze multiple lines in a single line.

$ cat -e /etc/lsb-release
Output
DISTRIB_ID=Ubuntu$
DISTRIB_RELEASE=20.04$
DISTRIB_CODENAME=FocalFossa$
DISTRIB_DESCRIPTION="Ubuntu 20.04 LTS"$


How to Concatenate Multiple Files in a Single File ?

When you pass more then one file name as arguments with cat command then contents of files will be concatenated. It will read file as given sequence and will append to display same.

In below command it will read the contents of file1.txt and file2.txt and display the result in the terminal:

$ cat file1.txt file2.txt

You can concatenate the contents of file file1.txt and file2.txt and store them to another file using redirection operator as below:

$ cat file1.txt file2.txt > file3.txt

If the file is not present then it will be created otherwise it will be overwritten.


[Need assistance in fixing Linux System errors? We can help you. ]

This article covers how to use the Linux cat command. Cat(concatenate) command is very frequently used in Linux. It reads data from the file and gives their content as output. It helps us to create, view, concatenate files.

If you want to add a bit of new text to an existing text file, you use the cat command to do it directly from the command line (instead of opening it in a text editor).

Type the cat command followed by the double output redirection symbol ( >> ) and the name of the file you want to add text to.

Related Posts