Javatpoint Logo
Javatpoint Logo

Rename Folder in Linux

In Linux, the renaming process of a folder or directory is not done with a traditional rename command; instead it is done through the 'mv' command. The 'mv' command is a multi-purpose command. It is not just limited to move files and directories, but it can also be used for renaming the files and directories.

It is important to keep the file system structured to ease access to the data. Sometimes, we create some temp files, and later we need to rename them. In such cases, it is a handy tool.

However, the directories can be renamed using various commands and utilities such as mv command, find command, rename command, using Bash, and more.

Let's have a look at the following techniques of renaming directories:

Renaming directories using mv command

Basically, the mv command is used to move files, but we can also rename the folders and directories by it. We can simply rename the folders by executing the mv command, followed by the old folder name and new folder name, respectively.

For example, to rename a folder named as 'Old_folder' to 'New_folder,' execute the command as follows:

The above command will rename the folder.

Execute the ls command to list all the available files and folders in your current working directory:

Consider the below output:

Rename Folder in Linux

Renaming directories using find command

In some cases, we don't know exactly where the required directories are located. The find command assists us in finding and locating the directories in the Linux system.

To find the directories use the find command with the 'type' option to look for directories from the file system. We can rename them by executing the mv command with the '-execdir' option.

For example, to rename the 'New_folder' directory, execute the below command to find it:

The above command will locate the directory from the file system. If you don't remember the exact directory name, you can type the matching directory name. Consider the below output:

Rename Folder in Linux

Now, to rename the directory, execute the mv command with '-execdir' option as follows:

The above command will rename the directory as 'Directory'. We can list the files and directories using ls command as follows:

Consider the below output:

Rename Folder in Linux

Renaming directories using the rename command

The rename command is a built-in utility to rename the files and directories for the most Linux distribution. However, it may not be directly available for all the Linux distribution.

Instead of the mv command, we can rename files using the rename command. It allows us to rename multiple files and directories. We can rename multiple directories together, such as rename all the text directories into any other format, rename all the directories which are in lowercase to uppercase, and more.

Syntax:

In order to use the rename, we must have it on our machine. If it is not installed, it will display the output as follows:

Rename Folder in Linux

To install the rename, execute the command as follows:

It will start a daemon process and install the rename configurations on our machine. Consider the below output:

Rename Folder in Linux

The rename configurations are successfully installed on our machine. Now we will use the rename command. Consider the following examples:

Example1: If we want to rename our directories written in uppercase to lowercase. To display the directories, execute the ls command as follows:

the above command will display all the directories from the current working directory. Consider the below output:

Rename Folder in Linux

Now, perform the rename operation, execute the below command:

The above command will rename all the directories from uppercase to lowercase.

To verify the operation, list all the directories by executing ls command:

Consider the below output:

Rename Folder in Linux

As from the above output, all directories are renamed as in lowercase.

Example2: Rename all text files into pdf files.

We can rename all '.txt' extension file as '.pdf' extension. We have the following text files in our current working directory:

Rename Folder in Linux

To rename all text files as pdf files, execute the command as follows:

The above command will rename all the text files into pdf files. Consider the below output:

Rename Folder in Linux

Renaming directories using the Bash script

we can rename files and directories using the Bash scripting language. In order to rename multiple directories by using a Bash script, create a new script, and use an 'mv' command with for loop. let's understand the following example:

First, create some text files which can be renamed later. To create 10 files at once, execute the below script:

The above script will create 10 text files. Consider the below commands:

Rename Folder in Linux

To verify whether the files are created or not, execute the below script:

The above script will display all the created files. Consider the below output:

Rename Folder in Linux

Now create a variable 'newfile,' execute the below script:

The above script will create a variable 'newfile' and replace all the '.txt' extension with '.zip' extension. The echo command will display the variable values. Consider the below output:

Rename Folder in Linux

Now, we can rename all the files using the mv command. To rename files with mv command using a bash script, execute the below script:

Consider the below script:

Rename Folder in Linux

The above script will convert all the text files into zip files. To list the files, execute the ls command as follows:

The above command will list the files with time and other specified options. Consider the below command:

Rename Folder in Linux

Getting Help

If you stuck during the use of the rename command, you could get help from the command line by executing the below command:

The above command will display all the available options that can be used with the rename command. Consider the below output:

Rename Folder in Linux

As from the above output, we can see that the options are displayed with their usage.

Also, we can read the manual by executing the below command:

The above command will display the manual on your terminal. Consider the below output:

Rename Folder in Linux

Scroll the above manual to read more. To exit from the terminal window, press the 'q' key.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA