rev Command
Beginner Text Processing man(1)Reverse characters in each line of a file
👁 9 views
📅 Updated: Mar 15, 2026
SYNTAX
rev [FILE]...
What Does rev Do?
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.
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.
Options & Flags
| Option | Description | Example |
|---|---|---|
| (no flags) | rev has no flags — it simply reverses each line | echo "hello" | rev |
| file | Reverse lines from a file | rev words.txt |
| stdin | Reverse lines from piped input | echo "hello world" | rev |
| multiple files | Process multiple files sequentially | rev file1.txt file2.txt |
| with cut | Common pattern: extract last field | echo '/var/log/syslog' | rev | cut -d'/' -f1 | rev |
| palindrome check | Compare original with reversed to check palindromes | word='racecar'; [ "" = "$(echo | rev)" ] && echo 'Palindrome' |
Practical Examples
#1 Reverse a string
Reverses the characters in the string.
$ echo "Hello World" | rev
Output:
dlroW olleH
#2 Extract file extension
Gets the last extension by reversing, cutting the first field, and reversing back.
$ echo 'archive.tar.gz' | rev | cut -d'.' -f1 | rev
Output:
gz
#3 Get filename from path
Extracts the filename from a full path using rev+cut pattern.
$ echo '/var/log/nginx/access.log' | rev | cut -d'/' -f1 | rev
Output:
access.log
#4 Reverse each line in a file
Reverses the characters of each line in the file.
$ rev /etc/hostname#5 Check for palindrome
If the output matches the input, the word is a palindrome.
$ echo "racecar" | rev
Output:
racecar
#6 Get last field of variable-length data
Extracts the last colon-separated field regardless of how many fields exist.
$ echo 'a:b:c:d:e' | rev | cut -d':' -f1 | rev
Output:
e
Tips & Best Practices
Last field extraction: The rev | cut -d'x' -f1 | rev pattern extracts the last delimited field. This is one of the most useful rev tricks.
rev vs tac: rev reverses characters within each line. tac reverses the order of lines in a file. Different tools for different reversal tasks.
Multi-byte characters: rev may not correctly handle multi-byte UTF-8 characters on all implementations. Test with your locale if working with non-ASCII text.
Frequently Asked Questions
How do I reverse a string in bash?
Use echo "string" | rev. This reverses all characters in the string. In bash 4+, you can also use parameter expansion tricks.
How do I get the file extension from a filename?
Use echo 'file.tar.gz' | rev | cut -d'.' -f1 | rev to get the last extension (gz). For the base name, use basename command instead.
What is the difference between rev and tac?
rev reverses characters within each line (hello -> olleh). tac reverses the order of lines in a file (last line becomes first). They are different operations.
Related Commands
More Text Processing Commands
Master Linux with Professional eBooks
Curated IT eBooks covering Linux, DevOps, Cloud, and more
Browse Books →