Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Use 'cat' and 'tac' Commands with Examples in Linux
The cat command is a fundamental Linux utility that reads files sequentially and displays their contents to standard output. The name is derived from its function for concatenating and listing files. The tac command (which is "cat" spelled backwards) performs a similar function but displays file contents in reverse order, printing the last line first.
Basic cat Command Usage
The simplest usage of cat is to display file contents:
$ cat text.txt
I love tutorialspoint.com
This command reads the file and displays its content to stdout (standard output) on your terminal.
Concatenating Multiple Files
The cat command can display multiple files sequentially:
$ cat text.txt text2.txt text3.txt
I love tutorialspoint.com I love codingground in tutorialspoint.com I love send18.com
Combining Files with Redirection
You can concatenate multiple files into a single new file using the > redirection operator:
$ cat text.txt text2.txt > text3.txt
This command joins the contents of text.txt and text2.txt and writes them to text3.txt:
I love tutorialspoint.com I love codingground in tutorialspoint.com
Copying Files
The cat command can copy content from one file to another location:
$ cat text.txt > /tmp/file.txt $ cd /tmp/ $ cat file.txt
I love tutorialspoint.com
Creating New Files
You can create a new file using cat with redirection:
$ cat > abc.txt
After running this command, you can type content and press Ctrl+D to save and exit.
Using the tac Command
The tac command is the reverse version of cat, displaying file contents from bottom to top:
$ tac text3.txt
I love codingground in tutorialspoint.com I love tutorialspoint.com
Practical Use Case − Log File Analysis
The tac command is particularly useful for debugging log files, as it reverses the chronological order to show the most recent entries first:
$ tac /var/log/dpkg.log
2016-12-12 11:48:30 startup packages configure 2016-12-12 11:48:30 status installed sqlitebrowser:amd64 3.9.0ubuntu1 2016-12-12 11:48:30 status half-configured sqlitebrowser:amd64 3.9.0ubuntu1 2016-12-12 11:48:29 status unpacked sqlitebrowser:amd64 3.9.0ubuntu1 2016-12-12 11:48:29 configure sqlitebrowser:amd64 3.9.0ubuntu1 2016-12-12 11:48:29 startup packages configure ...
Key Differences
| Command | Output Order | Primary Use Case |
|---|---|---|
| cat | Top to bottom (original order) | Display, concatenate, create files |
| tac | Bottom to top (reversed order) | Log analysis, reverse chronological viewing |
Conclusion
The cat and tac commands are essential Linux utilities for file manipulation and viewing. While cat displays files in their original order and supports concatenation and file creation, tac provides reverse-order display that's particularly valuable for log file analysis and debugging tasks.
