Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux tac Command

Linux tac Command

Linux Command Reference Linux Command Reference


I. Overview of the tac Command

The tac command is a simple but practical text-processing tool in Linux; its function is the opposite of the common cat command - it displays the file contentin reverse line order.

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:

1.2 Basic Functions

The main functions of the tac command include:


II. tac Command Syntax and Parameters

2.1 Basic Syntax



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

2.2 Common Option Parameters

Option Description
-b or --before Place the separator at the beginning of a line rather than the end
-r or --regex Treat the separator as a regular expression
-s or --separator=STRING Use the specified string as the separator (default is newline)
--help Display help information
--version Display version information

III. tac Command Usage Examples

3.1 Basic Usage: Displaying a File in Reverse

Example

Create a sample file

echo

-e

"Line 1\nLine 2\nLine 3"

example.txt

Use tac to display in reverse

tac

example.txt

Output result:

第三行

第二行

第一行

3.2 Using a Custom Separator

Example

Create comma-separated content

echo

"apple,banana,orange"

fruits.txt

Display in reverse using a comma as the separator

tac

-s

','

fruits.txt

Output result:

橙子,香蕉,苹果

3.3 Handling 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 output of the ls command in reverse

ls

|

tac

Display grep results in reverse

grep

"error"

log.txt

|

tac


IV. 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 content in forward order Default output, no order change
tac Display file content 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 a file Can specify the number of lines, but does not reverse the order

V. Practical Applications of tac

5.1 Viewing the 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 it in reverse is more intuitive:

Example

tac

temperature_records.csv

|

head

-n

10

5.3 Combining with Sorting Commands

Sort first, then display in reverse:

Example

sort

data.txt

|

tac

5.4 Special Handling in Scripts

When some scripts need to process lines in reverse order:

Example

for

line

in

$

(

tac

config.list

)

;

do

   

echo

"Processing: $line"

done


VI. Notes and Common Issues

6.1 Performance Considerations

6.2 Handling Special Characters

6.3 Common Mistakes

Example

Wrong: option and file order reversed

tac

example.txt

-s

','

 

Incorrect Writing

Correct:

tac

-s

','

example.txt


Linux Command Reference Linux Command Reference