rev

Reverse characters in each line of input.

Category: Text coreutils reverse strings pipes

Install

# Most systems (GNU coreutils) — often already installed
# RHEL / Alma / Rocky
sudo dnf install coreutils

# Debian / Ubuntu
sudo apt install coreutils

# openSUSE
sudo zypper install coreutils

# Arch
sudo pacman -S coreutils

What it does

rev reads text from standard input (or one or more files) and reverses the characters in each line. It’s handy for quick inspections, simple transformations, and shell pipelines.

How it works (mechanical)

  • Reads input line-by-line.
  • For each line, outputs the characters in reverse order.
  • Line boundaries stay the same (it does not reorder lines).
  • Works great in pipelines: echo ... | rev.
  • Commonly used with tools like cut, awk, and sed.

Quick Start

# Reverse a string
echo "stressed" | rev

# Reverse each line of a file
rev /etc/hosts

10 Practical Examples

# 1) Reverse a single word
echo "desserts" | rev
# 2) Reverse a sentence (characters, not words)
echo "Hello world" | rev
# 3) Reverse each line in a file
rev input.txt
# 4) Reverse from stdin
cat input.txt | rev
# 5) Reverse, then take the first N characters (which were last before)
echo "ABCDEFGHIJ" | rev | cut -c1-4
# 6) Extract the last 3 characters of each line (common trick)
# Reverse → take first 3 → reverse back
rev input.txt | cut -c1-3 | rev
# 7) Strip a filename extension (simple case)
# Example: "report.pdf" -> "report"
printf '%s
' "report.pdf" | rev | cut -d. -f2- | rev
# 8) Reverse a CSV line to work from the end (careful: not CSV-aware)
echo "a,b,c,d" | rev
# 9) Combine with awk to reverse characters only in a specific field
# Reverse field 2, keep the rest
echo "one two three" | awk '{ $2 = ("" $2 | "rev" ); print }'  # (see Notes)
# 10) Visual check: palindrome test (case-sensitive)
s="racecar"; [ "$s" = "$(printf '%s' "$s" | rev)" ] && echo "palindrome"

Notes & Gotchas

  • Characters vs words: rev reverses characters in each line, not word order.
  • Unicode: behavior can be surprising with combining characters / emojis; results may not match “visual” reversal.
  • Tabs/spaces: whitespace is reversed like any other character.
  • Multiple files: you can pass multiple filenames; output is concatenated in that order.
  • Field tricks: for “from-the-end” parsing, the pattern rev | cut ... | rev is common.

Historical Context

rev is a classic Unix text filter that’s been around for decades. It shows up in many Unix-like systems because it’s small, predictable, and composes well with other command-line tools.

Modern Equivalent

For anything beyond simple per-line reversal (CSV-aware parsing, Unicode graphemes, structured data), you’ll usually reach for awk, perl, or a short python one-liner — but rev remains a fast, handy building block in shell pipelines.

Related Commands

  • tac — reverse the order of lines (not characters).
  • cut — extract columns/fields from lines.
  • awk — powerful text processing and field logic.
  • sed — stream editor for transformations.
  • tr — translate or delete characters.