How to Reverse Order of Lines in a File in Linux

As a Linux user, you often need to reverse the order of lines in a file for various purposes like reading log files from most recent entries first, processing data in reverse chronological order, or preparing files for specific operations. This article explores several Linux commands that can accomplish this task efficiently.

Using tac Command to Reverse Order of Lines

The tac command (which is "cat" spelled backwards) is the simplest and most direct way to reverse line order in a file. It reads the file from end to beginning and prints each line in reverse order.

tac file.txt

To save the reversed output to a new file ?

tac file.txt > reversed_file.txt

Example with Sample File

Consider a file sample.txt with the following content ?

Line 1
Line 2  
Line 3
Line 4

Running tac sample.txt produces ?

Line 4
Line 3
Line 2
Line 1

Using sed Command to Reverse Order of Lines

The sed (stream editor) command can reverse line order using pattern manipulation. This approach is more complex but offers greater control over text processing.

sed '1!G;h;$!d' file.txt

How it works ? This sed script uses three commands: 1!G appends hold space to pattern space (except for first line), h copies pattern space to hold space, and $!d deletes pattern space (except for last line).

Alternative sed Approach

A more readable sed command with explicit line numbering ?

sed -n '1!G;h;$p' file.txt

The -n option suppresses automatic printing, and $p prints only the final result.

Using awk Command to Reverse Order of Lines

The awk command provides a programmatic approach using arrays to store and reverse lines.

awk '{a[i++]=$0} END {for (j=i-1; j>=0;) print a[j--]}' file.txt

How it works ? This awk script stores each line in array a[] with incrementing index i, then in the END block, it prints array elements in reverse order using a decrementing loop.

Comparison of Methods

Command Simplicity Performance Memory Usage Best For
tac Very High Excellent Low Simple line reversal
sed Medium Good Medium Text processing pipelines
awk Low Good High Complex text manipulation

Advanced Options

Handling Large Files

For very large files, consider using tac with buffering options ?

tac -b file.txt > reversed_file.txt

Processing Multiple Files

To reverse lines in multiple files simultaneously ?

tac file1.txt file2.txt file3.txt

Using with Pipes

Combine with other commands in a pipeline ?

cat logfile.txt | grep "ERROR" | tac

Common Use Cases

  • Log file analysis ? Reading newest entries first from system logs

  • Data processing ? Reversing chronologically ordered data

  • File preparation ? Preparing files for reverse processing algorithms

  • Debugging ? Examining recent changes or events first

Conclusion

The tac command is the most efficient and straightforward method for reversing line order in files. For complex text processing needs, sed and awk provide more flexibility but with increased complexity. Choose the method that best fits your specific requirements and performance needs.

Updated on: 2026-03-17T09:01:38+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements