How to Rename Files in Linux

A command-line terminal is a necessary software for administrating Linux servers. It gives Linux customers a few of the greatest productive instruments while saving your machine’s assets.

To successfully use the potential of Linux, you will have to have sturdy data of the basics – easy instructions, like renaming current recordsdata and folders.  In this tutorial, you’ll discover ways to Linux rename folders.

How to Rename Files in Linux with the mv Command

Shortened from “move,” the mv command is, without doubt, one of the best instructions to make use of.  It can do two fundamental however important duties when dealing with recordsdata on Linux. One is shifting recordsdata from one location to a different one, and the opposite is renaming a number of recordsdata by means of the terminal.

First, let’s see how renaming recordsdata with mv works on Linux. To start, we enter our server by means of the command line utilizing SSH.

To enter our server, kind the next into your terminal:

ssh your-user@your-server

If we’re utilizing an area pc, as an alternative to a server, then we must open the terminal from the primary menu.

Afterward, it is very important to know the way the mv command works. To do that, we run the next:

mv --help

As we are able to see within the earlier picture, the fundamental use of the mv command is as follows:

mv [option] [SOURCE]...[DIRECTORY]

Here are a few of the hottest mv choices:

  • -f – exhibits no message earlier than overwriting a file.
  • -i – shows warning messages earlier than overwriting a file.
  • -u – solely transfer a file whether it is new or if it doesn’t exist within the vacation spot.
  • -v – present what the command does.

Rename Files in Linux Using the mv Command

If we need to rename a file, we are able to do it like this:

mv oldnamefile1 newnamefile1

Assuming we’re positioned within the listing, and there’s a file referred to as file1.txt, and we need to change the title to file2.txt. We might want to kind the next:

mv file1.txt file2.txt

As easy as that. However, in case you are not within the listing, you will have to kinda bit extra. For instance:

cd /home/user/docs/files
mv file1.txt file2.txt

Rename Multiple Files in Linux With the mv Command

The mv command can solely rename one file, however, it may be used with different instructions to rename a number of recordsdata.

Let’s take the instructions, discoverfor, or whereas loops, and rename a number of recordsdata.

For instance, when making an attempt to alter all recordsdata in your present listing from .txt extension to .pdf extension, you’ll use the next command:

for f in *txt; do
   mv -- "$f" "${f%.txt}.pdf"

This will create a loop (for) trying by means of the record of recordsdata with the extension .txt. It will then exchange every .txt extension with .pdf. Finally, it should finish the loop (executed).

If you need extra superior options, you’ll want to make use of the rename command, we’re about to cowl.

Rename Files in Linux Using the Rename Command

With the rename Files in Linux command, you should have a bit of extra management. Top Linux configurations embody it by default. But, if you happen to don’t have it put in, you are able to do it in only a minute with an easy command.

In the case of Debian, Ubuntu, Linux Mint, and derivatives:

sudo apt install rename

On the opposite hand, in case you are utilizing CentOS 7 or RHEL:

sudo yum install rename

And, in case you are utilizing Arch Linux:

yay perl-rename ## or yaourt -S perl-rename

Now, we are able to begin utilizing the rename command. In basic, the fundamental syntax of the rename command seems like this:

rename 's/old-name/new-name/' files

It could seem complicated at first, but it surely’s rather a lot less complicated than it might sound.

In this instance, we are going to create a brand new folder referred to as filetorename, and utilizing the contact command, we are going to create 5 recordsdata.

mkdir filetorename
cd filetorename
touch file{1..5}.txt

With the final ls command, you’ll be able to view the recordsdata that you just created.

If we need to rename a single file referred to as file1.txt, the sentence could be like this:

rename ‘s/file1/newfile1/’ file1.txt

If we wished to alter the extension to all recordsdata, for instance, to .php. We may do it this manner:

rename ‘s/.txt/.php/’ *.txt

We can even specify one other listing the place the recordsdata you need to rename are.

rename ‘s/.txt/.php/’ FILE/PATH

We’d like to say that rename makes use of a daily expression of Perl, which means this command has in-depth prospects.

Finally, it’s a good suggestion to test all of the command choices. You can view them within the terminal by executing:

rename –help

Remove Rename Command

If you do not want to have rename put in your system, take away it utilizing the software program supervisor. Or from the terminal.

For Debian, Ubuntu, Linux Mint and derivatives:

sudo apt remove rename

And for CentOS and RHEL:

sudo yum remove rename

That’s it, rename is eliminated out of your Linux machine.


Rename Files in Linux utilizing the terminal is a straightforward and sensible process however typically crucial. Knowing to learn how to do it is one thing each server supervisor ought to know. As now we have seen, there are two instructions that may do it. One is easier than the opposite, however, each accomplishes the duty. We encourage you to proceed to research these instructions and enhancing the standard of your on a regular basis workflow.

Leave a Reply