rev command in Linux with Examples
The rev
command in Linux is used to reverse the characters in each line of a string or text. It reads input line by line and outputs each line with its characters in reverse order. If no file is specified, rev
reads from standard input; otherwise, it processes the contents of the given file(s) and writes the reversed output to the terminal.
Here, we will explore the syntax, examples, and options associated with the rev command to effectively utilize it in your Linux environment.
Syntax
rev [option] [file...]
where,
- file...: Specifies the file(s) to process. If no file is provided, rev will read from standard input.
- options: Command-line options that modify the behavior of the rev command.
Examples of Using the rev Command
Example 1: To see the Manual page of the rev Command
Use the man rev
command to access the manual page and learn more about the usage and options of the rev
command.
Syntax :
man rev

Example 2: How to Reverse a String Using the rev Command
changing the order of characters in the string so that the first character becomes the last, the second becomes the second-last, and so on
Syntax:
rev {press Enter}
[string] {press Enter}

Example 3: Displaying Reverse text in the Terminal
When you reverse text in the terminal, you use a command (like rev
) to take a string or file input and output the text with the character order reversed, directly in the terminal window.
Syntax:
echo "enter the text" |rev

Example 4: Reverse the text into the text file
Suppose we have a text file named as 'sample.txt'.

Using rev command on sample file. It will display the result on the terminal as follows:

Each line in the file is processed individually, and the characters within each line are reversed.
Common Options for the rev command
The rev command comes with a few options that can be helpful for specific tasks. Below are the available options:
1. -V (version):
This option displays the version information of the rev command and then exits
Syntax:
rev -V

2. -h (help):
This option prints a help message, displaying the available options and usage of the rev command.
Syntax:
rev -h

Conclusion
The rev command in Linux is a useful utility for reversing the order of characters in each line of text, whether it's from a file or direct input. While the command is simple, it can be powerful when combined with other Linux commands for text manipulation tasks. By understanding its syntax, options, and examples, you can incorporate rev into your Linux workflow to simplify text processing tasks.