Skip to main content
ubuntuask.com

Back to all posts

How to Read A File In Reverse Order In Linux?

Published on
5 min read
How to Read A File In Reverse Order In Linux? image

Best Tools to Read Files in Reverse Order on Linux to Buy in July 2026

1 USB 3.0 SD Card Reader for PC, Laptop, Mac, Windows, Linux, Chrome, SDXC, SDHC, SD, MMC, RS-MMC, Micro SDXC Micro SD, Micro SDHC Card and UHS-I Cards - No SD Card Included. (USB 3.0 SD -Black2)

USB 3.0 SD Card Reader for PC, Laptop, Mac, Windows, Linux, Chrome, SDXC, SDHC, SD, MMC, RS-MMC, Micro SDXC Micro SD, Micro SDHC Card and UHS-I Cards - No SD Card Included. (USB 3.0 SD -Black2)

  • DUAL SLOTS DESIGN: EASILY SWITCH BETWEEN SD AND MICRO SD CARDS!
  • LIGHTNING FAST 5GBPS SPEEDS: SAVE TIME TRANSFERRING LARGE FILES QUICKLY!
  • WIDE COMPATIBILITY: WORKS SEAMLESSLY WITH MULTIPLE OPERATING SYSTEMS!
BUY & SAVE
$4.99
USB 3.0 SD Card Reader for PC, Laptop, Mac, Windows, Linux, Chrome, SDXC, SDHC, SD, MMC, RS-MMC, Micro SDXC Micro SD, Micro SDHC Card and UHS-I Cards - No SD Card Included. (USB 3.0 SD -Black2)
2 7 in 1 Memory Stick Pro Duo Card Reader, USB-C Multi Cards Reader for SD/TF/CF/MS/XD Cards, SD Card Reader for iPhone, Tablet, Laptop, Windows, Linux, Android, Camera, Game Track Camera

7 in 1 Memory Stick Pro Duo Card Reader, USB-C Multi Cards Reader for SD/TF/CF/MS/XD Cards, SD Card Reader for iPhone, Tablet, Laptop, Windows, Linux, Android, Camera, Game Track Camera

  • SIMULTANEOUS TRANSFERS: CONNECT 5 CARDS AT ONCE FOR UNMATCHED EFFICIENCY.

  • LIGHTNING FAST SPEEDS: TRANSFER FILES UP TO 5GBPS-10X FASTER THAN USB 2.0!

  • UNIVERSAL COMPATIBILITY: WORKS WITH MULTIPLE DEVICES AND VARIOUS CARD FORMATS.

BUY & SAVE
$23.99
7 in 1 Memory Stick Pro Duo Card Reader, USB-C Multi Cards Reader for SD/TF/CF/MS/XD Cards, SD Card Reader for iPhone, Tablet, Laptop, Windows, Linux, Android, Camera, Game Track Camera
3 Amicool External USB-C USB 3.0 CD/DVD Drive Burner +/-RW, Ultra-Slim Portable DVD Player Writer Reader for Mac iMac MacBook, Windows 11/10/8 Linux, Laptop Desktop PC Computer Optical Drive

Amicool External USB-C USB 3.0 CD/DVD Drive Burner +/-RW, Ultra-Slim Portable DVD Player Writer Reader for Mac iMac MacBook, Windows 11/10/8 Linux, Laptop Desktop PC Computer Optical Drive

  • ULTRA-SLIM DESIGN: WEIGHS JUST 7.35 OZ FOR ULTIMATE PORTABILITY.

  • PLUG & PLAY COMPATIBILITY: WORKS SEAMLESSLY WITH ALL MAJOR OPERATING SYSTEMS.

  • FAST & RELIABLE: 24X CD AND 8X DVD BURNING FOR QUICK DATA MANAGEMENT.

BUY & SAVE
$29.99
Amicool External USB-C USB 3.0 CD/DVD Drive Burner +/-RW, Ultra-Slim Portable DVD Player Writer Reader for Mac iMac MacBook, Windows 11/10/8 Linux, Laptop Desktop PC Computer Optical Drive
4 SD Card Reader 7 in 1, Kxable USB 3.0 Memory Card Reader/Writer 5Gbps, for SD/Micro SD/MS/CF/MMC/XD/SDHC/SDXC Camera Memory Card, Reader for Mac OS, Windows, Linux- with 2 USB A/C to Micro 3.0 Cables

SD Card Reader 7 in 1, Kxable USB 3.0 Memory Card Reader/Writer 5Gbps, for SD/Micro SD/MS/CF/MMC/XD/SDHC/SDXC Camera Memory Card, Reader for Mac OS, Windows, Linux- with 2 USB A/C to Micro 3.0 Cables

  • VERSATILE 7-IN-1 READER: SUPPORTS MULTIPLE CARD TYPES FOR ALL USERS.
  • LIGHTNING FAST TRANSFERS: ACHIEVE 5GBPS SPEEDS FOR QUICK FILE ACCESS.
  • DURABLE & COMPACT DESIGN: HIGH-QUALITY MATERIALS FOR PORTABILITY & STYLE.
BUY & SAVE
$16.99
SD Card Reader 7 in 1, Kxable USB 3.0 Memory Card Reader/Writer 5Gbps, for SD/Micro SD/MS/CF/MMC/XD/SDHC/SDXC Camera Memory Card, Reader for Mac OS, Windows, Linux- with 2 USB A/C to Micro 3.0 Cables
5 Memory Stick Pro Duo Adapter USB C/Lightning, 4 in 2 SD/MicroSD/MS/xD Card Reader for Sony Cybershot SD Card, Memory Stick Pro Duo Reader for iPhone17/16/14/Macbook/iPad/PC/Laptop/Windows/Linux

Memory Stick Pro Duo Adapter USB C/Lightning, 4 in 2 SD/MicroSD/MS/xD Card Reader for Sony Cybershot SD Card, Memory Stick Pro Duo Reader for iPhone17/16/14/Macbook/iPad/PC/Laptop/Windows/Linux

  • UNIVERSAL CONNECTIVITY: DUAL PORTS FOR IPHONE, ANDROID, AND PC!

  • 4-IN-1 READER: SUPPORTS MEMORY STICK PRO DUO, XD, SD, MICROSD!

  • COMPACT & PORTABLE: PERFECT FOR PHOTOGRAPHERS AND ON-THE-GO ACCESS!

BUY & SAVE
$16.99
Memory Stick Pro Duo Adapter USB C/Lightning, 4 in 2 SD/MicroSD/MS/xD Card Reader for Sony Cybershot SD Card, Memory Stick Pro Duo Reader for iPhone17/16/14/Macbook/iPad/PC/Laptop/Windows/Linux
6 Hicober USB C to SD Card Reader, Micro SD Memory Card Reader, Type C to SD Card Reader Adapter 2TB Capacity for MacBook Camera Android Windows Linux and Other Type C Device-Blue

Hicober USB C to SD Card Reader, Micro SD Memory Card Reader, Type C to SD Card Reader Adapter 2TB Capacity for MacBook Camera Android Windows Linux and Other Type C Device-Blue

  • SHARE INSTANTLY: UPLOAD 2TB OF MEMORIES TO SOCIAL MEDIA IN SECONDS!

  • EFFORTLESS TRANSFER: ENJOY 5GBPS SPEED WITH NO APPS NEEDED-PLUG AND PLAY!

  • ULTIMATE DURABILITY: LIGHTWEIGHT, STURDY DESIGN PERFECT FOR ON-THE-GO USE!

BUY & SAVE
$12.91 $15.99
Save 19%
Hicober USB C to SD Card Reader, Micro SD Memory Card Reader, Type C to SD Card Reader Adapter 2TB Capacity for MacBook Camera Android Windows Linux and Other Type C Device-Blue
7 SABRENT SuperSpeed 2 Slot USB 3.0 Flash Memory Card Reader for Windows, Mac, Linux, and Certain Android Systems Supports SD, SDHC, SDXC, MMC/MicroSD, T Flash [Black] (CR-UMSS)

SABRENT SuperSpeed 2 Slot USB 3.0 Flash Memory Card Reader for Windows, Mac, Linux, and Certain Android Systems Supports SD, SDHC, SDXC, MMC/MicroSD, T Flash [Black] (CR-UMSS)

  • EXPERIENCE LIGHTNING-FAST DATA TRANSFER WITH SUPERSPEED USB 3.0.
  • CONVENIENT USB-POWERED DESIGN – NO EXTRA POWER SUPPLY NEEDED!
  • EFFORTLESS SETUP: PLUG-N-PLAY WITH UNIVERSAL MEMORY CARD SUPPORT.
BUY & SAVE
$6.99
SABRENT SuperSpeed 2 Slot USB 3.0 Flash Memory Card Reader for Windows, Mac, Linux, and Certain Android Systems Supports SD, SDHC, SDXC, MMC/MicroSD, T Flash [Black] (CR-UMSS)
+
ONE MORE?

To read a file in reverse order in Linux, you can use the combination of some Linux commands. Here's one possible approach:

  1. Firstly, you can use the tac command, which is the reverse version of cat. It reads lines from a file and prints them in reverse order. For example, tac file.txt will output the contents of file.txt in reverse order.
  2. In case you want to view the file interactively or scroll through it, you can pipe the output of tac to the less command. This allows you to scroll up and down through the reversed file. For instance, tac file.txt | less will display file.txt in reverse order using the less pager.
  3. If you want to read the file in reverse order but view it all at once, you can redirect the output of tac to another file. This will save the reversed contents to a new file. For example, tac file.txt > reversed.txt will create a new file named reversed.txt with the contents of file.txt in reverse order.

These methods should help you read a file in reverse order in Linux using command-line utilities.

How to display the last line of a file using Linux commands?

To display the last line of a file using Linux commands, you can use the tail command with the -n option.

The syntax is as follows:

tail -n 1

where <filename> is the name of the file you want to display the last line of.

For example, to display the last line of a file named "example.txt", you would run:

tail -n 1 example.txt

This command will output the last line of the file on the terminal.

What is the syntax for removing duplicate lines from a file read in reverse order using uniq in Linux?

To remove duplicate lines from a file read in reverse order using uniq in Linux, you can combine the use of the tac command to reverse the file and the uniq command to remove duplicates. Here is the syntax:

tac <file_name> | uniq

Explanation:

  1. tac command is used to reverse the lines of a file.
  2. should be replaced with the name of the file you want to process.
  3. The output of tac is then piped (|) to the uniq command to remove duplicate lines.

Note: The uniq command requires that duplicate lines be adjacent to each other, so if your file has duplicated lines that are not consecutive, this method will not remove them.

How to reverse the order of lines in a file using sed command in Linux?

To reverse the order of lines in a file using the sed command in Linux, you can use the following command:

sed '1!G;h;$!d' filename

Here's how it works:

  • The sed command is used to manipulate and transform text.
  • The expression '1!G;h;$!d' is the sed script that will be executed.
  • 1!G appends each line to the "hold space" (a temporary buffer), except for the first line.
  • h copies the contents of the "pattern space" (current line) to the "hold space".
  • $!d deletes all lines except the last line.
  • filename should be replaced with the name of the file you want to reverse the lines in.

This command will print out the reversed lines of the file on the console. If you want to save the result in a new file, you can use the shell redirection operator (>) like so:

sed '1!G;h;$!d' filename > newfile

This will save the reversed lines in a new file called newfile.

How can I read a file in reverse order and replace a specific word with another using sed command in Linux?

To read a file in reverse order and replace a specific word with another using the sed command in Linux, you can make use of the tac command to reverse the order of lines and then pipe the output to the sed command for performing the replacement.

Here's an example command:

tac filename.txt | sed -e 's/old_word/new_word/g' > output.txt

Explanation:

  1. tac filename.txt: This command reads the contents of filename.txt in reverse order. The output is then passed to the next command through the pipe.
  2. sed -e 's/old_word/new_word/g': This command replaces all occurrences of old_word with new_word in the input stream. Modify old_word and new_word with your specific words.
  3. > output.txt: This redirection operator redirects the output of the above command to output.txt file. You can change output.txt to a different filename if desired.

After running the above command, the file filename.txt will be read in reverse order, and all occurrences of old_word will be replaced with new_word. The modified content will be saved in the output.txt file.

How to read a file in reverse order and redirect the output to another file?

To read a file in reverse order and redirect the output to another file, you can follow these steps:

  1. Open the input file using a file object in read mode.

input_file = open('input_file.txt', 'r')

  1. Read the contents of the input file and store them in a list.

file_lines = input_file.readlines()

  1. Reverse the order of the list.

reversed_lines = file_lines[::-1]

  1. Open the output file using another file object in write mode.

output_file = open('output_file.txt', 'w')

  1. Write the reversed lines to the output file.

output_file.writelines(reversed_lines)

  1. Close both the input and output files.

input_file.close() output_file.close()

Here's the complete code snippet:

input_file = open('input_file.txt', 'r') file_lines = input_file.readlines() reversed_lines = file_lines[::-1]

output_file = open('output_file.txt', 'w') output_file.writelines(reversed_lines)

input_file.close() output_file.close()

Make sure to replace 'input_file.txt' with the actual path or filename of your input file, and 'output_file.txt' with the desired path or filename of the output file.