Innovate | Connect | Transform
ContactService statusUSD $

GICT knowledge base

How to Use the rev Command to Reverse Lines of Text in Linux

Published

LinuxLinux and Commands

The rev command is a built-in Linux utility that often goes unnoticed but can play a powerful role in text processing and data manipulation. This tool reads input line by line and reverses the characters in every line. While its primary function may seem simple, you can leverage rev in a variety of practical ways, including data transformation, log analysis, scripting, and text obfuscation. If you work with Linux or Unix-like systems, understanding how to use rev can save you time and help you accomplish complex tasks more efficiently.

In this article, you will learn what the rev command does, how to install it, and how to use it in real-world scenarios. The article covers step-by-step instructions, integration with other commands, troubleshooting common issues, and advanced scripting techniques. By the end, you will have a deep understanding of how to use rev as part of your Linux toolkit.

Prerequisites

Before you begin, ensure you have:

  • Access to a Linux system or terminal emulator
  • Basic command line experience
  • Permission to install packages (if rev is not already present)

Introduction to the rev Command

The rev command processes text line by line, reversing the order of characters in each line. For example, the line:

Linux is awesome

becomes:

emosewa si xuniL

You can use rev for quick text transformations, as part of more complex processing pipelines, or even within automation scripts. It works with files, standard input, and the output of other commands.

Installing the rev Command

Most Linux distributions ship with the rev command as part of the util-linux package. However, some minimal installations or custom environments might not include it by default. To verify if rev is installed, run:

$ rev --version

If you see version information, you are ready to proceed. If you receive a “command not found” error, install the required package using your distribution’s package manager.

On Ubuntu or Debian

$ sudo apt update
$ sudo apt install util-linux

On CentOS or RHEL

$ sudo yum install util-linux

On Fedora

$ sudo dnf install util-linux

After installation, confirm that rev is available:

$ rev --version

Basic Usage of rev

The core syntax for using rev is straightforward:

$ rev [filename]

If you provide a file, rev reads and reverses each line of that file. If no file is specified, rev reads from standard input.

Step 1: Reversing a File

Suppose you have a file named phrases.txt:

Linux makes life easier
Shell scripting is fun
Practice improves skill

To reverse each line, run:

$ rev phrases.txt

Output:

reis ea efil sekam xuniL
nuf si gnitpircs llehS
lliks sevorpmi ecitcarP

Step 2: Reversing Standard Input

You can also use rev interactively or with piped input. For example:

$ echo "reverse this line" | rev

Output:

enil siht esrever

If you run rev alone, you can type lines directly and see the reversed output after pressing Enter. Press Ctrl+D to exit:

$ rev
Type any sentence here
ereh ecnetnes yna epyT

Using rev in Linux Pipelines

Step 3: Reverse Output from ls

To reverse the names of files in your current directory:

$ ls | rev

If your directory contains:

document.txt
report.pdf
notes.md

The output will be:

txt.tnemucod
fdp.troper
dm.seton

Step 4: Reverse Usernames from /etc/passwd

To extract and reverse the usernames:

$ cut -d: -f1 /etc/passwd | rev

If /etc/passwd contains:

root:x:0:0:root:/root:/bin/bash
user1:x:1000:1000::/home/user1:/bin/bash

The output will be:

toor
1resu

Step 5: Reverse the Last Word on Each Line

$ awk '{ print $1, $2, $3 | "rev" }' phrases.txt

You may also combine rev with awk, sed, cut, and other tools for more complex transformations.

Scripting with rev

Step 6: Batch Reverse All .log Files

Create a script named reverse_logs.sh:

#!/bin/bash

for file in *.log; do
    rev "$file" > "${file%.log}_reversed.log"
    echo "Reversed $file -> ${file%.log}_reversed.log"
done

Make the script executable:

$ chmod +x reverse_logs.sh

Step 7: Reverse User Input

Create a script named reverse_input.sh:

#!/bin/bash

read -p "Enter a phrase: " user_input
echo "$user_input" | rev

Advanced rev Use Cases

Palindrome Detection

$ read -p "Enter a word: " word
$ if [[ "$word" == "$(echo $word | rev)" ]]; then echo "Palindrome"; else echo "Not a palindrome"; fi

Log File Analysis

$ rev server.log | cut -d' ' -f1 | rev

Simple Text Obfuscation

$ rev confidential.txt > scrambled.txt

Handling Special Cases

  • Empty lines and whitespace are preserved exactly.
  • Unicode support depends on your terminal and locale.
  • Binary files should not be processed with rev.

Troubleshooting and Best Practices

  • If rev is missing, install util-linux.
  • Check encoding for unexpected output.
  • Use rev with awk, sed, cut, and pipelines for more power.

rev vs tac

rev reverses characters in lines, while tac reverses the order of lines.

Line one
Line two
Line three

rev output:

eno eniL
owt eniL
eerht eniL

tac output:

Line three
Line two
Line one
$ tac sample.txt | rev

When Should You Use rev?

  • Quick reverse transformations
  • Text obfuscation
  • Log suffix or extension extraction
  • Palindrome testing
  • Educational scripting exercises

NOTE: rev is not encryption and should not be used for security.

Conclusion

The rev command is a simple yet effective tool for reversing the characters in each line of a text file or input stream. Despite its straightforward functionality, rev can be quite powerful when combined with other Linux utilities or used in shell scripts. Knowing how to use rev expands your ability to manipulate and analyze text, process logs, create custom tools, and automate repetitive tasks. Whether you are a student, developer, administrator, or hobbyist, adding rev to your command-line toolbox can help you solve text-processing challenges with confidence.

With its ease of use and versatility, rev demonstrates the power of Unix philosophy: small tools can accomplish big things when you know how to use them well. Try incorporating rev into your next Linux session and see how it can streamline your workflow.

Related guides

Account services are unavailable until the production backend is configured.