Skip to content

rev Command: Reverse Lines Character by Character

Summary

The rev command reverses each line of its input, character by character. It's a simple yet surprisingly useful tool for manipulating text.

Introduction

The rev command is a utility in Linux and Unix-like operating systems that reverses the characters in each line of a file or from standard input. It reads input line by line and writes each line to standard output with the characters in reverse order. This can be helpful in a variety of situations, such as processing data where the order of characters matters, obfuscating text, or simply for fun.

Use Case and Examples

Basic usage: Reverse a string

echo "hello" | rev
This reverses the string "hello" and outputs "olleh".

Reverse the lines in a file

rev file.txt
This reverses each line of the file.txt and prints the reversed lines to the standard output.

Using rev with redirection to create a mirrored file

rev file.txt > reversed_file.txt
This reverses each line of file.txt and saves the reversed lines to a new file named reversed_file.txt.

Reverse multiple files

rev file1.txt file2.txt
This command will reverse the lines in file1.txt and file2.txt and print them to the standard output, one after the other.

Reverse standard input until EOF

rev
This starts rev in interactive mode, reading from standard input. Type text and press Enter to reverse it. Press Ctrl+D to signal end-of-file (EOF) and exit.

Commonly used flags

Flag Description Example
-V, --version Display version information and exit. rev --version
-h, --help Display help message and exit. rev --help
--stdio-sync Flush standard output after each line. Useful when piping output to another program. rev --stdio-sync file.txt | other_command


Share on Share on

Comments