Cat Command Complete Tutorial

Cat command in Linux/Unix with Examples

In this article, we are discussing about Cat command.

As you all know, while working in Unix and Linux systems. You need to work with lot of files. For this purpose, You need to view the content of the files. Creating files having some desired content. Cat command can be used 

What you will learn after reading the article

  • Cat Command Syntax
  • What does Cat command mean
  • Usage of cat command 
  • Parameters can be used with Cat command?
  • Cat command examples in Unix and linux
  • Cat command options in brief.

Cat command : syntax

cat command always used in lowercase letters in unix and linux systems. It works in all versions of unix and linux. It is one of the most widely used command.

Syntax: Cat options file_name

What does Cat Command mean?

Cat Command: Concatenate files and print on the standard output

It is used in creating files, viewing the content of files, concatenating files data and various other uses as well. 

What is usage of ls command and what parameters can be used with it?

Cat command used to view and create the file . Also you can search the pattern of content  of these files using with another command.

You can use cat command with grep command using pipe operator.

Command: Cat filename.txt | grep “pattern”

Usage of Cat Commands:

  • Display Content of file.
  • Copy the content from one file to another file.
  • Concatenate multiple files and display the result in terminal.

There are so many other uses of cat command, which we will discuss in next section in detail. 

Cat command examples in unix and linux systems

cat command Example 1:

How to display or print content of the file ?

Command: cat file_name

You need to run the cat command without parameters, just need to provide the file name.

In the below example, we run the

cat log.txt

After executing the command, It print all the data which file contains in it. 

Cat Command Example

cat command Example 2:

How to display or print content of multiple file ?

Command: cat file_name1 file_name2 

You need to run the cat command without parameters, just need to provide the file names.

Here you can provide multiple files.

In the below example, we run the

cat log1.txt log2.txt

It concatenate the both file’s data and print in terminal.

cat command Example 3:

How to create a file using cat command ?

Command: cat > file_name

Data which needs to put in a file

control + d

You need to use the operator > file name(for which name you want to create),insert the data or content in a file.

For exiting, use control + d.

cat > think.txt

thinkheights is a blogging site.

After executing the command, It created the file having content “thinkheights is a blogging site.”

cat command example

cat command Example 4:

How to display line numbers in a file ?

Command: cat -n  file_name

You need to use the -n parameters along with the file name to get the lines numbers along with data.

cat -n log.txt

Output is like that:

1 Unix is operating system.
2 Unix is most secure operating system.
3 unix is used in banking, telecom and in financial sector.
4 unix is free operating system.
5 unix is powerful.
6 unix is easy to use.
7 unix is multiuser operating system.

See below command line output:

Cat command example

cat command Example 5:

How to see where the line ends in a file?

When you are working in a big files, You may got confuse where the line end or start.

For this you can use, -e parameter, this will put $ at the end of each line.

Command: cat -e  file_name

$ cat -e log.txt
Unix is operating system.$
Unix is most secure operating system.$
unix is used in banking, telecom and in financial sector.$
unix is free operating system.$
unix is powerful.$
unix is easy to use.$
unix is multiuser operating system.$

Cat Command example

cat command Example 6:

How to copy the content from one file to another file using cat command?

With cat command, you can create the file and copy whole data in it.

Command: cat file_name > new_file_name

Example:

$ cat log.txt > new_file.txt

cat command Example 7:

How to view the content of file in reverse order?

With cat command, You can view the file from first to last line.

For viewing in opposite way, You should use tac for this 

Command: tac file_name 

Example:

$ tac log.txt  

cat command example

cat command Example 8:

How to highlight te tab character in a file through cat command?

With cat command, You can highlight the tab character with character ^I

For this, You should use -T parameter

Command: cat -T file_name 

Example:

$ cat -T log.txt
Unix^I is operating system.

Cat command example in unix

Cat Command Example 9:

How to suppress empty lines from file using cat command?

In a file, If having multiple empty lines, and if you want to suppress it. you sould use -s parameter.

Command: cat -s file_name 

$ cat -s log.txt
Unix is operating system.

Unix is most secure operating system.

Here the empty lines are higher, But it suppresses all empty lines while viewing 

Cat Command Example 10:

How to highlight end of lines and tabs in file using cat command?

In this  article, You already learn about highlighting tab and end of lines seperately using parameter -E  and -T.

You can use -ET or -A

Command: cat -A file_name 

cat -ET file_name

Example:

$ cat -A log.txt
Unix^I is operating system.$
Unix is most secure operating system.$
unix is used in banking, telecom and in financial sector.$

See below Image

Cat command parameters Summary

CommandUsage
Cat filenameview contents of a file 
cat filename1 fimename2view multiple files using cat
cat -n [filename] view contents of file with line numbers
cat > [name-of-new-file] create file using cat command
cat file1.txt > file2.txtcopy the contents of one file to another file
cat -E filenameline ends highlighted 
cat -s filenamesuppress repeated empty lines
cat  -T filename display tab characters as ^I
tac filenameView the Contents in Reverse Order
cat -A filenameuse -E and -T together

Leave a Comment

Your email address will not be published. Required fields are marked *