• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • VCS
  • Interview Questions
  • Database
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

tac Command Examples in Linux

by Deepika

Tac is a command-line utility tool that is used for displaying and concatenating files with lines in reverse order. It is available on Unix-like operating systems, such as Linux and macOS, and is included in the GNU coreutils package.

The tac command is the reverse of cat, which concatenates files and displays their contents in the order they appear. tac instead reads each file in reverse order and displays the content of the file with the lines reversed. This can be useful in many situations, such as when you want to view the last few lines of a log file or when you want to reverse the order of a file.

The tac command can be used with multiple files as well. If more than one file is given, tac concatenates the files in reverse order and displays the result. For example, the command tac file1.txt file2.txt will display the contents of file2.txt followed by file1.txt, with the lines in each file reversed.

Here is an example of how to use the tac command:

$ cat file.txt
line 1
line 2
line 3
line 4
$ tac file.txt
line 4
line 3
line 2
line 1

As shown in the example above, tac reads the lines of the file in reverse order and displays them in that order.

tac Command Examples

1. Concatenate specific files in reversed order:

# tac path/to/file1 path/to/file2 ...

2. Display `stdin` in reversed order:

# cat path/to/file | tac

3. Use a specific [s]eparator:

# tac -s separator path/to/file1 path/to/file2 ...

4. Use a specific [r]egex as a [s]eparator:

# tac -r -s separator path/to/file1 path/to/file2 ...

5. Use a separator [b]efore each file:

# tac -b path/to/file1 path/to/file2 ...

Summary

In summary, tac is a useful command-line tool for displaying the contents of files with lines in reverse order. It can be used with multiple files and is a convenient way to quickly view the last few lines of a file or reverse the order of a file. More information on tac and its usage can be found in the GNU coreutils documentation.

Filed Under: Linux

Some more articles you might also be interested in …

  1. openfortivpn Command Examples in Linux
  2. Linux Command line Basics – Working with Files and Directories
  3. uname: command not found
  4. blackfire: A command-line profiling tool for PHP (Command Examples)
  5. Ngrok how to share localhost to world
  6. fail2ban-client: command not found
  7. Understanding Variables in Bash Shell Under Linux
  8. lastlog: command not found
  9. How to Install and Configure Kerberos in CentOS/RHEL 7
  10. Comparing NET-TOOLS V/s IPROUTE Package Commands (ip Vs ifconfig command comparison)

You May Also Like

Primary Sidebar

Recent Posts

  • glab Command Examples
  • “glab repo” Command Examples
  • “glab release” Command Examples
  • “glab pipeline” Command Examples

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright