Linux rev Command Tutorial for Beginners (with Examples)

Sometimes, while working on the Linux command line, you may want to reverse a string or line(s) of text. You'll be glad to know there are command line utilities that let you do exactly this. Here, in this tutorial, we will discuss one such tool, dubbed rev. But before we do that, it's worth mentioning that all examples here have been tested on an Ubuntu 18.04 LTS machine.

Linux rev command

The rev command in Linux reverses lines of text characterwise. Following is its syntax:

rev [option] [file...]

And here's what the man page says about it:

       The  rev utility copies the specified files to standard output, revers?
       ing the order of characters in every line.  If no files are  specified,
       standard input is read.

Following are some Q&A-styled examples that should give you a better idea on how the rev command works.

Q1. How to use rev command?

Simple, just execute 'rev' sans any option.

rev

When you execute the tool in such a way, it will wait for you to enter some input. You can enter a word or a line, and as soon as you're done, press the Enter key, and you'll see reversed text in the output.

For example, I entered the following input:

hello world

And got the following output:

dlrow olleh

Q2. How rev command works with files?

This is also pretty straightforward. Just pass the name of the file as input, and you'll see reversed contents in output.

Here's an example:

How rev command works with files

So you can see that the lines in file 'test.txt' got reversed in the output.

Q3. How to see help and version information?

Use the -V and -h command line options for this.

rev -V
rev -h

Here's the output these commands produced on my system:

How to see help and version information

Q4. How to reverse line wise, instead of character wise?

While the rev command works character wise on lines of text, there's another command - dubbed tac - that reverses the information lines wise. You can learn more about tac by heading here.

Conclusion

The rev command doesn't have a steep learning curve. In fact, it doesn't really offer many command line options. In this tutorial, we have discussed majority stuff. To learn more about rev, you can head to the tool's man page.

Share this page:

0 Comment(s)