tac

file managementLinux/Unix
The tac command is one of the most frequently used commands in Linux/Unix-like operating systems. tac Concatenate and print files in reverse (opposite of cat)

Quick Reference

Command Name:

tac

Category:

file management

Platform:

Linux/Unix

Basic Usage:

tac [options] [arguments]

Common Use Cases

    Syntax

    tac [OPTION]... [FILE]...

    Options

    Option Description
    -b, --before Attach the separator before instead of after
    -r, --regex Interpret the separator as a regular expression
    -s, --separator=STRING Use STRING as the separator instead of newline
    --help Display help and exit
    --version Output version information and exit

    Examples

    How to Use These Examples

    The examples below show common ways to use the tac command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    # Display file contents in reverse order (last line first) tac file.txt
    # Display multiple files in reverse order tac file1.txt file2.txt
    # Use standard input when no file is specified echo -e "Line 1\nLine 2\nLine 3" | tac
    # Advanced Examples Advanced
    # Reverse order of paragraphs (separated by blank lines) tac -b -r -s '^$' document.txt
    # Use a custom separator instead of newline tac -s ";" data.csv # Process a binary file (less common use case) tac -b binary.dat # Redirect output to a new file tac original.txt > reversed.txt # Use tac with grep to find lines before a match (in original file order) tac log.txt | grep -A 5 "ERROR" | tac # Combine with other commands tac file.txt | head -n 10 # Show the last 10 lines (like tail -n 10) # Read from stdin, show in reverse order
    cat file.txt | tac
    # Reverse lines and pipe to another command tac log.txt | grep "ERROR" # Show all .log files in reverse tac *.log # Use with process substitution tac <(echo -e "line1\nline2\nline3")

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The `tac` command in Linux is essentially the reverse of the `cat` command, as its name suggests (tac is cat spelled backwards). While `cat` concatenates and displays files starting from the first line to the last, `tac` displays the contents from the last line to the first. This command is particularly useful when you need to review log files or any text where the most recent entries are at the end. Instead of using `cat` and then scrolling to the bottom, you can use `tac` to immediately see the most recent entries first. By default, `tac` uses newline characters as separators between records. However, it offers flexibility through several options: 1. The `-s` or `--separator` option allows you to specify a different separator than the default newline. This is useful for files with custom record separators like CSV files or custom log formats. 2. With the `-b` or `--before` option, `tac` attaches the separator before each record instead of after it. This changes how the separation works and can be useful in specific parsing scenarios. 3. The `-r` or `--regex` option interprets the separator as a regular expression, providing more advanced pattern matching for determining where records begin and end. A common use case for `tac` is in log analysis, where you might want to start reviewing from the most recent entries. Another creative use is to combine it with other commands like `grep` to find specific patterns but display them in reverse chronological order. The `tac` command works with both files and standard input, making it versatile for command pipelines. For example, you can pipe the output of another command to `tac` to reverse its order. While `tac` is conceptually simple, it provides functionality that isn't easily replicated with other standard tools, making it a valuable addition to a Linux user's command-line toolkit, especially for text processing and log analysis tasks.

    Related Commands

    These commands are frequently used alongside tac or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the tac command works in different scenarios.

    $ tac
    View All Commands