How to Rename a Directory in Linux

Hence, in this post, you will discover quick and easy commands you can use to rename a directory in the Linux terminal.

How to Rename a Directory in Linux

Renaming a directory is probably one of the most common and standard operation you can perform on any system. When getting started with the Linux terminal, learning how to create, copy, move, delete, and rename files and directory is one of the first steps.

Although you can rename files and directories using a GUI interface, we recommend embracing the chaos of the command-line.

Let's jump in.

Linux Rename Directory.

In Linux and Unix systems, we can rename a directory using the mv command. This command is also used to move files and directorys.

Check our tutorial on the mv command to learn more.

To rename a directory using the mv command, use the syntax as shown:

mv [options] [src] [dest]

Where src is the directory you wish to rename and the dest is the new name of that directory.

For example, suppose we have a directory called backups and we wish to rename that directory to backups_old. We can run the command as:

mv backups backups_old

Ensure that the old directory exists in the specified parth before renaming to the new name.

If the directory you wish to rename is not in the current path, you can use an absolute path as shown:

mv /home/ubuntu/backups /home/ubuntu/backups_old

The command will work in a similar manner whether using relative or absolute paths.

Rename Multiple Directories

If you need to rename more than one directory at once, you may need to do some bash tricks. For example, you can perform a simple scripts that looks for the target directories and assigns a new name to each.

Consider an example script shown below:

for dir in *; do
	if [-d "$dir"]; then
		mv -- "$dir" "${dir}.old"

The script above starts by creating a for loop to iterate over the directories in a given directory.

We then check if the file is a directory by using the -d parameter.

Finally, we rename the directories by adding a .old extension to the name.

Rename Directory with Rename Command

Yes there is a command in Linux called rename that allows us to rename a single or multiple directories.

NOTE: The rename command may not be readily available in your system. You can install by running the commands:

$ sudo apt-get install rename
$ sudo yum install prename
$ sudo pacman -S rename

With the commands used in Debian-based distros, REHL/CentOS/Fedora, and Arch-based bistro, respectively.

Rename Single directory using the rename command

Once you have the tool installed, you can rename a directory as:

rename [options] 's/[expression]/[replacement]/' [file name]

The commands will rename the directory by replacing the value of the expression parameter with that of the replacement parameter.

For example, to rename the backups directory to backups_old.

rename '/s/backups/backups_old'

The -s parameter tells the rename command that we wish to replace the expression with the replacement parameter.

Rename Multiple directories using the rename command.

Using the rename command, we can rename multiple directories at once without having to do any script magic.

The command is as shown:

rename -v 's/old_name/new_name/' *

The command will search the current directory for all matching directories and rename them to the specified new_name.

By default, the command will rename each folder by adding a numerical value. For example, the directories with take the format:

new_name1, new_name2, new_name3, etc.

You can also use the mv command with a bash script as shown:

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


In this post, we covered various methods techniques of renaming a single or multiple documents in Linux and Unix systems.

If you enjoyed this post, leave us a comment down below and share with your friends.

Until next time.

If you enjoy our content, please consider buying us a coffee to support our work:

Table of Contents
Great! Next, complete checkout for full access to GeekBits.
Welcome back! You've successfully signed in.
You've successfully subscribed to GeekBits.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info has been updated.
Your billing was not updated.