How to Use ‘cat’ and ‘tac’ Commands with Examples in Linux

LinuxOperating SystemFile System

Cat command is a well known Unix utility that reads files sequentially. Writing them to conventional output. The name is derived from its function for concatenating and listing the documents. Tac (that is “cat” backwards) concatenates every record to traditional output much like the cat command. However in opposite: line-by means of-line, printing the last line first. This article explains about “How to use ‘cat’ and ‘tac’ commands with examples”.

The basic example of cat command should be like this –

$ cat text.txt

The above command is to read files and display them to stdout, meaning to display the content of files on your terminal. The sample output should be like this –

I love tutorialspoint.com

Another usage of the cat command is to study or combine a couple of files together and ship the output to a monitor as shown below –

$ cat text.txt text2.txt text3.txt

The sample output should be like this –

I love tutorialspoint.com
I love codingground in tutorialspoint.com
I love send18.com

This command can also be used to concatenate (join) multiple files into one single file using the “>” Linux as shown below –

$ cat text.txt text2.txt>text3.txt

The above command joins text.txt and text2.txt and concatenates the data into a different file- text3.txt file. The sample output should be like this –

I love tutorialspoint.com I love codingground in tutorialspoint.com

The cat command is also used to copy the content from one file to an other new file. The new (updated) file can be renamed as arbitrary.

For example, copy the following file from the current location to /tmp/ directory as shown below –

/Desktop$ cat text.txt > /tmp/file.txt

The above command copies text.txt file data to file.txt file. The sample output should be like this –

~/Desktop$ cat text.txt > /tmp/file.txt
~/Desktop$ cd /tmp/
/tmp$ cat file.txt
I love tutorialspoint.com

A less usage of the cat command is to create a new file with the below command –

~/Desktop$ cat >abc.txt

The sample output should be like this –

/Desktop$ ls
abc.txt text3.txt tumblr_static_ic_mysoundcloud512full.png
text2.txt text.txt web-1024x490.png

Usage of Tac Command in Linux

Tac is almost the reverse model of cat command (additionally spelled backwards) which prints every line of a report beginning from the lowest line and completing on the top line in your gadget trendy output. Sample example should be like this –

$ tac text3.txt

The sample output should be like this –

I love codingground in tutorialspoint.com
I love tutorialspoint.com

The most important usage of tac command is that, it can provide great help in order to debug log files, even reversing the chronological order of log contents.

The sample example should be like this –

$ tac /var/log/dpkg.log

The sample output should be like this –

2016-12-12 11:48:30 startup packages configure
2016-12-12 11:48:30 status installed sqlitebrowser:amd64 3.9.0ubuntu1-0~1225~201608241849~ubuntu16.04.1
2016-12-12 11:48:30 status half-configured sqlitebrowser:amd64 3.9.0ubuntu1-0~1225~201608241849~ubuntu16.04.1
2016-12-12 11:48:29 status unpacked sqlitebrowser:amd64 3.9.0ubuntu1-0~1225~201608241849~ubuntu16.04.1
2016-12-12 11:48:29 configure sqlitebrowser:amd64 3.9.0ubuntu1-0~1225~201608241849~ubuntu16.04.1
2016-12-12 11:48:29 startup packages configure
2016-12-12 11:48:29 status installed mime-support:all 3.59ubuntu1
2016-12-12 11:48:29 status half-configured mime-support:all 3.59ubuntu1
2016-12-12 11:48:29 trigproc mime-support:all 3.59ubuntu1
2016-12-12 11:48:29 status installed bamfdaemon:amd64 0.5.3~bzr0+16.04.20160701-0ubuntu1
2016-12-12 11:48:29 status half-configured bamfdaemon:amd64 0.5.3~bzr0+16.04.20160701-0ubuntu1

.....................................................................................

After this article, you will be able to understand – How to Use ‘cat’ and ‘tac’ Commands with Examples in Linux, we will come up with more Linux based tricks and tips. Keep reading!

raja
Published on 20-Jan-2020 16:52:24
Advertisements