How to Reverse Text and Strings in Linux Terminal Using the rev Command

When manipulating text files and command-line streams in Linux, administrators frequently need to reverse strings. While the well-known tac command reverses files vertically line-by-line (from bottom to top), the native rev command reverses text horizontally line-by-line (from right to left).

Supplied as part of the standard util-linux package on virtually every Linux distribution, rev copies standard input or specified files to standard output, reversing the order of characters on each line. It is particularly powerful in shell pipelines for extracting trailing file extensions, parsing directory paths from right to left, or analysing structured data fields.


Basic Syntax and How rev Operates

The syntax for rev is exceptionally simple:

rev [file ...]

If no filename is passed, rev reads directly from standard input (stdin). When input lines are fed into rev, every line’s character sequence is inverted while newline terminations remain preserved.

Example: Comparing rev with Standard Text

echo "Linux Terminal Pipeline" | rev

The command outputs:

enilepiP lanimreT xuniL

Distinguishing rev from tac and Other Utilities

Understanding the difference between horizontal and vertical file inversion ensures you select the correct tool for your scripting pipeline:

Command Orientation Primary Function
rev Horizontal (Column/Character) Inverts characters from right to left across each line independently.
tac Vertical (Row/Line) Prints entire lines from bottom to top, preserving the left-to-right character order of each line.
cut Horizontal Slicing Extracts specified fields or byte positions starting from the beginning of a line.

Practical Shell Pipeline Applications

While reversing plain text strings is intuitive, the true utility of rev emerges when combined with other core Unix tools such as cut and awk.

1. Extracting File Extensions from Arbitrary File Paths

When working with deeply nested directories where paths contain variable numbers of slashes and dots, extracting the final file extension using cut -d. -f... is difficult because cut counts fields from left to right. By sandwiching cut between two rev commands, you can reliably slice from the right:

echo "/var/data/backups/archive.2026.tar.gz" | rev | cut -d. -f1 | rev

How this pipeline executes:

  1. rev flips the path to: zg.rat.6202.evihcra/spukcab/atad/rav/
  2. cut -d. -f1 extracts the first field before the first period: zg
  3. The second rev flips zg back to: gz

2. Isolating the Last Segment of a URL or Directory Path

Similarly, you can extract the final component of a URL or nested filesystem path without knowing the total depth of the tree:

echo "https://digitash.com/linux/terminal/tutorials/" | rev | cut -d/ -f2 | rev

This neatly returns tutorials, handling trailing slashes cleanly.

3. Checking for Palindromes in Wordlists

You can quickly audit a dictionary or list of terms to identify palindromic strings by comparing a file against its reversed counterpart using paste and awk:

rev words.txt | paste words.txt - | awk '$1 == $2 {print $1}'

Reversing Entire Multi-Line Files

To process an entire file and save the output to a new destination:

rev access.log > access_reversed.log

If you need to reverse a file both horizontally and vertically (complete two-dimensional inversion), pipe tac into rev:

tac input.txt | rev > inverted.txt

Because rev is lightweight, written in C, and operates natively with standard POSIX buffers, it executes efficiently even on multi-gigabyte log archives.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.