Linux terminal on a Ubuntu laptop.
Fatmawati Achmad Zaenuri/Shutterstock

Linux’s rev command reverses strings of text. This command can operate either on provided text or a file, and it seems deceptively simple. But like many command-line utilities, its real power becomes apparent when you combine it with other commands.

The rev command is one of those simple Linux utilities that, at first glance, appears to be something of an oddity. It performs a single function: it reverses strings. And apart from being able to print a quick help page (-h) and show you its version number (-V), it doesn’t accept any command-line options.

So, rev reverses strings, and that’s it? No variations or options? Well, yes and no. Yes, it has no permutations, but no, that’s hardly all. This tutorial shows you how to combine it for powerful operations.

When you use rev as a building block in more complicated command sequences, it really starts to show its worth. rev is one of a group of commands (like tac and yes) that are facilitators. It’s easier to appreciate their usefulness when you see how they make the use of other commands more efficient.

Using the rev Command

Used on the command line with no other parameters, rev takes any typed input, reverses it, and then prints it in the terminal window. It keeps doing this until you hit Ctrl+C to exit.

rev

"rev" used with "stdin" in a terminal window.

If you type some text and press Enter, it makes rev print the string in reverse—unless you provide it with a palindrome, of course.

"rev" with a palindrome in a terminal window.

Passing Text to rev

You can use echo to pipe text to rev.

echo one two three | rev

"echo one two three | rev" in a terminal window.

You can also use rev to reverse the contents of an entire file of text, line by line. In this example, we have a file containing a list of filenames. The file is called “filelist.txt.”

rev filelist.txt

"rev filelist.txt" in a terminal window.

Each line is read from the file, reversed, and then printed to the terminal window.

Combining rev with Other Commands

Here’s an example using piping of input that calls rev twice.

This command strips the last character off the string of text. This could be useful to remove punctuation. We need to use the cut command to strip the character.

echo 'Remove punctuation.' | rev | cut -c 2- | rev

"echo 'Remove punctuation.' | rev | cut -c 2- | rev" in a terminal window.

Let’s break that down.

  • echo sends the string into the first call to rev.
  • rev reverses the string and pipes it into cut.
  • The -c (characters) option tells cut to return a sequence of characters from the string.
  • The 2- option tells cut to return the range of characters from character two until the end of the line. If a second number were provided, such as 2-5, the range would be from characters two to five. No second number means “up to the end of the string.”
  • The reversed string—minus its first character—is passed to rev which reverses the string, so it’s back to its original order.

Because we trimmed off the first character of the reversed string, we trimmed off the last character of the original string. Yes, you could do this with sed or awk, but this is an easier syntax.

Separating the Last Word

We can use a similar trick to return the last word of the line.

The command is similar to the last one: again, it uses rev twice. The differences lie in the way the cut command is used to select portions of the text.

echo 'Separate the last word' | rev | cut -d' ' -f1 | rev

"echo 'Separate the last word' | rev | cut -d' ' -f1 | rev" in a terminal window.

Here’s the command breakdown:

  • echo sends the string into the first call to rev.
  • rev reverses the string and pipes it into cut.
  • The -d' ' (delimiter) option tells cut to return a sequence of characters delimited by a space.
  • The -f1 option tells cut to return the first section of the string not containing the delimiter. In other words, the first part of the sentence up to the first space.
  • The reversed first word is passed to rev which reverses the string, so it’s back to its original order.

Because we extracted the first word of the reversed string, we trimmed off the last word of the original string. The last word of the sentence was “word,” and it’s printed out for us.

Trimming Content From Files

Let’s say we have a file containing a list of filenames, and the filenames are in quotation marks. We want to remove the quotation marks from the filenames.

Let’s look at the file:

less filelist.txt

"less filenames.txt" in a terminal window.

The contents of the file are displayed for us in less.

Contents of filenames.txt in less in a terminal window.

We can remove the punctuation from both ends of each line with the following command. This command uses both rev and cut twice.

rev filelist.txt | cut -c 2- | rev | cut -c 2-

"rev filelist.txt | cut -c 2- | rev | cut -c 2-" in a terminal window.

The filenames are listed for us without the quotation marks.

Filenames without quotation marks in a terminal window.

The command breaks down like this:

  • rev reverses the lines in the file and pipes them into cut.
  • The -c (characters) option tells cut to return a sequence of characters from each line.
  • The 2- option tells cut to return the range of characters from character two until the end of each line.
  • The reversed strings, minus their first characters, are passed to rev.
  • rev reverses the strings, so they’re back to their original order. They’re piped into cut a second time.
  • The -c (characters) option tells cut to return a sequence of characters from each string.
  • The 2- option tells cut to return the range of characters from character two until the end of each line. This “hops over” the leading quotation mark, which is character one on each line.

A Lot of Piping

Here’s a command that returns a sorted list of every file extension in the current directory. It uses five distinct Linux commands.

ls | rev | cut -d'.' -f1 | rev | sort | uniq

"ls | rev | cut -d'.' -f1 | rev | sort | uniq" in a terminal window.

The process is straightforward:

  • ls lists the files in the current directory. These are piped into rev.
  • rev reverses the filenames and pipes them into cut.
  • cut returns the first portion of each filename up to a delimiter. The -d'.' tells cut to use the period “.” as the delimiter. The portion of the reversed filenames up to the first period are the file extensions. These are piped into rev.
  • rev reverses the file extensions into their original order. They are piped into sort.
  • sort sorts the file extensions and pipes the results into uniq.
  • uniq returns a single listing for each type of unique file extension. Note if there’s no file extension (such as for the makefile, and the directories Help and gc_help), the entire filename is listed.

To put a finishing touch to it, add the -c (count) command-line option to the uniq command.

ls | rev | cut -d'.' -f1 | rev | sort | uniq -c

"ls | rev | cut -d'.' -f1 | rev | sort | uniq -c" in a terminal window.

We now get a sorted list of the different file types in the current directory with a count of each.

That’s a pretty nifty one-liner!

drawroF og ot drawkcaB gnioG

Sometimes you have to go backward to go forward. And you usually go forward fastest as part of a team.

Add rev to your repertoire of go-to commands, and you’ll soon be using it to simplify otherwise complicated command sequences.

Dave McKay Dave McKay
Dave McKay first used computers when punched paper tape was in vogue, and he has been programming ever since. He is now a Data Protection Officer and has worked as a freelance programmer, manager of an international software development team, and an IT services project manager. Dave is a Linux evangelist and open source advocate.
Read Full Bio »