Rename Folder in Linux

Rename Folder in Linux

In the Linux system, there is not any single dedicated command for renaming the files and directories. Mostly, the mv command is used extensively for the purpose of renaming and moving files.

Below we have listed some of the popular ways to rename any file and directory.

  • With the help of mv command
  • With the help of find command
  • With the help of rename command
  • With the help of bash script

Rename with the help of mv command

The actual working of the mv command is of moving files and directories. This command will stuff from one place to another, and we move our file in the same directory but with a new name. This process eventually leads to renaming the file or directory.

Example

In this example, we have to create a directory at the user's home directory and then rename that directory to a new name.

 #mkdir directory_created
#ls 
Rename Folder in Linux

Now, rename directory_created to directory_renamed.

#mv directory_created directory_renamed
Rename Folder in Linux

Hence the directory has been renamed successfully with the help of mv command.

Rename with the help of find command

The primary work of the find command is to search for files in the file system or directory hierarchy. Below we have explained the syntax of the find command to rename any directory.

Syntax  

#find . -depth -type d -name <InitialName> -execdir mv {} <FinalName> \

Example

Now, let us try the same example of directory_created and directory_renamed.

 #mkdir directory_created
#find . –depth –type d –name directory_created –execdir mv {} directory_renamed
#ls 
Rename Folder in Linux

Rename with the help of rename command

The rename command is not installed in the system binary, and you can install this using the below screenshot.

Rename Folder in Linux

The primary use case of the rename command is to rename multiple files at a time using the terminal. The rename command uses an expression to rename these multiple files.

Example

In this example, we have renamed all the files and the folders to the lower cases. You could even provide any specific file instead of the *, and the * is used to rename all the content present in the directory.

Rename Folder in Linux

Rename with the help of bash script

You can rename multiple files and directories by using a bash script. We are going to create a script that could find all the png files and rename them according to the serial numbers.

Example 1

First, we will create some .png files using the touch command. It is straightforward to use the touch command, and you can also create multiple files if assigned as arguments.

Rename Folder in Linux

Now, we have to rename these image files using a script. Let us code a script for this task. Open a text editor and name the script file as renameScript.sh.

 #!/bin/bash
i=1
for Old_name in *.png
do
mv $Old_name ${i}.png
echo $Old_name"----> "${i}”.png”
i=$(( i + 1 ))
done   

Now, execute the file and see the results.

Rename Folder in Linux

Hence all of the .png files have been renamed.

Example 2

We can also rename directories using the bash script, the approach will be the same, but some additional lines will be added.

First, we have created some demo directories using the mkdir command.

Rename Folder in Linux

Now, we have to create a script to rename these demo directories.

 #!/bin/bash
i=0
for Old_directory in *
do
          if [ -d "$Old_directory" ]
          then
          mv "${Old_directory}"  "d${i}"
          i=$(( i+1 ))
          fi
done 

The if condition is for confirming whether the variable is a directory or not.

Rename Folder in Linux