Linux Rename Files With These Simple Methods And Hacks


Get to grips with the file renaming powerhouse of the Linux world and give mv—and yourself—a rest. Rename is flexible, fast, and sometimes even easier.  Here’s a tutorial to this powerhouse of a command. In this article, we will cover everything about Linux Rename files.

Why You Shouldn’t Use mv?

There’s nothing wrong with mv for Linux rename files. The command does a good job and it is found on all Linux distributions, in macOS, and in other Unix-like operating systems. So it’s always available. But sometimes you just need a bulldozer, not a shovel.

The mv command has a purpose in life, and that is to move files. It is a happy side effect that it can be used to move an existing file into a new file, with Linux rename files. The net effect is to rename the file, so we get what we want. But mv is not a dedicated file renaming tool.

Linux Rename Files With mv

To use mv to Linux Rename Files type mv, space, the name of the file, space, and the new name you wish the file to have. Then press Enter.

You can use ls to check the file has been renamed Linux Rename Files.

mv oldfile.txt newfile.txt

ls *.txt

Linux Rename Files (Multiple Files) with mv

Things get trickier when you want to Linux Rename Files multiple files. mv has no capability to deal with renaming multiple files. You must resort to using some nifty Bash tricks. That’s fine if you know some medium-grade command-line fu, but the complexity of renaming multiple files with mv stands in stark contrast to the ease of using mv to Linux Rename Files.

Things escalate quickly.

Let’s say we’ve got a directory with a variety of files in it, of differing types. Some of these files have a “.prog” extension. We want to rename them at the command line so that they have a “.prg” extension.

Linux Rename Files

How do we wrangle mv into doing that for us? Using ls *.prog -l.

Here’s one way to do it that doesn’t resort to writing an actual Bash script file. for f in *.prog; do mv — “$f” “${f%.prog}.prg”

The first part starts a loop that is going to process each “.prog” file in the directory, in turn.

The next part says what the processing will do. It is using mv to move each file to a new file. The new file is going to be named with the original file’s name excluding the  “.prog” part. A new extension of “.prg” will be used instead.

There Has To Be A Simpler Way

Most definitely. It is the rename command.

Linux Rename Files is not part of a standard Linux distribution, so you will need to install it. It also has a different name in different families of Linux, but they all work the same way. You’ll just have to substitute the appropriate command name according to the Linux flavor you’re using.

in Ubuntu and Debian-derived distributions you install rename like this:

-sudo apt-get install rename

In Fedora and RedHat-derived distributions you install prename like this. Note the initial “p,” which stands for Perl.

To install it in Manjaro Linux use the following command. Note that the renaming command is called perl-rename.

Linux Rename Files

– sudo pacman -Syu perl-rename

Now let’s use the following command to rename them. We’ll then check with ls whether it worked. Remember to substitute rename with the appropriate command Linux Rename Files if you’re not using Ubuntu or a Debian-derived Linux.

Also read: How to Delete password from a password very easily.

– rename ‘s/.prog/.prg/’ *.prog

– ls *.pr*

The first part is the command name, rename (or prename or Perl-name, for the other distributions.

The last part is *.prog, which tells rename to operate on all “.prog” files.

The middle part defines the work we want to be done on each filename. The s means substitute. The first term (.prog) is what rename will search for in each filename and the second term (.prg)  is what it will be substituted with.

Also read: What is System 32? Should you delete it or keep it?

The middle part of the command, or central expression, is a Perl ‘regular expression’ and it is what gives the rename command its flexibility.

Linux Rename Files


Hope this article has helped you to the Linux rename files. We will be back with other tips and tricks. Until then let us know if this article was helpful and you were able to rename Linux files.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.