6 Best Ways to Rename a Directory in Linux

Written by

Reviewed by

Last updated: July 24, 2024

Expert verified

SVG Image

TL;DR

To rename a directory in Linux, you can try any of the four methods:

  1. 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.
  2. 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.
  3. 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 like project_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 to final_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:

  1. In the Terminal window, navigate to the directory containing the directory you want to rename using the cd command.
  2. 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.

  1. Press Enter to execute the command. Here is an example of renaming a directory called text to doc using the mv command:
mv command to move or rename files

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:

  1. Open the Terminal app and use cd command to head to the location containing the directory you want to rename.
  2. 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.

  1. Press Enter to execute the command.
  2. Here is the output of renaming a directory called files to docs using the rename command:
rename a directory in linux

3. find Command

The find command is a powerful tool to search for files and directories, which can be combined with mv for renaming.

  1. Open your terminal.
  2. 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.

locating and renaming directory using find command

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.

  1. 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.

creating bash script file with nano editor
  1. 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.

writing script to rename directories
  1. Save and exit the nano editor by pressing CTRL + X, then Y, and Enter.
saving and exiting the file
  1. Make the script executable:
chmod +x rename_directories.sh

This command makes the rename_directories.sh script executable.

making the script file executable
  1. Now run the script:
./rename_directories.sh

This command executes the rename_directories.sh script to batch rename the directories.

executing the script file to rename the directory

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:

  1. Launch the Terminal application on your Linux system and execute the following command to change the directory to your desired location:
cd ~/directory_name
  1. List the contents of the directory using the ls command to verify the presence of the directories you want to rename.
ls
  1. To rename all directories with the prefix old_ to have the prefix new_, you could use the following command:
mv old_* new_*
  1. 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:
renaming directories in bulk

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:

  1. Head to the Terminal app and navigate to the location where you want to rename the directory:
cd ~/directory_name
  1. To rename all directories at once, run the following command:
rename 's/old_/new_/g' old_*
  1. 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:
regular expression pattern with rename command

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:

  1. To change the ownership of the directory, use the chown command. For example:
sudo chown username:groupname directory_name
  1. To change the permissions of the directory, use the chmod command. For example:
chmod u+rwx,g+rwx,o+rwx directory_name
  1. 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:

  1. Ensure that you are in the correct directory by using the cd command.
cd ~/directory_name
  1. 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
  1. 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:

  1. In the Terminal window, execute the following command:
rmdir directory_name
  1. Alternatively, you can run the following command:
rm -r directory_name
  1. 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 or find 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 like project_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 of project@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:

Frequently Asked Questions

What is the basic structure of directories in Linux?

Linux follows a hierarchical structure where the root directory is at the top of the tree, and all other directories are its subdirectories. Currently, it has a root directory (/), home directory (~), system directories (/bin, /sbin, /usr, /var, etc.), temporary directories (/tmp), and configuration directories (/etc).

Can I use wildcards when renaming directories?

Yes, you can use wildcards when renaming directories, but the process depends on your operating system. In Unix-based systems (Linux, macOS), you can use shell features like globbing with the 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?

To organize files and directories in Linux, you can create new directories using the 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.

Ojash

Author

Ojash is a skilled Linux expert and tech writer with over a decade of experience. He has extensive knowledge of Linux's file system, command-line interface, and software installations. Ojash is also an expert in shell scripting and automation, with experience in Bash, Python, and Perl. He has published numerous articles on Linux in various online publications, making him a valuable resource for both seasoned Linux users and beginners. Ojash is also an active member of the Linux community and participates in Linux forums.

Akshat

Reviewer

Akshat is a software engineer, product designer and the co-founder of Scrutify. He's an experienced Linux professional and the senior editor of this blog. He is also an open-source contributor to many projects on Github and has written several technical guides on Linux. Apart from that, he’s also actively sharing his ideas and tutorials on Medium and Attirer. As the editor of this blog, Akshat brings his wealth of knowledge and experience to provide readers with valuable insights and advice on a wide range of Linux-related topics.

Share this article
Shareable URL
Prev Post

Top 9 Ways to View Linux File Content: A Comprehensive Guide

Next Post

How to Check Log4j Version in Linux [3 Best Ways]

Leave a Reply

Your email address will not be published. Required fields are marked *

Read next