Renaming or moving folder and directories can be complicated if those folders and directories have some subfolders.
There’s absolutely not any rename function in Ubuntu Linux.
Rather, you merely move the document, giving it a new name. If you do not really mv it to a different directory, then you’ve effectively renamed it:
Renaming directories on Linux isn’t done with a specific “rename” or “ren” etc. command but with a command which serves multiple functions: the “mv” command.
The mv is the brief form for the “move”. We can simply rename by providing the current directory and folder name and destination folder or directory name. If the origin or directory has files we will need to rename by using the recursive move command. This is only going to change the given folder or directory name but transfer all subfolder and files.
Rename Directories on Linux with mv
To rename a directory on Linux, use the “mv” command and specify the directory to be renamed in addition to the destination for your directory. The general syntax of mv command is:
$ mv source_folder new_directory
An example command of renaming folder
In this case, we’ll rename the directory called curr_data into new_data.
$ mv curr_data new_data
Rename Directories using the find command
In some instances, you might not know exactly where your directories can be found on your system that you want to rename.
Fortunately for you, there’s a command that can help you find the directories on a Linux system.
This is called the “find” command.
So as to locate and rename directories on Linux, use the “find” command with the “type” option so as to search for directories. After that, you can eliminate your directories by implementing the “mv” command with the“-execdir” alternative.
The general way of using the find command is:
$ find . -depth -type d -name <source_directory> -execdir mv {} <target_directory> \;
An example of find command to rename
For this example, suppose that you need to rename a directory starting with “data” in your filesystem to “backupData”.
The first part of the command will find where your directory is located.
$ find . -depth -type d -name “data”
./data
Now you know where your directory is, you can rename it by using the “execdir” option and the “mv” command.
$ find . -depth -type d -name temp -execdir mv {} backupData \;
Overwrite Forcibly If Exists
In some instances, there may be an existing directory or folder with the new name. We will need to confirm the overwrite. But this may be a daunting task if there’s a great deal of them. We can overwrite existing files and folder with -f option automatically. -f means by force.
$ mv curr_data new_data
Rename Directories using rename
Rather than using the “mv” command, you may use a dedicated built-in command, however this command might not be directly available in your distribution.
So as to rename directories on Linux, use “rename” with how you want the files to be renamed in addition to the target directory.
The general way of using rename command is:
$ rename <expression> <directory>
The example command below shows how to rename all directories that have names in uppercase letters to lowercase letters:
Notice the expression:
$ rename ‘y/A-Z/a-z/’ *