Linuxs rev
Command reverses text strings. This command can be run with either provided text or a file and seems deceptively simple. But, like many command line utilities, its true power shows when you combine it with other commands.
the rev
command is one of those simple Linux utilities that might seem a bit strange at first. It performs one function: it inverts strings. And besides being able to print out a quick help page ( -h
) and show the version number ( -V
), it doesn’t accept Command line options .
So, rev
flips strings, and that’s it? No variations or options? Well yes and no. Yes, it has no permutations, but no, that’s hardly all. This tutorial will show you how to combine them for powerful operations.
If you use rev
as a component in more complicated command sequences, it is only just beginning to show its value. rev
belongs to a group of commands (like tac
and yes
) who are moderators. Their usefulness is easier to see when you see how they make using other commands more efficient.
Using the rev command
Used on the command line with no other parameters rev
accepts every input entered, reverses it and then prints it out in the terminal window. This will continue until you press Ctrl + C to exit.
rev
When you enter text and press Enter, it does rev
Output the string in reverse order – unless you type an a. at palindrome , Of course.
Passing text to Rev
You can use echo
to direct text rev
.
echo one two three | rev
You can also use rev
to reverse the content of an entire text file line by line. In this example, we have a file with a list of filenames. The file is called “filelist.txt”.
rev filelist.txt
Each line is read from the file, reversed and then output in the terminal window.
Combining rev with other commands
Here is a example Using pipes from inputs calling rev
twice.
This command removes the last character from the text string. This can be useful for removing punctuation marks. We have to use that cut
Command to undress the character .
echo 'Remove punctuation.' | rev | cut -c 2- | rev
Let’s break that down.
-
echo
sends the string in the first call torev
. -
rev
turns the string over and feeds it incut
. - the
-c
(Sign) option sayscut
to return a sequence of characters from the string. - the
2-
Option sayscut
to return the range of characters from character two to the end of the line. If a second number was given, such as2-5
, the range would be from two to five characters. No second number means “to the end of the string”. - The reverse string – minus the first character – is an. to hand over
rev
reversing the string so that it is back in its original order.
Since we truncated the first character of the reversed string, we truncated the last character of the original string. Yes, you could do that with sed
or awk
, but this is a simpler syntax.
Separate the last Word
We can use a similar trick to return the last word on the line.
The command is similar to the last one: Again, it uses rev
twice. The differences are in the way that cut
Command is used to select parts of the text.
echo 'Separate the last word' | rev | cut -d' ' -f1 | rev
Here is the command breakdown:
-
echo
sends the string in the first call torev
. -
rev
turns the string over and feeds it incut
. - the
-d' '
(Separator) option sayscut
to return a string separated by a space. - the
-f1
Option sayscut
to return the first section of the string that does not contain the separator. In other words, the first part of the sentence up to the first space. - The reversed first word becomes at. to hand over
rev
reversing the string so that it is back in its original order.
Since we extracted the first word of the inverted string, we truncated the last word of the original string. The last word of the sentence was “word” and was printed out for us.
Cropping content from files
Suppose we have a file with a list of filenames, and the filenames are enclosed in quotation marks. We want to remove the quotation marks from the filenames.
Let’s look at the file:
less filelist.txt
The content of the file is shown to us in less
.
We can use the following command to remove the punctuation marks at either end of each line. This command uses both rev
and cut
twice.
rev filelist.txt | cut -c 2- | rev | cut -c 2-
For us, the file names are listed without quotation marks.
The command is broken down as follows:
-
rev
reverses the lines in the file and redirects them tocut
. - the
-c
(Sign) option sayscut
to return a sequence of characters from each line. - the
2-
Option sayscut
to return the range of characters from character two to the end of each line. - The reverse strings, minus their first characters, are sent to. to hand over
rev.
-
rev
reverses the strings so they are back in their original order. You have blown the whistlecut
a second time. - the
-c
(Sign) option sayscut
to return a sequence of characters from each string. - the
2-
Option sayscut
to return the range of characters from character two to the end of each line. This “jumps” over the leading quotation mark, which is the first character in every line.
Lots of piping
Here is a command that will return an ordered list of all the file extensions in the current directory. It uses five different Linux commands.
ls | rev | cut -d'.' -f1 | rev | sort | uniq
The process is simple:
-
ls
lists the files in the current directory. These are fed inrev
. -
rev
reverses the filenames and redirects them tocut
. -
cut
returns the first part of each file name up to a separator. the-d'.'
toldcut
to use the period “.” as a separator. The part of the reversed filenames up to the first dot are the file extensions. These are fed inrev
. -
rev
reverses the file extensions to their original order. They are fed insort
. -
sort
sorts the file extensions and guides the resultsuniq
. -
uniq
returns a single listing for each type of unique file extension. Note that the entire file name is listed if there is no file extension (as for the Makefile and the Help and gc_help directories).
To add the finishing touches, add the -c
(count) command line option to uniq
Command.
ls | rev | cut -d'.' -f1 | rev | sort | uniq -c
We now get a sorted list of the different file types in the current directory with the respective number.
That’s a pretty nifty one-liner!
DrawroF and ot DrawkcaB gnioG
Sometimes you have to go backwards to move forward. And usually, the fastest way to get ahead in a team.
Add to rev
to your repertoire of go-to commands and you will soon be using it to simplify otherwise complicated command sequences.
Linux commands | ||
Files | tar · pv · cat · tac · chmod · grep · difference · sed · With · man · pushed · popd · fsck · Test disk · seq · fd · pandoc · CD · $ PATH · awk · join · jq · wrinkles · unique · Journalctl · tail · stat · ls · fstab · echo · fewer · chgrp · chown · rev · look · Strings · Type · rename · Postal code · unzip · assemble · ummount · To install · fdisk · mkfs · rm · rmdir · rsync · df · gpg · weather · Nano · mkdir · from · ln · Patch · Convert · rclon · Scraps · srm | |
Processes | alias · screen · above · kind · renice · progress · strace · system · tmux · chsh · story · at · Batch · for free · which · dmesg · chfn · User mod · ps · chroot · xargs · tty · pinkie finger · lsof · vmstat · Time out · Wall · Yes sir · kill · sleep · sudo · it is · Time · groupadd · User mod · groups · lshw · switch off · start anew · Stop · switch off · passwd · lscpu · crontab · date · bg · fg | |
Networking | netstat · Ring · Trace route · ip · ss · who is · fail2ban · bmon · she · finger · nmap · ftp · curl · wget · who · who am I · w · iptables · ssh-keygen · ufw |
Best Linux Laptops for Developers and Enthusiasts