๐ŸŽ New User? Get 20% off your first purchase with code NEWUSER20 ยท โšก Instant download ยท ๐Ÿ”’ Secure checkout Register Now โ†’
Menu

Categories

๐Ÿ’ก Text Processing October 21, 2026 11

Linux Command: rev

Reverse characters in each line of a file

Terminal โ€” Text Processing
Command
$ echo "Hello World" | rev
Output
dlroW olleH

The rev command reverses the character order of each line in a file or input stream. Each line is reversed independently โ€” the first character becomes the last and vice versa. rev is useful in text processing pipelines for tasks like extracting file extensions, reversing field order in combination with cut, parsing right-to-left data, and various string manipulation tricks. While it seems simple, rev is a powerful building block in Unix pipelines. Combined with cut and other tools, it enables extracting the last field from variable-length delimited data โ€” something that cut alone cannot do easily.

Syntax

rev [FILE]...

Key Options

  • (no flags) โ€” rev has no flags โ€” it simply reverses each line
  • file โ€” Reverse lines from a file
  • stdin โ€” Reverse lines from piped input
  • multiple files โ€” Process multiple files sequentially
  • with cut โ€” Common pattern: extract last field
  • palindrome check โ€” Compare original with reversed to check palindromes

Examples

Reverse a string

echo "Hello World" | rev

Output: dlroW olleH

Extract file extension

echo 'archive.tar.gz' | rev | cut -d'.' -f1 | rev

Output: gz

Get filename from path

echo '/var/log/nginx/access.log' | rev | cut -d'/' -f1 | rev

Output: access.log

Pro Tips

  • The rev | cut -d'x' -f1 | rev pattern extracts the last delimited field. This is one of the most useful rev tricks.
  • rev reverses characters within each line. tac reverses the order of lines in a file. Different tools for different reversal tasks.
  • rev may not correctly handle multi-byte UTF-8 characters on all implementations. Test with your locale if working with non-ASCII text.

Learn more: Full rev reference โ†’

Share this tip