LinuxCommandLibrary

rev

Reverse each line in a file

TLDR

Reverse text typed into terminal

$ rev
copy

Reverse the text string "hello"
$ echo "hello" | rev
copy

Reverse an entire file and print to stdout
$ rev [path/to/file]
copy

Use '\0' as a line separator (zero termination)
$ rev -0 [path/to/file]
copy

Display help
$ rev -h
copy

Display version
$ rev -V
copy

SYNOPSIS

rev [FILE...]

DESCRIPTION

The rev command is a fundamental Unix-like utility designed to reverse the order of characters within each line of its input. It reads lines from specified FILEs or, if no files are provided, from standard input. For every line it processes, rev effectively reverses the sequence of all characters and then prints the modified line to standard output.

Unlike tac, which reverses the order of lines in a file, rev operates strictly at the character level within each individual line. This makes it an invaluable tool in text processing pipelines, often combined with other commands like grep or awk for specialized tasks such as identifying palindromes, preparing data for unique sorting requirements, or simple string manipulation.

For instance, running echo "Linux is awesome" | rev would produce emosewa si xuniL.

CAVEATS

rev processes input line by line; it does not reverse the order of lines in a file. For that specific functionality, the tac command should be used.

While modern implementations of rev generally handle multi-byte characters (such as those in UTF-8) correctly based on system locale settings, it's important to be aware that on older or misconfigured systems, it might process bytes literally, which could lead to incorrect reversal of complex multi-byte characters or grapheme clusters.

INPUT/OUTPUT

By default, rev reads from standard input. Alternatively, you can specify one or more FILEs as arguments; rev will then process lines from these files sequentially. If a FILE is specified as - (a hyphen), it also signifies reading from standard input. All processed output, regardless of the input source, is consistently written to standard output.

NO COMMAND-LINE OPTIONS

rev stands out for its straightforward design as it does not accept any command-line options or flags to modify its behavior. Its sole function is to reverse characters within lines, making it a highly focused utility.

HISTORY

rev is a classic and long-standing Unix utility, embodying the Unix philosophy of performing one task efficiently and effectively. It has been a standard component of GNU Coreutils from its early stages, ensuring its pervasive availability across Linux distributions and other Unix-like operating systems. Its simple yet powerful character-reversal capability has made it a quiet but essential part of the command-line toolkit for decades.

SEE ALSO

tac(1), grep(1), awk(1), sed(1)

Copied to clipboard