Logo

How to Rename a Directory in Linux in Four Easy Ways

Try this guide with our instant dedicated server for as low as 40 Euros

rename a directory in linux

Even the new users know that directories are the primary working blocks of a Linux distribution. Each user gets their own home directory, and many system activities have dedicated directories.

As you can imagine, renaming a directory is a common activity admins perform for system maintenance and activity scheduling. Users often need to rename directories for better file management in their home directory.

This comprehensive tutorial will go into the details of how to rename a directory in Linux. We’ll cover four popular commands for renaming directories in a Linux environment.

Table Of Contents

  1. Prerequisites
  2. How to Rename a Directory in Linux in Four Easy Ways
    1. Method #1: Rename Directories With the mv Command
    2. Method #2: Rename Directories With the rename Command
      1. Install the rename Command
      2. Rename a Single Directory
      3. Rename Multiple Directories
      4. Change All Letters of the Directory Name to Lowercase
    3. Method #3: Renaming Directories With the find Command
    4. Method #4: Renaming Directories With Bash Scripts
  3. Conclusion
  4. FAQs

 

Let’s start with the prerequisites for running these commands.

Prerequisites

Before running these commands, make sure you have:

How to Rename a Directory in Linux in Four Easy Ways

Once you have the prerequisites, you can try out the following methods of renaming directories and subdirectories in Linux.

Method #1: Rename Directories With the mv Command

The mv command is primarily used to relocate files. However, it also allows the renaming of directories. You can conveniently change the names of folders by using the mv command. In the command syntax, you need to specify the old folder name and the new folder name consecutively.

The typical command syntax is as follows:

# mv [current directory name] [new directory name]

For instance, use the following command to rename a directory:

# mv old-dir new-dir

method 1 to rename a directory in linux

As you can see, the directory old-dir has been renamed to new-dir.

When the command executes successfully, you won’t see any output. That’s why we strongly recommend using the ls command to list the contents of the current directory and confirm that the target directory has been renamed.

Method #2: Rename Directories With the rename Command

The rename command is a native utility in most Linux distributions. As the name suggests, it is used to rename files and directories using regular expressions. The rename command is very versatile and can be used to rename multiple directories and change the case of the directory’s name.

The basic command syntax is as follows:

# rename [options] <expression> <replacement file>

Install the rename Command

There is a chance that the rename command might not be available on some Linux distributions. If that’s the case with your system, you need to install it before you can use it.

For this, run the following command:

For Ubuntu and Debian, run the command: sudo apt install rename

On CentOS and Fedora, employ the command: sudo yum install prename

If you are using Arch Linux, you can install it with sudo pacman -S rename

Rename a Single Directory

Run the following command to rename a single directory:

# rename -v 's/olddir/newdir/' *

method 2 to rename a directory in linux

As you can see, the directory olddir has been renamed to newdir.

Let’s run through the various components of this command:

  • rename: references the rename command.
  • -v: forces verbose output. This is optional.
  • s: Short for substitute, tells that the expression will be replaced with the replacement.
  • olddir: indicates the directory name you want to replace.
  • newdir: indicates the new directory name.
  • *: looks in the current location for all the matches.

Rename Multiple Directories

Let’s say you have several folders named in series Document1, Document2, and you want to rename them to Work1, Work2. You can use the rename command for this activity:

$ rename -v 's/Document/Work/' *

Rename Multiple Directories

This command will replace Document with Work in the folder names within the current working directory.

In addition to the basic syntax we discussed earlier, the rename command also offers the -y flag, which can be incredibly handy when you want to replace only a specific character in the directory name.

For example, the following command will replace the character o with in all the directory names in this path.

# rename -v ‘y/o/-/’ *

This command will replace Document with Work

Change All Letters of the Directory Name to Lowercase

There are times when you want to standardize directory names by converting them all to lowercase letters for consistency. The rename command offers a straightforward way of completing this task.

For instance, consider the following command:

$ rename -v 'y/A-Z/a-z/' *

When you run this command, it will go through all the directories in the current directory and change all letters in their names to lowercase.

Change All Letters of the Directory Name to Lowercase

Method #3: Renaming Directories With the find Command

The find command is primarily used to search through the directories to find files and directories based on specific rules you set. It’s pretty nifty for renaming directories in bulk.

The basic syntax of the find command is:

find . -depth -type d -name [current_name] -execdir mv {} [new_name] \;

Let’s apply this syntax to an example. Suppose you have a directory named OldStuff that you want to rename to NewStuff. For this, use the following command:

# find . -depth -type d -name OldStuff -execdir mv {} NewStuff \;

This command finds all directories with the name OldStuff in the current directory (represented by .) and renames them to NewStuff.

method 3 to rename a directory in linux

Method #4: Renaming Directories With Bash Scripts

Bash scripts are like little programs you can create to automate tasks on your computer, including renaming directories.

Let’s see a typical bash script that accomplishes this task with the mv command:

#!/bin/bash

# Searches for directories and renames them according to the specified pattern

for d in *

do

if [ -d "$d" ]

then

mv -- "$d" "${d}_$(date +%Y%m%d)"

fi

done

This bash script is designed to search for directories within the current working directory and rename them by adding a date suffix in the format “YYYYMMDD.” Let’s break down how it works:

  • #!/bin/bash: This line is known as a shebang and indicates that the script should be interpreted using the Bash shell.
  • The script uses a for loop to iterate through all items (files and directories) in the current directory. The for d in * line sets up the loop, where d is a variable that will represent each item.
  • The script checks if the current item d is a directory using the conditional statement [ -d “$d” ]. If it’s a directory, the script proceeds with renaming.
  • The mv command takes care of the renaming operation. It renames the directory $d by appending the current date in the “YYYYMMDD” format using $(date +%Y%m%d). The curly braces {} around d are used to ensure that the variable d is properly recognized in the string.
  • The if statement ensures only directories are renamed, and other items like files are ignored.

When this script is executed, each directory in the current directory will be renamed with its original name followed by the current date. For example, a directory named Folder1 will be renamed to Folder1_20231103 (if the script is run on November 3, 2023).

To use this script, save it to a file with the sh extension. Remember to give it execute permission with chmod +x scriptname.sh. You can run it with ./scriptname.sh in the directory where you want to perform the renaming operation.

method 4 to rename a directory in linux

This script is used to add timestamps to directories, making it easier to track when changes were made.

Conclusion

In Linux, renaming directories is a simple yet essential task for efficient file management. By using commands like mv and rename, you can easily rename directories individually or in batches. For more advanced tasks, Bash scripts and the find command provide powerful automation options.

RedSwitches, a trusted bare metal hosting provider, offers reliable server solutions that can enhance your Linux experience.

If you’re looking for a robust server for your Linux projects, we offer the best dedicated server pricing and deliver instant dedicated servers, usually on the same day the order gets approved. Whether you need a dedicated server, a traffic-friendly 10Gbps dedicated server, or a powerful bare metal server, we are your trusted hosting partner.

FAQs

Q. Can I undo a directory renaming operation in Linux?

Unfortunately, Linux’s mv and rename commands do not have a built-in undo feature. It’s a good practice to back up your data before executing the renaming operation.

Q. Why choose RedSwitches for hosting solutions?

RedSwitches offers reliable and robust bare metal hosting services, ensuring high performance, security, and scalability for your server needs. Their services are ideal for businesses and individuals looking for a top-tier hosting provider.

Q. Are there any risks involved in renaming directories in Linux?

Renaming directories can be safe, but be cautious when working with critical data. Mistakes may lead to unintended consequences, so always double-check your commands before executing them.

Q. How can I rename a directory within the same parent directory?

To rename a directory without moving it, use the mv or rename command followed by the current name and the new name, ensuring both are in the same parent directory.

Q. Can I move a directory to a destination directory and rename it at the same time?

Yes, the mv command allows you to specify both the target directory and the new name, effectively moving and renaming the directory in one operation.

Q. What happens to the destination files when a directory is renamed?

When a directory is renamed, the destination files remain within the directory; only the directory’s name is changed.

Q. How do I ensure that I don’t overwrite existing files in the destination location?

When using mv, add the -n option to prevent overwriting. For the rename command, you can use a regular expression that matches only the intended files.

Q. Is it possible to rename directories from different source locations in one command?

With the rename command, you can use wildcards and regular expressions to rename multiple directories from different source locations, provided the expression is crafted to match the intended paths correctly.

Q. How can I rename a directory in Linux using the mv command?

To rename a directory in Linux using the mv command, you need to open the Linux terminal and run the following command: # mv. Replace the pattern with the name of the directory you want to rename and with the new desired name for the directory.

Q. How can I rename a directory using the rename command in Linux?

Open the terminal and use the following syntax: # rename ‘s///’. Replace with the current name of the directory with the desired new name and with the name of the directory or file you want to rename.

Q. How do I rename multiple directories using the rename command in Linux?

To rename multiple directories using the rename command in Linux, you can specify a regular expression pattern that matches the directories you want to rename. For example, you can use the following syntax: rename ‘s///‘. Replace with the pattern that matches the old directory names, with the pattern for the new directory names and with the name of the directory or file containing the directories you want to rename.

Q. How can I rename a directory on Linux if I’m not sure where the directory is located?

If you are not sure where the directory you want to rename is located, you can use the find command to search for it. Once you locate the directory, you can then use the mv command to rename it.

Q. Can I rename multiple directories using a single command in Linux?

Yes, you can rename multiple directories using a single command in Linux. The rename command allows you to specify a pattern for matching the directories you want to rename and then rename them all at once.

Try this guide with our instant dedicated server for as low as 40 Euros