Cat command in Linux with examples

Cat(concatenate ) command is very frequently used in linux.It reads data from file and give their content as output.It helps us to create,view,concatenate files.So let us see some frequently used cat commands.

1) To view a single file
Command:

$cat filename
Output

It will show content of given filename
2) To view multiple files
Command:

$cat file1 file2
Output

This will show content of file1 and file2.
3) To view contents of a file preceding with line numbers.
Command:

$cat -n filename
Output

It will show content with line number
example:-cat-n  geeks.txt
1)This is geeks
2)A unique array

4) Create a file 
Command:

$ cat >newfile
Output

Will create and a file named newfile
5) Copy the contents of one file to another file.
Command:

$cat [filename-whose-contents-is-to-be-copied] > [destination-filename]
Output

The content will be copied in destination file
6) Cat command can suppress repeated empty lines in output
Command:

$cat -s geeks.txt
Output

Will suppress repeated empty lines in output
7) Cat command can append the contents of one file to the end of another file.
Command:

$cat file1 >> file2
Output





Will append the contents of one file to the end of another file
8) Cat command can display content in reverse order using tac command.
Command:

 $tac filename
Output

Will display content in reverse order 
9) Cat command can highlight the end of line.
Command:

$cat -E "filename"
Output

Will highlight the end of line
10) If you want to use the -v, -E and -T option together, then instead of writing -vET in the command, you can just use the -A command line option.
Command

$cat -A  "filename"
This article is contributed by Pranav. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.


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

The cat utility expressions take the following form:


cat [OPTIONS] [FILE_NAMES]
Copy
OPTIONS - cat options. Use cat --help to view all available options.
FILE_NAMES - Zero or more file names.
Displaying File Contents with Cat
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 in the terminal:


cat /etc/issue
Copy

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 :

cat file1.txt > file2.txt
Copy
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 :

cat file1.txt >> file2.txt
Copy
Same as before, if the file is not present it will be created.


Print Line Numbers
To display contents of a file with line numbers use the -n argument:

cat -n /etc/lsb-release
Copy
1 DISTRIB_ID=Ubuntu
2 DISTRIB_RELEASE=18.04
3 DISTRIB_CODENAME=bionic
4 DISTRIB_DESCRIPTION="Ubuntu 18.04.1 LTS"
Copy
Suppress Repeated Empty Lines
Use the -s argument to omit the repeated empty output lines:

cat -s file.txt
Copy
Display TAB characters
Use the -T argument to visually distinguish between tabs and spaces.

cat -T /etc/hosts
Copy
127.0.0.1^Ilocalhost
127.0.1.1^Iubuntu1804.localdomain
Copy
The TAB characters will be displayed as ^I.


Display End of Lines
To display the invisible line ending character use the -e argument:

cat -e /etc/lsb-release
Copy
DISTRIB_ID=Ubuntu$
DISTRIB_RELEASE=18.04$
DISTRIB_CODENAME=bionic$
DISTRIB_DESCRIPTION="Ubuntu 18.04.1 LTS"$
Copy
The Line endings will be displayed as $.

Concatenating Files with Cat
When passing two or more file names as arguments to the cat command the files contents will be concatenated. Cat reads the files in the sequence given in its arguments and displays the files 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:

cat file1.txt file2.txt
Copy
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 :


cat file1.txt file2.txt > combinedfile.txt
Copy
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 file3.txt to use the (>>) operator:

cat file1.txt file2.txt >> file3.txt
Copy
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 with Cat
When creating a small file it is much easier to use cat instead of using Vim, Sublime Text, Visual Studio Code or any other 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 the CRTL+D to save the files.

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

cat > file1.txt
Copy

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

cat > file1.txt
Copy
If a file named file1.txt is present it will be overwritten. Use the (‘>>’) operator to append the output to an existing file.