How to Rename Files in Linux – mv Command

A command line terminal is an essential tool for managing Linux servers. Give Linux users some of the best productivity tools while saving your machine resources. In this tutorial, you will learn how to rename files and folders in Linux.

To effectively use the potential of Linux, you’ll need to have a solid understanding of the basics: , such as renaming existing files and folders.

How to rename files in Linux with the mv command

Acronym for “move”, the command mv is one of the easiest commands to use. You can perform two basic but essential tasks when handling files in Linux. One is to move files from one location to another, and the other is to rename one or more files through the terminal.

First, let’s see how to rename files with the command mv on Linux.

To start with, we access our server through the command line using . If you’re unsure about how to operate SSH and want more information, here’s a helpful one.

To access our server, type the following in your terminal:

ssh your-user@your-user

If we are using a local computer, instead of a server, we have to open the terminal from the main menu.

Next, you need to know how the command works mv. To do this, we execute the following:

mv –help

You will see that the basic usage of the command mv is the following:

wm…

These are some of the options mv More popular:

  • -F: Does not show any message before overwriting a file.
  • -Yo: Display warning messages before overwriting a file.
  • -or: Move a file only if it is new or does not exist in the destination.
  • -v: shows what the command does.
See also  We asked Syed Balkhi, founder of BeginnerWP, what he thinks about the future of WordPress

And the parameters are:

: the source of the file

: the destination directory.

Rename files in Linux with the mv command

If we want to change the name of a file, we can do it like this:

mv oldfilename1 newfilename1

Assuming we are located in the directory and there is a file called file1.txt and we want to rename it to file2.txt. We must write the following:

mv file1.txt file2.txt

It’s that easy. However, if you’re not in the directory, you’ll need to type a bit more. For example:

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

Rename multiple files with mv command

The command mv can only rename one file, but can be used with other commands to rename multiple files.

Let’s take the commands, find, for or the loops while and let’s rename several files.

For example, when trying to change all files in your current directory from .txt extension to .pdf extension, you would use the following command:

for f in *txt; do mv — “$f” “${f%.txt}.pdf” done

This will create a (for) loop looking through the list of files with a .txt extension. It will then replace each .txt extension with .pdf. Finally, the loop will end (done).

If you want more advanced features, you’ll need to use the rename command, which we’re about to detail.

Rename files in Linux with the rename command

with the command rename, you will have a little more control. Many Linux configurations include it by default. But, if you don’t have it installed, you can do it in just a minute with a simple command.

See also  How to use the Linux Ping command

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

sudo apt install rename

On the other hand, if you’re using Cent OS 7 either RHEL:

sudo yum install rename

And if you are using ArchLinux:

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

Now, we can start using the command rename. In general, the basic syntax of the rename command looks like this:

rename ‘s/old-name/new-name/’ files

It may seem complex at first, but it is much easier than it seems.

In this example, we will create a new folder called filetorename and, using the touch command, we will create 5 files.

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

with the last command lsyou can see the files you created.

If we want to rename a single file called file1.txtthe line would look like this:

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

If we wanted to change the extension of all the files, for example, to .phpwe could do it this way:

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

We can also specify another directory where the files that you are going to rename are.

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

We would like to mention that rename uses a regular expression of , which means that this command has wide possibilities.

Finally, it’s a good idea to review all command options. You can see them in the terminal by running:

rename –help

Some common examples of how to use the command rename:

  • Convert filenames to uppercase: rename ‘y/az/AZ/’ *
  • Convert filenames to lowercase: rename ‘y/AZ/az/’ *
  • Replace spaces in filenames with underscores: rename ‘y/ /_/’ *

remove rename Command

If you no longer wish to have rename installed on your system, remove it with the software manager. Or from the terminal.

See also  How to make a professional portfolio (in 6 simple steps)

For Debian, Ubuntu, Linux Mint and derivatives:

sudo apt remove rename

And to CentOS Y RHEL:

sudo yum remove rename

That’s it, rename will be removed from your Linux machine.

conclusion

Renaming files in Linux using the terminal is a simple and practical task, and sometimes it is very important. Every server administrator should know how to do it.

As we have seen, there are two commands that can do this task. One is simpler than the other, but both get the job done.

Keep researching these commands and improving the quality of your daily work.

Gustavo is passionate about creating websites. He focuses on the application of SEO strategies at for Spain and Latin America, as well as the creation of high-level content. When he is not applying new WordPress tricks you can find him playing the guitar, traveling or taking an online course.

Loading Facebook Comments ...
Loading Disqus Comments ...