Renaming the files and directories is a frequent task that a user has to carry out. Fortunately, Linux has an effortless way of renaming files and directories directly from the terminal.

We will be discussing two Linux commands to rename the directories in Linux. The mv and rename commands.

Using mv Command To Rename a Directory in Linux

The mv command is one of the basic commands provided by Linux and all other Unix-like systems. mv is basically used to move the files from one location to the another. But it’s also widely used as a way to rename a files and directories as well.

Use the following syntax to rename a directory using mv.

Syntax:

mv [old_name_of_directory] [new_name_of_directory]

First, check the directories present in the current working directory using the ls command.

ls

Output:

gaurav@ubuntu:~/workspace$ ls -l
total 76
drwxr-xr-x 2 root root 4096 Sep  9 16:19 daa
drwxr-xr-x 2 root root 4096 Sep  9 16:20 dmta
drwxr-xr-x 2 root root 4096 Sep  9 15:19 pc
drwxr-xr-x 2 root root 4096 Sep  9 16:19 pmcd
drwxr-xr-x 2 root root 4096 Sep  9 16:19 qps
drwxr-xr-x 2 root root 4096 Sep  9 16:19 ssda

Now, we will rename the folder named daa to robot using the mv command.

Example:

mv daa robot

Then, check the list of directories again using the ls command to verify the new name of the directory.

gaurav@ubuntu:~/workspace$ ls
dmta  pc  pmcd  qps  robot  ssda

If you try to rename a directory to a name already used by another directory in the desired location, the directory with the old name will be deleted and over-written with the new one.

Example:

gaurav@ubuntu:~/workspace$ ls -l
total 76
drwxr-xr-x 2 root root 4096 Sep  9 16:19 dmta
drwxr-xr-x 2 root root 4096 Sep  9 16:20 pc
drwxr-xr-x 2 root root 4096 Sep  9 15:19 pmcd
drwxr-xr-x 2 root root 4096 Sep  9 16:19 qps
drwxr-xr-x 2 root root 4096 Sep  9 16:19 robot
drwxr-xr-x 2 root root 4096 Sep  9 16:19 ssda

From the above list, let’s work with the directories dmta, pc and qps.

Example Output:

gaurav@buntu:~/workspace$ mv dmta qps
gaurav@ubuntu:~/workspace$ ls
pc  pmcd  qps  robot  ssda
gaurav@ubuntu:~/workspace$

Here I tried to rename directory ‘dmta‘ as ‘qps‘. Here directory qps already existed but even then the overwriting prompt was not shown by the terminal.

Also, you can see that the directory named ‘dmta‘ has been deleted. This is the lacunae in using the mv command if there exists more than one directories with the same name.

To avoid such ambiguity we can use the rename command.


Using rename Command To Rename Directories

As already mentioned above, mv is a very basic command and also has some ambiguous behaviour. To overcome these shortcomings we can use the rename command to rename multiple files at once.

rename doesn’t come built-in with the Linux distribution. You will have to install it separately first. rename is available in two different versions. But their functioning will be the same in all the environments. The only difference will be with the commands to install the rename utility. Check them out below.

Install rename on Ubuntu and Debian distributions:

sudo apt-get install rename

Install rename on Fedora, CentOS, and RedHat distributions:

sudo dnf install prename

After installing rename on your Linux computer, use the code below rename a single directory.

Syntax:

sudo rename [perl expression] [directory]

Example:

We will try renaming a single directory first using rename command. We will rename the directory named dir2 as ssh.

Output:

root@ubuntu:~# ls
DIR1  dir2  dir3  dir4  dir5  pc  snap
root@ubuntu:~# rename 's/dir2/ssh/' dir2
root@ubuntu:~# ls -l
total 28
drwxr-xr-x 2 root root 4096 Sep  9 15:15 DIR1
drwxr-xr-x 2 root root 4096 Sep  9 15:15 dir3
drwxr-xr-x 2 root root 4096 Sep  9 15:15 dir4
drwxr-xr-x 2 root root 4096 Sep  9 15:15 dir5
drwxr-xr-x 2 root root 4096 Sep  9 15:19 pc
drwxr-xr-x 3 root root 4096 Sep  9 14:59 snap
drwxr-xr-x 2 root root 4096 Sep  9 15:15 ssh

From the above output we can see that directory named ‘dir2‘ is renamed as ‘ssh‘ using the rename command.

Rename Multiple Directories at Once Using rename Command

Suppose I have two directories, ‘game‘ and ‘work‘. Both the directories are named in lower case. I’ll change the names of these directories using the rename command to upper case letters.

Example:

sudo rename 'y/a-z/A-Z/' [directories_to_rename]

Output:

Checking the list of current directories using ls command.

root@ubuntu:~# ls -l
total 36
drwxr-xr-x 2 root root 4096 Sep  9 15:15 DIR1
drwxr-xr-x 2 root root 4096 Sep  9 15:15 dir3
drwxr-xr-x 2 root root 4096 Sep  9 15:15 dir4
drwxr-xr-x 2 root root 4096 Sep  9 15:15 dir5
drwxr-xr-x 2 root root 4096 Sep  9 15:31 game.sql
drwxr-xr-x 2 root root 4096 Sep  9 15:19 pc
drwxr-xr-x 3 root root 4096 Sep  9 14:59 snap
drwxr-xr-x 2 root root 4096 Sep  9 15:15 ssh
drwxr-xr-x 2 root root 4096 Sep  9 15:30 work.sql

Using the rename command to change the names of the highlighted directories.

root@ubuntu:~# sudo rename 'y/a-z/A-Z/' *.sql

Checking the output with ls command.

root@ubuntu:~# ls -l
total 36
drwxr-xr-x 2 root root 4096 Sep  9 15:15 DIR1
drwxr-xr-x 2 root root 4096 Sep  9 15:31 GAME.SQL
drwxr-xr-x 2 root root 4096 Sep  9 15:30 WORK.SQL
drwxr-xr-x 2 root root 4096 Sep  9 15:15 dir3
drwxr-xr-x 2 root root 4096 Sep  9 15:15 dir4

Here we can see that we have changed the directory names from lower case to upper case.

Example 2:

In this example we will try to rename the part of multiple files simultaneously.

Let us list the directories first.

root@ubuntu-s-1vcpu-1gb-blr1-01:~# ls -l
total 56
drwxr-xr-x 2 root root 4096 Sep  9 15:15 DIR1
drwxr-xr-x 2 root root 4096 Sep  9 15:31 GAME.SQL
drwxr-xr-x 2 root root 4096 Sep  9 15:30 WORK.SQL
drwxr-xr-x 2 root root 4096 Sep  9 15:50 dir1.sql
drwxr-xr-x 2 root root 4096 Sep  9 15:50 dir2.sql
drwxr-xr-x 2 root root 4096 Sep  9 15:15 dir3
drwxr-xr-x 2 root root 4096 Sep  9 15:15 dir4
drwxr-xr-x 2 root root 4096 Sep  9 15:15 dir5
drwxr-xr-x 2 root root 4096 Sep  9 15:19 pc
drwxr-xr-x 2 root root 4096 Sep  9 15:54 sheldon1
drwxr-xr-x 2 root root 4096 Sep  9 15:54 sheldon2
drwxr-xr-x 2 root root 4096 Sep  9 15:54 sheldon3
drwxr-xr-x 3 root root 4096 Sep  9 14:59 snap
drwxr-xr-x 2 root root 4096 Sep  9 15:15 ssh

Running the rename commadn as rename -v so that we can see the changes made as output.

root@ubuntu:~#rename -n -v sheldon sheldonEPQ sheldon?
'sheldon1' -> 'sheldonEPQ1'
'sheldon2' -> 'sheldonEPQ2'
'sheldon3' -> 'sheldonEPQ3'
root@ubuntu:~# ls -l
total 56
drwxr-xr-x 2 root root 4096 Sep  9 15:15 DIR1
drwxr-xr-x 2 root root 4096 Sep  9 15:31 GAME.SQL
drwxr-xr-x 2 root root 4096 Sep  9 15:30 WORK.SQL
drwxr-xr-x 2 root root 4096 Sep  9 15:54 sheldonEPQ1
drwxr-xr-x 2 root root 4096 Sep  9 15:54 sheldonEPQ2
drwxr-xr-x 2 root root 4096 Sep  9 15:54 sheldonEPQ3

In this example we have changed the part of the name of multiple files simultaneously.


Conclusion

In this tutorial, we specifically learnt how to amend the directory names using the mv and rename command. We learnt to rename a single directory as well as multiple directories.