How to Use the rev Command to Reverse Text in Linux

The Weird World of Linux Utilities

The Linux operating system is famous for providing hundreds of tiny, highly specific command-line tools that do exactly one thing perfectly. You use sort to alphabetize text, grep to search for text, and wc to count text. But buried deep within the Linux core utilities is a command that seems utterly useless at first glance: the rev (Reverse) command.

If you feed a sentence into the rev command, it will literally output the exact same sentence, but spelled completely backward. If you feed it “Hello World,” it outputs “dlroW olleH.”

Why on earth would a systems administrator ever need to spell words backward? While it sounds like a novelty trick, the rev command is actually a devastatingly powerful tool when combined with other data extraction tools, specifically when you are trying to parse complex, inconsistent file paths or web addresses.

Step 1: The Basic Reversal

The basic syntax of the command is incredibly simple. You can reverse the contents of an entire text file instantly by typing the command followed by the filename.

rev myfile.txt

The terminal will instantly output every single line of the file, completely flipped from right to left. The order of the lines themselves remains exactly the same (Line 1 is still Line 1), but the characters inside the line are mirrored.

You can also use the echo command to pipe a single, specific string of text directly into rev to test how it works.

echo "Linux Administration" | rev

The terminal will output: “noitartsinimdA xuniL”.

Step 2: The Secret Power of the Reverse

To understand the true power of rev, you must look at a common data extraction problem.

Imagine you have a text file filled with thousands of different web addresses. Some URLs are short (http://website.com/index.html). Some are incredibly long and complex (http://website.com/blog/2024/january/images/photo.jpg).

Your boss demands that you extract only the filename from the very end of every single URL (e.g., you only want index.html and photo.jpg). You cannot use the standard cut command to extract the text by counting the slashes (/), because the short URL only has three slashes, while the long URL has six. If you tell Linux to “cut after the third slash,” the command will completely fail on the longer URLs.

Step 3: The Reversal Trick

Because the data you actually want is located at the absolute end of an unpredictable string, you can use the rev command to temporarily flip the internet upside down.

If you reverse the URL http://website.com/blog/images/photo.jpg, it becomes:

gpj.otohp/segami/golb/moc.etisbew//:ptth

Do you see the magic? By reversing the text, the filename (gpj.otohp) is suddenly forced to the absolute front of the sentence. Furthermore, it is now perfectly separated from the rest of the garbage by the very first slash (/).

Step 4: Executing the Pipeline

Now that the chaotic data is perfectly aligned at the front of the sentence, you can use a standard cut command to grab exactly what you need, and then immediately run rev a second time to flip the text back to normal English.

echo "http://website.com/blog/images/photo.jpg" | rev | cut -d'/' -f1 | rev

How this works:

  1. rev: Flips the URL backward.
  2. cut -d’/’ -f1: Tells Linux to look for the slash (-d'/') and extract only the very first field of text it sees (-f1). It successfully rips out gpj.otohp and deletes the rest of the URL.
  3. rev: It takes the isolated string gpj.otohp and flips it backward again.

The terminal instantly outputs the mathematically perfect answer: photo.jpg.

By using the rev command as a temporary structural mirror, you can force chaotic, unpredictable data into a perfectly uniform line, allowing you to slice massive datasets with absolute surgical precision.

Get the best tech tips delivered straight to your inbox.

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