Change the Name of a Directory in Linux (rename a directory)

Last updated: January 4, 2023

Renaming a directory in Linux can be a useful way to keep your file system organized and maintainable. It can also help you to have the name more descriptive, change the case of the directory name, or correct a typo or mistake in the directory name.

In this tutorial, we learn how to rename a directory in Linux from the terminal.

Prerequisites

The command requires you to have the appropriate permissions to rename or move the directory. If you don't have the necessary permissions, you'll need to use sudo to run the command as the root user.

Using the mv command to rename a directory

The mv command is used to move and rename files and directories. To rename a directory using mv, you can specify the current name of the directory as the source and the new name as the destination.

Syntax:

mv [current-directory-name] [new-directory-name]

This will rename the directory "current-directory-name" to "new-directory-name". Remember destination should be the same path. If the destination path is a different location then mv performs move instead of rename.

Open the Terminal by pressing CTRL+ALT+T from the keyboard. Change the directory using cd into the respective directory where the directory or directories you wish to rename is present. You may use pwd command to check the path of your current directory.

Before renaming directories let's just list the content in the current working directory using the ls command :

ls

For example to rename the directory "dir-name-old" to "dir-name-old, type:

mv /home/bobbin/dir-name-old /home/bobbin/dir-name-new
rename a directory using mv command

This will rename the directory 'dir-name-old' to 'dir-name-new'. The mv command does not output anything. To verify you may use the ls command.

You can also use the -v option to print the names of the files that are being renamed, which can be useful for verifying that the correct files are being renamed.

Alternative methods for renaming a directory

Using rename command

The rename command is generally used to rename multiple files or directories. To rename a directory using the rename command, you can use the following syntax:

rename 's/dir-old-name/dir-new-name/' dir-old-name

Note that the rename command is not available on by default in many Linux Distributions. But you can easily install from the repository using the Distros-specific package manager.

For example, if you want to rename the directory dir-name-new to dir2, you can use the following command:

rename 's/dir-name-new/dir2/' dir-name-new
change directory dir-name-new to dir2 using rename command

This will rename the directory dir-name-new to dir2.

Find and Rename a Directory

If you're unaware of the location of the directory you wish to rename, use find and mv command to search the directory name and rename it.

Syntax:

find . -depth -type d -name [current-directory-name] -execdir mv {} [new-directory-name] \;

For example, the following command search for the directory name dir2 in the current directory and its subdirectories, then rename it to dir3.

find . -depth -type d -name 'dir2' -execdir mv {} 'dir3' \;
find the directory dir2 and rename it to dir3

This will find the directory dir2 and rename it to dir3. The dot in the find command indicates to search in the current directory and its subdirectories.

Conclusion

Renaming a directory from the terminal is pretty easy and time-saving. It can help you to avoid confusion or mistakes when working with your files.

Related Read: How to Rename a Directory with Spaces in Linux

About The Author

Bobbin Zachariah

Bobbin Zachariah

Bobbin started his career in IT in the year 2000 as a computer science instructor and worked as a Linux system engineer and senior analyst roles. Currently working in DevOps environments to increase efficiency and improve delivery time in AWS Cloud infrastructure. He is certified in RHEL, CCNA, and MCP and holds a Masters's in computer science. When in his free time love playing cricket, blogging, and listening to music.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment