Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux rev command

Linux rev command

Linux Command Reference Linux Command Reference


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

Typical use cases

  1. Test text-processing capabilities
  2. Check for palindromes
  3. Process specially formatted data
  4. 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 rev to 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:

  1. process text line by line, keeping the line order unchanged
  2. fully reverse the character order of each line (including spaces and special characters)
  3. 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:

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:

Mastering the rev command lets you:

  1. enhance your understanding of the Linux text-processing toolchain
  2. lay a foundation for learning more complex text-processing commands such as sed and awk
  3. solve certain special text-processing needs

Suggested further study:

Linux Command Reference Linux Command Reference