tac command

The tac command shows the output of cat command in reverse manner hence the name of this command is also in reverse form that is tac . As we know cat command is used to see the content of file,creating the new file and concatenating the two file also (If you do not know soon my next write up is on this).
When we use cat command you can read the content in correct format means left to right reading and moving towards downwards.

As we know the cat command is one the basic Linux command. We have covered this command in our post – Linux Basic Commands For Every Beginner.

How To Use tac Command

It is very easy to use the tac command.

Syntax

tac [options] file...

-b, –before : It attach the separator before (not after).
-r, –regex : It interpret the separator as a regex(regular expression).
-s, –separator=STRING: It use STRING as the separator instead of newline.
–help: It displays the help.
–version: It shows the version of the tac command.

Example 1 : tac single file with no option

In this example, we are using only one file with tac command. It is very simple way to use this command.

See in given below screenshot, the output of tac command which is reverse of cat command out.

tac

Example 2: tac multiple files with no options

In this example, we are using two files.

user1@server01:~$ cat file1
red
blue
green
user1@server01:~$ cat file2
yellow
black
white
user1@server01:~$ tac file1 file2
green
blue
red
white
black
yellow
user1@server01:~$

Example 3: tac command with -b option

In this example, we will use -b option means it will the separator before not after. Notice the file1 and file2 order.

user1@server01:~$ tac file1
green
blue
red
user1@server01:~$ tac file2
white
black
yellow
user1@server01:~$ tac -b file2 file1


white
blackyellow

green
bluereduser1@server01:~$

tac command with option -r

In this example, tac command interpret the separator as a regex. Here the separator is contents of file2.

user1@server01:~$ tac file1
green
blue
red
user1@server01:~$ tac file2
white
black
yellow
user1@server01:~$ tac -r file2 file1
white
black
yellow
green
blue
red
user1@server01:~$

tac command with option -s

In this example, we are using the -s option. Notice it use STRING as the separator instead of newline. Here, we have used the STRING values “green” and “blue”.

user1@server01:~$ tac file1
green
blue
red
user1@server01:~$ tac -s "green" file1

red
blue
greenuser1@server01:~$ tac -s "blue" file1

green
red
blueuser1@server01:~$

Read Some More Articles

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.