TL;DR
To rename a directory in Linux, you can try any of the four methods:
- mv Command: Rename a directory by navigating to its parent directory and executing mv old_dir_name new_dir_name, where you replace old_dir_name with the current name and new_dir_name with the desired name.
- rename Command: Execute rename ‘s/old_dir_name/new_dir_name/’ old_dir_name within the parent directory to batch rename a directory, substituting old_dir_name with the current name and new_dir_name with the new name.
- find Command: Use find /path/to/search -type d -name “old_dir_name” -exec mv {} new_dir_name \; to locate and rename a directory, specifying the path to search, the current directory name, and the new name.
Read our article below to learn more about how to easily and efficiently rename a directory in Linux.
Renaming directories in Linux can feel challenging, especially with complex structures. However, several effective methods can simplify your workflow and keep your system organized. This post guides you through step-by-step methods for renaming directories, troubleshooting common errors, and using best practices. These skills will help you manage your Linux system more efficiently, making navigation and maintenance easier.
Why Rename Directories?
Renaming directories in Linux is important for several reasons. Here are some common scenarios and benefits of renaming directories:
- Organizing Files: Renaming directories helps you keep your files organized. For example, changing a generic name like
folder1
to something more specific likeproject_reports
makes it easier to find what you need later. - Clarity and Consistency: Descriptive and consistent directory names help everyone understand the contents at a glance. If you work in a team, using clear and consistent names avoids confusion and makes collaboration smoother.
- Reflecting Changes: As projects evolve, directory names might need updating to reflect their current state. For instance, a directory named
drafts
might be renamed tofinal_reports
once the documents inside are finalized. - Compliance and Standards: In some environments, adhering to naming standards is crucial for compliance and regulation. Renaming directories to follow these standards ensures that your file system meets organizational or legal requirements.
- Automation and Scripting: Clear and logical directory names make writing automation scripts easier. If your scripts need to process specific directories, using well-defined names reduces errors and makes the scripts more maintainable.
- Avoiding Conflicts: Renaming directories helps avoid conflicts when combining multiple directories or migrating data. If two directories have the same name, renaming one prevents overwriting or merging issues.
How to Rename a Directory in Linux
To rename a directory in Linux, you can use several methods. The mv
command lets you rename by executing mv old_dir_name new_dir_name
. The rename
command is for batch renaming, using rename 's/old_dir_name/new_dir_name/' *
.
The find
command combined with mv
can rename directories with find /path/to/search -type d -name "old_dir_name" -exec mv {} /new/path/new_dir_name \;
. A bash script can automate renaming with a loop, creating and running a script with mv "$dir" "${dir/old_directory_prefix/new_directory_prefix}"
.
Here’s the step-by-step guide for above mentoned four methods and two more methods to rename a directory:
1. mv Command
The mv
command is used to move or rename files and directories in Linux. To rename a directory in Linux using the mv
command, follow these steps:
- In the Terminal window, navigate to the directory containing the directory you want to rename using the
cd
command. - Type the following command:
mv old_dir_name new_dir_name
Replace old_dir_name with the current name of the directory and new_dir_name with the new name you want to give to the directory.
- Press Enter to execute the command. Here is an example of renaming a directory called text to doc using the
mv
command:

2. rename Command
The rename
command is specifically designed for batch renaming files and directories. Here’s how you can rename a directory in Linux using the rename
command:
- Open the Terminal app and use
cd
command to head to the location containing the directory you want to rename. - Type the following command:
rename 's/old_dir_name/new_dir_name/' *
Replace old_dir_name with the current name of the directory and new_dir_name with the new name you want to give to the directory.
- Press Enter to execute the command.
- Here is the output of renaming a directory called files to docs using the
rename
command:

3. find Command
The find command is a powerful tool to search for files and directories, which can be combined with mv for renaming.
- Open your terminal.
- Use the find command to locate and rename the directory:
find /path/to/search -type d -name "old_directory_name" -exec mv {} /new/path/new_directory_name \;
This command searches for directories named old_directory_name and renames them to new_directory_name.

4. Using a Bash Script for Batch Renaming
Shell scripts can be used to automate the renaming of multiple directories, especially useful for complex renaming patterns.
- Access your command window and create a new bash script file:
nano rename_directories.sh
This command opens a new file named rename_directories.sh in the nano text editor.

- Write the script to batch rename directories:
#!/bin/bash
for dir in old_directory_prefix*; do
mv "$dir" "${dir/old_directory_prefix/new_directory_prefix}"
done
This script renames all directories starting with old_directory_prefix to new_directory_prefix.

- Save and exit the nano editor by pressing CTRL + X, then Y, and Enter.

- Make the script executable:
chmod +x rename_directories.sh
This command makes the rename_directories.sh script executable.

- Now run the script:
./rename_directories.sh
This command executes the rename_directories.sh script to batch rename the directories.

5. Wildcard Pattern
The mv
command is a simple and effective tool for renaming directories in bulk. You can use a wildcard pattern to match all directories that you want to rename and specify the new name for each directory. Here’s how you can do it:
- Launch the Terminal application on your Linux system and execute the following command to change the directory to your desired location:
cd ~/directory_name
- List the contents of the directory using the
ls
command to verify the presence of the directories you want to rename.
ls
- To rename all directories with the prefix old_ to have the prefix new_, you could use the following command:
mv old_* new_*
- This command uses the old_* wildcard pattern to match all directories with the prefix old_, and the new_* pattern to specify the new prefix for each matching directory. The
mv
command will move each directory to a new location with the new prefix. Here is the output:

6. Regular Expression Pattern
The rename
command can also be used to rename directories in bulk, using regular expression patterns to match and replace parts of the directory names. For example, to rename all directories with the old_ to have the new_, you could follow these steps:
- Head to the Terminal app and navigate to the location where you want to rename the directory:
cd ~/directory_name
- To rename all directories at once, run the following command:
rename 's/old_/new_/g' old_*
- The regular expression pattern s/old_/new_/g is used to globally substitute all occurrences of the old_ prefix in a file or directory name with the new_ prefix. This pattern is commonly used in the
rename
command to rename files or directories in bulk. You’ll see the following output:

3 Potential Issues When Renaming a Directory in Linux
When renaming a directory in Linux, you may encounter three common issues or errors. To address these concerns, consider the following troubleshooting methods tailored to each specific error type:
1. Permission Denied Errors
If you get a “Permission denied” error when trying to rename a directory in Linux, it means that you don’t have the required permissions to perform the action. To fix this, you need to change the ownership or permissions of the directory. There are two ways to do this, which are:
- To change the ownership of the directory, use the
chown
command. For example:
sudo chown username:groupname directory_name
- To change the permissions of the directory, use the
chmod
command. For example:
chmod u+rwx,g+rwx,o+rwx directory_name
- Once you execute these commands, you can test whether the error has been resolved by attempting to rename the directory.
2. Directory Not Found Errors
If you get a “Directory not found” error when trying to rename a directory in Linux, it means that the directory you are trying to rename doesn’t exist or you are trying to rename it from the wrong directory. To fix this error, follow these steps:
- Ensure that you are in the correct directory by using the
cd
command.
cd ~/directory_name
- Next, make sure that you have entered the correct name of the directory you want to rename. To do that, you can execute the following command to view and confirm the directory name:
ls
- If the directory doesn’t exist, you may need to create the directory using the
mkdir
command and then try renaming the newly created directory.
mkdir directory_name
3. Conflicting Directory Names
If you encounter an error indicating that the new directory name you want to use already exists, you may need to delete the existing directory before proceeding with the renaming process. To do so, follow these steps:
- In the Terminal window, execute the following command:
rmdir directory_name
- Alternatively, you can run the following command:
rm -r directory_name
- Once done, use the following command to ensure the existing directory is successfully deleted from the current location. If so, try renaming the directory again.
ls
Best Practices to Rename Directories in Linux
Renaming directories in Linux can improve organization and clarity. Follow these best practices to ensure you rename directories effectively and avoid potential issues. Here are five key tips to keep in mind:
- 🛡️ Check for Conflicts Before Renaming: Ensure no other directory or file has the same name before renaming. Use commands like
ls
orfind
to check for existing names and avoid accidental overwrites or data loss. - 📚 Use Clear and Descriptive Names: Choose names that clearly describe the directory’s contents. Avoid vague names like
folder1
and opt for specific ones likeproject_reports
to make it easy to find and understand the contents. - 📅 Include Dates When Relevant: When dealing with time-sensitive data, include dates in your directory names. For example,
backup_2024_07_19
helps you quickly identify when the backup was created, making it easier to manage and track versions. - 🔄 Avoid Special Characters: Stick to letters, numbers, underscores, and hyphens. Special characters can cause issues with scripts and commands. For example, use
project_reports_2024
instead ofproject@reports!2024
. - 🧹 Keep Names Consistent: Follow a consistent naming convention throughout your system. If you use underscores, always use them. Consistency helps maintain an organized and predictable file system, making navigation easier.
To Sum Up
In this article, I’ve walked you through step-by-step methods to rename directories in Linux, including using commands like mv
and rename
, graphical file managers, and bash scripts. I’ve also covered how to troubleshoot common errors and shared best practices to ensure you rename directories effectively and avoid potential issues.
To expand your Linux knowledge further:
- Explore how to create a home directory for a user in Linux to effectively manage user environments and permissions.
- Check out methods to list only directories in Linux to help you quickly identify and manage directories in your system.
- Learn how to copy directories in Linux, which will provide you with the skills to duplicate directory structures and contents efficiently.
Frequently Asked Questions
What is the basic structure of directories in Linux?
Can I use wildcards when renaming directories?
mv
command in a for loop to iterate through and rename directories. In Windows, you can use the rename
or ren
command with a for /d
loop for similar results. However, be cautious when using wildcards, as they may lead to unintended consequences. Double-check your commands and consider backing up your data before making bulk changes.How can I use directories to organize files in Linux?
mkdir
command and move files and directories using the mv
command. It’s important to choose descriptive names for directories to make it easier to find and manage files.