How to use cat and tac command in Linux

  • Post last modified:January 3, 2022
  • Reading time:4 mins read
  • Post author:
  • Post comments:0 Comments

What is cat command?

Cat command is used to read and print file content on a terminal screen. If you bifurcate concatenation, you will get a cat in between, which means you can combine multiple files at once. The cat command is not limited to reading files, and it can do more, let me show you how to leverage cat.

Create a new file using cat

Why use any text editor to write simple text content when you can use cat command with redirection symbol to make a new file content.

Open your terminal screen and pass the following command, and once you give the command, it will ask you to enter a file content.

$ cat > [FILE-NAME]
Cat: create a new file
Cat: create a new file
  • >:- Redirection symbol.
  • [FILE-NAME]:– Provide a file name to save it, if the file exists data will get override.

If you do not wish to enter more content, press Ctrl + D to save it.

The problem with > redirection will erase previous content to save a newer one. To prevent it, use >> redirection; see the below example for a clear view.

$ cat >> [FILE-NAME]
Cat with >> to save previous and newer content
Cat with >> to save previous and newer content

Read this article to clear your concept on redirection.

Read file content using cat

Above we have created a new file, but how to read it, It’s pretty simple, not much fuss, pass the file name along with cat command.

$ cat [FILE-NAME]
read file content using cat
Read file content using cat.

Read multiple file at once using cat

I have heard that the cat command can read multiple files simultaneously rather than opening individual files one by one. Technically that process is called concatenation.

$ cat [FILE-1] [FILE-2] [FILE-3]

I have created three groups for demo purposes that hold dummy data. Now you will see how to display concatenate data.

Read multiple files using cat
Read multiple files using cat.

Redirect cat output to new file

On top, you have seen how to concatenate multiple text files, but what of use when we cannot save for later use. In Linux, everything is possible.

This task can be achieved using redirection symbols. Let me show you how to do.

$ cat [FILE-NAME-1] [FILE-NAME-2] [FILE-NAME-3] > CONCATENATE-FILE
Redirect cat output to a new file
Redirect cat output to a new file

The problem with > the redirection will erase a previous content to save a newer one. To prevent its use >> redirection, see the below example for a clear view.

redirect to keep previous and more recent content
redirect to keep previous and more recent content

Create number line using cat

This feature is quite helpful to generate number lines or count numbers, even its count blank whitespaces.

To test this, pass the below command with the -n parameter to create numbers.

$ cat -n [FILE-NAME]
Create number line using cat
Create number line using cat

What is tac command?

A tac command is similar to a cat command, but the difference is data will get printed in reverse order. tac command is capable of concatenate, regex and separator.

tac command syntax

tac [FILE-NAME]

Read file in reverse order

I use the tac over tail command for the short files. Let me show you how it works.

tac groupData
tac command to reverse order
tac command to reverse order

Seprate the content

This is quite useful for separating content you don’t want to display. Previously I have created multiple files. From that, I don’t want to have group2 data.

To perform this, pass the below command with -s which stand for separator.

$ tac -s "group2" group1 group3
Separate the content
Separate the content

Difference between cat & tac

The significant difference between cat and tac is cat command can read in up & down manner, and tac read in reverse order apart from that cat can generate number line, read unprintable character, a blank line which all is missing in tac command, however, tac command has the support of regex, separator which can be helpful in bash scripting.

You cannot compare both based on reading ability. Both are developed for different use, a cat is abused much, and tac is suspense for many users.

Wrap-up

I have tried to cover all cat & tac command options, which we can use regularly. I have left tac command with regex, which is out of the scope of this article.

Are you guys interested in how we can use regex in tac command? Please let me know in the comment section.

Leave a Reply