Table of Contents
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]
- >:- 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]
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 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.
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
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.
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]
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
Read file in reverse order
I use the tac over tail command for the short files. Let me show you how it works.
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
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.
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.
A man with a tech effusive, who has explored some of the amazing technology stuff and exploring more, While moving towards, I had a chance to work on Android Development, Linux, AWS, DevOps with several Open source tools.
One of my life mottos “Always be lifelong Students.”