Text and processing

The rev command

Reverses the characters of each line. The order of the lines does not change.

Syntax

rev [opciones] [archivo...]Try it in the terminal →

Most useful options

-0, --zeroUses the NUL byte (\0) as the record separator instead of the newline. It works well with the output of find -print0.
-h, --helpShows the help and exits.
-V, --versionShows the version and exits.

Copy-ready examples

$ echo hola | revreverses a word that comes through a pipe.

Example output

aloh
$ echo "Hola mundo" | revreverses the spaces and the capital letters too.

Example output

odnum aloH
$ printf 'uno\ndos\n' | revreverses each line separately; the lines keep their order.

Example output

onu
sod
$ printf 'abc\0def\0' | rev -0 | tr '\0' '\n'with -0 each record ends in NUL; tr swaps it for a newline so you can see it.

Example output

cba
fed

You can try them in the browser terminal: it does not touch your computer.

Common errors

MessageCauseFix
rev: invalid option -- 'Q'rev has no -Q option. Its options are -0, -h and -V.Fix the letter or check the options with rev --help.
rev: cannot open nofile: No such file or directoryThe file does not exist or the name is mistyped. rev warns, skips that file and continues with the rest.List the folder with ls and copy the exact name.

Typical mistakes

  • It reverses inside each line, not the order of the lines: that is what tac is for.
  • A huge file without line breaks is one giant line, and rev needs all of it in memory. Split it first.
  • rev splits records only at newlines. Output separated by NUL, like find -print0, needs -0 or it is treated as one single line.

Differences from similar commands

  • tac: reverses the order of the lines and leaves each line as it is. It is the counterpart of rev.
  • tr: replaces or deletes specific characters, but it does not reverse the text. It transforms, it does not flip.

Related commands

FAQ

How do I check whether a word is a palindrome?

Compare it with its reversed version: [ "$(echo "$p" | rev)" = "$p" ] succeeds only when they match. Spaces count, so remove them first if they should not.

How do I reverse the order of the words in a sentence?

rev reverses letters, not words. Turn the spaces into newlines with tr, reverse the lines with tac and join them back: echo "one two three" | tr ' ' '\n' | tac | tr '\n' ' ' prints 'three two one ' with a trailing space.

How does rev handle accents and the letter ñ?

rev counts characters according to the locale. A precomposed ñ or accented vowel is reversed as one letter; a combining accent stored as a separate character can end up apart from its letter.

Learn to actually use it

The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.

Start the free course