Linux tac Command

Linux Command Reference


1. tac Command Overview

The tac command is a simple but practical text processing tool in Linux systems. Its functionality is the opposite of the common cat command - displaying file contents inreverse line orderdisplay.

1.1 Origin of the Command Name

tac is actually cat spelled backwards, just as its function is the reverse operation of the cat command:

  • cat(concatenate): display file contents in forward order
  • tac: display file contents in reverse order

1.2 Basic Functionality

The main features of the tac command include:

  • Display file contents in reverse line order
  • Supports multiple file inputs
  • Can be used with pipes to process the output of other commands

2. tac Command Syntax and Parameters

2.1 Basic Syntax

tac [选项]... [文件]...

2.2 Common Option Parameters

Option Description
-bor--before Place the delimiter at the beginning of the line instead of the end
-ror--regex Treat the delimiter as a regular expression
-sor--separator=STRING Use the specified string as the delimiter (default is newline)
--help Display help information
--version Display version information

3. tac Command Usage Examples

3.1 Basic Usage: Display File in Reverse

Example

# Create a sample file
echo -e "First line\nSecond line\nThird line" > example.txt

# Use tac to display in reverse
tac example.txt

Output result:

Third row
Second row
First row

3.2 Using Custom Delimiters

Example

# Create content separated by commas
echo "apple, banana, orange" > fruits.txt

# Use comma as the delimiter to display in reverse
tac -s ',' fruits.txt

Output result:

Orange, banana, apple

3.3 Processing Multiple Files

Example

echo -e "A\nB\nC" > file1.txt
echo -e "1\n2\n3" > file2.txt

tac file1.txt file2.txt

Output result:

C
B
A
3
2
1

3.4 Using with Pipes

Example

# Display the ls command output in reverse
ls | tac

# Display grep results in reverse
grep "error" log.txt | tac

4. How the tac Command Works

4.1 Processing Flow

Example

graph TD
A[Read input file] --> B[Store content line by line]
B --> C[Reverse line order]
C --> D[Output result]

4.2 Comparison with Related Commands

Command Function Feature
cat Display file contents in forward order Default output, does not modify order
tac Display file contents in reverse order Reverse line order
rev Display each line's characters in reverse Reverse the character order of each line
tail Display the end of the file Can specify the number of lines, but does not reverse the order

5. Practical Application Scenarios of the tac Command

5.1 Viewing Latest Logs

When log files are appended in chronological order, using tac lets you quickly view the latest records:

Example

tac /var/log/syslog | less

5.2 Processing Time Series Data

For data recorded in chronological order, viewing in reverse is more intuitive:

Example

tac temperature_records.csv | head -n 10

5.3 Working with Sort Commands

Sort first, then display in reverse:

Example

sort data.txt | tac

5.4 Special Handling in Scripts

When lines need to be processed in reverse order in certain scripts:

Example

for line in $(tac config.list); do
    echo "Processing: $line"
done

6. Notes and Common Issues

6.1 Performance Considerations

  • tac needs to read the entire file into memory, which can consume a lot of memory when processing large files
  • For very large files, consider usingtail -ras an alternative (supported on some systems)

6.2 Special Character Handling

  • Lines containing special characters may affect the display
  • When using-roption, the regular expression needs to be properly escaped

6.3 Common Errors

Example

# Error: options and file order reversed
tac example.txt -s ','  # Incorrect syntax

# Correct:
tac -s ',' example.txt

Linux Command Reference

Other Extensions