You can rename any directory on your Linux machine using the mv command. However, mv stands for the move but the command can rename a file or directory.
Renaming a directory of file in Linux is one of the basic file operations. If you are a Linux user you must know such basic things like copying files, Removing directories, creating files etc.
In such a case, if you don’t know how to perform such basic orations then we have some useful posts for you.
Learn How to create a File on Linux
Learn How to Remove Directory on Linux
Learn How to Copy Directory on Linux
Well, lets talk about how to rename directory on Linux.
Table of Contents
How to Rename Directory on Linux
On Linux operating system, Mainly there are two ways of renaming directory. These two ways are-
- Renaming Linux Directories using Command Line Interface.
- Renaming Linux Directories using Graphical user Interface.
No matter which option you want to use but the both processes are very easy and simple.
You can use mv command on any Linux distribution to rename of move directory from one location to another location.
So what is the syntax of renaming files on Linux?
Well, here it is-
$ mv <old_name> <new_name>
In order to rename a directory in Linux you will have to use the mv command by placing old directory name and after a blank space you will have to place the new name of the directory.
Let suppose, we have a “Demo Project” directory on the Desktop of our computer. And we want to rename it as Final Project.
You can see in the Image that we have “Demo Project” directory on the desktop of computer. Now we will rename it as Final Project.
In the image you can see, I have run mv command in order to change the name of the directory. Now, you can guess what would be the result?
This is the result of after running mv command. You can see Demo Project has got disappeared from the list and there is a Final Project Directory in the list.
Note: Demo Project has got disappeared from the list it does not mean that data from the directory has also got disappeared. But the data that was stored in Demo Project is now in Final Project Directory.
So this is how you can rename directory in Linux using command line interface.
Note: mv command is also used to move the file from one location to another location.
Rename and Move Files Using mv Command
There is the multiple use of mv command in Linux. As you learned how to rename directory in Linux using mv command. Now, let’s see how to use the same command to move the file from one location to another location.
Syntax of moving files from one location to another using mv command-
$ mv file_name Path
Let’s take an example-
Suppose, we have a file named ‘Hello’ which in the Home directory of our computer. And we want to move it from our home directory to Documents directory.
Now in order to move Hello file from the Home Directory to Documents directory, we will run the following commands-
$ mv Hello Documents/
if you get any kind of permission error then you can use sudo. Let’s see how-
$ sudo mv Hello Documents/
As soon as you run this command your file will be moved quickly.
You can check it in the new directory by running ls or ls -l command.
In this tutorial, you learnt how to easily change the name of directory using a very simple mv command. However, there are many more ways to rename files in Linux using the command line interface.
Also, there are some more ways to change the name of directory in Linux but that methods are not easy to implement.
How to Rename Multiple Directory or Files at Once In Linux
You can rename multiple files on Linux using bash with the help of for loop or while loop.
Such loops are useful only when you want to change the extension of a file or want to rename file only for making Uppercase character from Lowercase, or Lowercase to Uppercase in File name.
But when it comes to renaming multiple Directory names in Linux such methods are not so useful.
However. if you want to change the multiple file extension then you can learn Bash scripting. We have also tutorials on our website related to Bash you can learn from here or you you can find lots of bash tutorials on the internet.
Renaming Multiple files Using Rename command
There is also rename command that can be used to rename multiple files on Linux. But if you are thinking that you can completely change the file name using rename command then you are wrong at this place.
Rename command is only useful in some cases.
Suppose, you have a directory with bunch of files in it. Some files name in the directory are capitalised and some are in lowercase. Now you want all capitalized file names to be in lowercase.
So, in such a case rename command is the great idea.
Let’s see how to perform such operation-
Install rename tool for your Linux Destro
Before you can use this tool you will have to install it into your machine. For the different Linux Distribution there is a different way to install it.
Installing rename tool in Ubuntu-
$ sudo apt install rename
Installing rename in Arch Linux-
$ yay perl-rename ## or yaourt -S perl-rename
Installing rename tool on RedHat, Fedora or CentOS–
$ sudo yum install prename
After Installing rename tool follow the steps below-
Step 1 – Open the directory in which files are located.
Step 2 – Run the following command-
$ rename 'y/A-Z/a-z' *
Step 3 – Operation Completed. Now you don’t have to do anything.
In this tutorial post you learned how to rename directory on Linux. We tried to cover all the use of mv command. Basically, mv command is used to move file from one location to another location but this command can also be used to rename files and directories.
In this single post we covered 3 things. Renaming files using mv command, Moving files using mv command and renaming multiple files using rename command.
However, rename tool is not so useful it can only change the case of character. Especially, it is useful only if you want to make file names in lowercase.
Bash can also be used to change the directory and file names on Linux. We have different tutorial post for that on website.
If you are facing any kind of issue while using the command that we have covered in this post then feel free to ask for a solution using our comment box.