Linux rev command
I. Overview of the rev command
rev is a simple yet practical text-processing command in Linux; its function is to reverse the order of characters in each line of the input text. The command name is short for "reverse".
Basic Functionality
- Reverse the character order of each line of text
- Keep the order between lines unchanged
- Process standard input or file contents
Typical use cases
- Test text-processing capabilities
- Check for palindromes
- Process specially formatted data
- Debugging and text analysis
II. Command syntax and parameters
Basic Syntax
rev [选项] [文件...]
Parameter Description
The rev command is very simple; most Linux implementations have no option parameters:
| Parameters | Description |
|---|---|
| file | specify the files to process (multiple allowed); if none are given, read standard input |
| -V/--version | display version information (supported by some implementations) |
| -h/--help | display help information (supported by some implementations) |
Note: rev implementations may vary slightly across Linux distributions; you can use
man revto view the specific documentation
III. Usage examples
Example 1: Basic usage
Reverse the contents of standard input:
Example
$
echo
"hello world"
|
rev
dlrow olleh
Example 2: Process file contents
Suppose there is a file example.txt with the content:
Linux
Command
rev
Perform the reversal:
Example
$
rev
example.txt
xuniL
dnammoC
ver
Example 3: Multiple-file processing
Example
$
rev
file1.txt file2.txt
displays the reversed contents of the two files in sequence
Example 4: Check for a palindrome
Example
$
echo
"madam"
|
rev
|
grep
-q
"madam"
&&
echo
"is a palindrome"
||
echo
"is not a palindrome"
is a palindrome
IV. How it works
The internal processing flow of the rev command:

Key points:
- process text line by line, keeping the line order unchanged
- fully reverse the character order of each line (including spaces and special characters)
- do not modify the original file contents
V. Notes and tips
Common questions
Invisible characters: rev reverses all characters, including spaces, tabs, etc.
Example
$
echo
-e
"a\tb"
|
rev
b a
Multibyte characters: some implementations may not handle Unicode characters correctly
Example
$
echo
"Chinese"
|
rev
may be displayed incorrectly
Practical tips
Combine with other commands to create complex pipelines:
Example
$
cat
/
etc
/
passwd
|
rev
|
cut
-d:
-f1
|
head
-5
Quickly reverse a file and save the result:
Example
$
rev
input.txt
output.txt
Check symmetry:
Example
$
diff
-s
<
(
cat
file.txt
)
<
(
rev
file.txt
|
rev
)write a shell script that does the following:
- accept a file as an argument
- check whether the file contains any palindrome lines
- output all palindrome lines and their line numbers
Reference solution:
Example
#!/bin/bash
while
IFS
=
read
-r
line;
do
reversed
=$
(
echo
"$line"
|
rev
)
if
[
"$line"
=
"$reversed"
]
;
then
echo
"Palindrome line: $line"
fi
done
<
"$1"
VII. Summary
Although simple, the rev command has unique value in text processing:
- Minimalist design: a model of the Unix philosophy of doing one thing
- Pipeline-friendly: perfectly suited to the Linux command pipeline system
- Educational value: helps you understand the basic concepts of text processing
Mastering the rev command lets you:
- enhance your understanding of the Linux text-processing toolchain
- lay a foundation for learning more complex text-processing commands such as sed and awk
- solve certain special text-processing needs
Suggested further study:
taccommand (outputs lines in reverse order)sedcommand text conversion featuresawkthe string-handling functions of
Linux Command Reference