The terminal is an essential tool for administrating Linux servers. It provides Linux users some of the best productivity tools while saving your machine’s resources.
To effectively use the potential of Linux, you need to have strong knowledge of the fundamentals – simple commands, like renaming existing files and folders. In this tutorial, you’ll learn how to rename folders in Linux.
How to Rename Files in Linux with the mv Command
The mv command is one of the easiest to use. It can do two very basic but essential things when file handling on Linux. One is moving files from one location to another, the other – to rename one or more files through the terminal.
Let’s see how renaming files with mv works on Linux.
First, we access our server through the command line using SSH. Here’s a helpful tutorial, if you would like to learn more.
If we are using a local computer, instead of a server, then we have to open the terminal from the main menu.
Afterward, it is necessary to know how the mv command works. To do this, we run the following:
As we can see in the previous image, the basic use of the mv command is as follows:
mv [option] [SOURCE]...[DIRECTORY]
Here are some of the most popular mv options:
- -f – shows no message before overwriting a file.
- -i – displays warning messages before overwriting a file.
- -u – only move a file if it is new or if it does not exist in the destination.
- -v – show what the command does.
And the parameters are:
[SOURCE] – the source destination of the file
[DESTINATION] – the destination directory.
Rename File on Linux Using the mv Command
If we want to rename a file, we can do it like this:
mv file1 newnamefile1
Assuming we are located in the directory and there is a file called file1.txt and we want to change the name to file2.txt.
mv file1.txt file2.txt
As simple as that. However, if you want some advanced features, you’ll need to use the rename command, we’re about to cover.
Rename Files on Linux Using the Rename Command
The mv command is used both to move files to other locations and to rename a file. However, you can also use the rename command, that gives you a bit more control.
Many Linux distributions include it by default. If you don’t have it installed, you can do it in just a minute with a simple command.
In the case of Debian, Ubuntu, Linux Mint, and derivatives:
sudo apt install rename
On the other hand, we are using CentOS 7 or RHEL:
sudo yum install rename
After this, we can start using it. 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’s a lot simpler than it might seem.
In this example, we will create a new folder called filetorename, and using the touch command, we will create 5 files.
With the last ls command, you can view the files that you created.
If we want to rename a single file called file1.txt, the sentence would be like this:
rename ‘s/file1/newfile1/’ file1.txt
If we wanted to change the extension to all files, for example, to .php. We could do it this way:
rename ‘s/.txt/.php/’ *.txt
We can also specify another directory where the files you want to rename are.
rename ‘s/.txt/.php/’ FILE/PATH
We’d like to mention that rename uses a regular expression of perl, meaning this command has extensive possibilities.
Finally, it is a good idea to check all the command options. You can view them in the terminal by executing:
If you no longer wish to have rename installed on your system, remove it using the software manager. 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 removed from your Linux machine.
Renaming files in Linux using the terminal is a simple and practical task but sometimes very important. Knowing how to do it is something every server manager should know.
As we have seen, there are two commands that can do it. One is simpler than the other, but both accomplish the task.
We encourage you to continue researching these commands and improving the quality of your everyday workflow.