Have you ever try to use rename command on Linux? Do you want to take grips with the file renaming powerhouse of the Linux world and provide mv
—and yourself—a rest. Rename is fast, flexible, and quite easier. Here’s a detailed guide on this powerhouse of a command.
Contents
- 1 What’s Going Wrong With mv?
- 2 How To Rename a Single File With mv
- 3 How to Rename Multiple Files with mv
- 4 What Happened?
- 5 Here’s An Easy Way
- 6 Use rename Command -> Come Let’s One More Try
- 7 What Happened Here?
- 8 Modifying Other Parts of a Filename
- 9 Removing Part of a Filename
- 10 Limit Modifies to Particular Parts of Filenames
- 11 Looking With Groupings
- 12 Use rename Command With Translations
- 13 You Can’t Learn Perl in Five Minutes
What’s Going Wrong With mv?
There’s nothing went wrong with mv
. However, the command does a good job. Also, you can found it on all Linux distributions, in macOS, and in other Unix-like OS. However, it’s always available. But certainly, you don’t want a shovel but need a bulldozer.
The command mv
has a purpose in life, and that is to move files. It’s a safe side effect that it can be used to move an older file into the new file, with a new name. The effect is to rename the file, so you get what you want. But mv is not a customize file renaming tool.
How To Rename a Single File With mv
If you want to use mv
to rename a file then type mv, space, the file name, space, and the new name you want the file to have. Then hit Enter.
You can also use ls
to check that the file becomes renamed or not
mv oldfile.txt newfile.txt
ls *.txt
How to Rename Multiple Files with mv
Things get complex after rename multiple files. mv
has no capability to deal with renaming various files. You must resort to using some useful Bash tricks. It’s good if you know some medium-grade command-line fu, but the difficulty of renaming different files with mv stands just to the ease of using mv to rename a single file.
Things escalate instantly.
Let’s say here is a directory with lots of files in it, of differing types. Some files have a “.prog” extension. We wish to rename those files at the command line so that they have a “.prg” extension.
Come let’s take a look at the files.
ls *.prog -l
Here’s one method to do it that can’t resort to writing an actual Bash script file.
for f in *.prog; do mv -- "$f" "${f%.prog}.prg"
Did it work or not? Let’s check the files and then proceed further.
ls *.pr*
So, it works fine. They all are “.prg” files now, and there are no “.prog” files in the directory.
What Happened?
What did the long command actually do? Let’s check it out.
for f in *.prog; do mv -- "$f" "${f%.prog}.prg"
The first part begins a loop that is going to process every “.prog” file in the directory, in turn.
Another part says what the processing will do. It uses mv to move each file to the new one file. The new file name is going to be quite similar to the original file’s name except for the “.prog” part. Also, the extension of “.prg” will be used.
Here’s An Easy Way
Most definitely. Here’s the rename command.
It is not part of standard Linux distribution, so you want to install it. Also, it has a different name in various families of Linux, but they all work in a similar way. You’ll simply substitute the correct command name according to the taste of Linux you’re using.
In Debian-derived or Ubuntu distributions you simply install rename
like this:
sudo apt-get install rename
In RedHat-derived distributions or Fedora you simply install prename like this. Remember that the initial “p,” stands for Perl.
sudo dnf install prename
To install it in Manjaro Linux use the following command. Keep in mind that the renaming command is known as perl-rename
.
sudo pacman -Syu perl-rename
Use rename Command -> Come Let’s One More Try
Here we’ll use rename
. We are doing this so that we have a set of “.prog” files.
ls *.prog
Come let’s use the command given below to rename them. We’ll also check with ls command whether it worked or not. If you want to substitute rename
with the accurate command name for Linux if can’t use Ubuntu or a Debian-derived Linux.
rename 's/.prog/.prg/' *.prog
ls *.pr*
It works well, now they all are “.prg” files excluding “.prog” files left in the directory.
What Happened Here?
Let’s discuss it in three parts.
In the first part, you’ll view the command name, rename
(or prename
or perl-rename
, for the other distributions).
The third last part is *.prog
, which explains rename
to operate on all “.prog” files.
The second part explains the work we like to be done on each filename. The s
means substitute. The 1st term (.prog
) is what rename
will look for in each filename and the 2nd term (.prg
) is what it will be substituted with.
The commands middle part or central expression is a Perl ‘regular expression‘ and it is what provides the rename
command its reliability.
Modifying Other Parts of a Filename
We’ve modified filename extensions so far, let’s change other parts of the filenames.
In the directory, there are many C source code files. All filenames are prefixed with “slang_”. We can also check this with ls
.
ls sl*.c
Here we replace all the occurrences of “slang_” with “sl_”. The command format is quite familiar to us. We’re just modifying the search term, the replacement term, and the type of file.
rename 's/slang_/sl_' *.c
Here we are searching for “.c” files, and looking for “slang_”. Whenever you found “slang_” in a filename it is substituted with “sl_”.
You can also check the command result after repeating the command ls
from above using the same parameters:
ls sl*.c
Removing Part of a Filename
We can also delete a part of a filename after substituting the search term with nothing.
ls *.c rename 's/sl_//' *.c ls *.c
We can also view from the ls command that our “.c” files are all prepended with “sl_”. Let’s leave that altogether.
The rename command follows the same format as mentioned above. We’re going to search for “.c” files. The search term is “sl_”, but there is no term of substitution. Two backslashes ‘//’ without anything between them mean an empty string.
rename
can process each “.c” file in turn. Also, it searches for “sl_” in the filename. If you found it, you can be replaced by nothing. Alternatively, the search term is removed.
Another use of the ls
command confirms that the “sl_” prefix has been erased from every “.c” file.
Limit Modifies to Particular Parts of Filenames
Come let’s use ls
to view at files that have the string “param” in their filename. Then we’ll use rename command to replace the string via string “parameter”. Also, we use ls one more time to view the effect the rename command has had on those files.
ls *param*
rename 's/param/parameter' *.c
ls *param*
You’ll then view four files having “param” in their filename. param.c, param_one.c, and param_two.c all have “param” at the begining of their name. third_param.c has “param” at the last of its name, just before the extension.
You can search rename command for “param” everywhere in the filename. You can then replace it with “parameter” in all cases.
Another use of ls command displays us that what has happened exactly. Whether “param” was at the begining or at the end of the filename. You can also replace it with a “parameter.”
We can use Perl’s metacharacters to purify the behavior of the central expression. Metacharacters are the signs that represent sequences or positions of characters. For instance, ^
means “start of a string,” (.) means any single character (besides from a newline character), $
means “end of a string”.
Also, we use the beginning of string metacharacter ( ^
) to limit our search to the start of the filenames.
ls *param*.c
rename 's/^parameter/value/' *.c
ls *param*.c
ls value*.c
The files we renamed are listed previously. Also, we can view the string “parameter” is at the beginning of 3 filenames and it is located at the end of one of the filenames.
Our rename command uses the begining of the line metacharacter (^) before the search term “parameter.” It explains to just consider the search term to found if it is at the begining of the filename. The search string “parameter” will be avoided if it is anywhere else in the filename.
Looking with ls
, we can then view that the filename having “parameter” at the last of the filename has not been changed. But the three filenames having “parameter” at the begining of their names having search string replaced by the substitute term “value.”
The rename power lies in the power of Perl.
Looking With Groupings
Here’s the case where you probably have files with the same strings in their names. They are not similar exactly the same strings, so you just look for and substitution can’t work here.
In this example we use ls
to view which files we have that begins with “str”. Also, there are two of them, string.c and strangle.c. We can also rename both strings after using a method known as a grouping.
The middle expression of the rename command will search for strings within filenames having the character sequence “stri” or “stra”. Here these sequences are followed by “ng”. Alternatively, our search term is going to look for “string” and “strang”. The substitution term is “bang”.
ls str*.c
rename 's/(stri|stra)ng/bang/' *.c
ls ban*.c
After using ls
another time confirms that string.c has become a bang.c and strangle.c is now bangle.c.
Use rename Command With Translations
The rename command performs all actions on filenames known as translations. Here’s an example of a translation that forces a set of filenames into uppercase.
In the rename command given below notice that we’re not using a s/
to begining the middle expression, we’re using y/
. It explains that the command can’t perform a substitution; we’re performing a translation.
The term a-z
is a Perl expression. It means that all lowercase characters in the sequence from a to z. But, the term A-Z
represents all uppercase letters in the sequence from A to Z.
The middle expression in this command can be paraphrased as “if any of the lowercase letters from a to z are found in the filename. It replaces them using the corresponding characters from the order of uppercase characters from A to Z.”
If you want to force the filenames of all “.prg” files to uppercase, simply use this command:
rename ‘y/a-z/A-Z/’ *.prg
ls *.PRG
The command ls
display us that all of the “.prg” extension files are now in uppercase. Also, to be strictly accurate, they’re not “.prg” files anymore. Well, they’re “.PRG” files. Make sure that Linux is case sensitive.
We can also reverse that last command after returning back to the position of the a-z
and A-Z terms in the middle expression.
rename ‘y/A-Z/a-z/’ *.PRG
ls *.prg
You Can’t Learn Perl in Five Minutes
Getting grips with Perl is time well spent. But to begin using the time-saving capabilities of the rename command, you don’t view to have much Perl knowledge at all to reap large advantages in power.
Conclusion:
Here’s all about ‘Use rename Command On Linux’. What are your views about it? If you want to share any other method or tips regarding the article then let us know below. For further queries and questions let us know in the comment section below!
Also Read: