How to Rename a Directory in Linux

Paul Hill

January 13, 2024 • 8 min read

    Renaming a directory in Linux is a straightforward task, achievable with a simple command. If you’re using Ubuntu or any other Linux distribution, you can rename a directory by using the mv (move) command. This command is not just for moving files and directories but also effectively renames them. Here’s how you do it:

    How to Rename a Directory in Linux

    You can rename a directory is by using the mv old-name/ new-name/ command. The syntax for the mv command looks like this:

    mv /path/to/old/ /path/to/new/
    Rename a Directory in Linux
    Rename a Directory in Linux

    Of course you are moving a file, which is the same thing as renaming in Linux.

    Take your Linux skills to the next level!

    Our Linux Fundamentals course will take your bash skills to the next level! Check it out by clicking the course below:

    Course: Linux Fundamentals

    This course is a comprehensive course designed to guide learners through the vast landscape of Linux…

    51 Lessons
    4 Quizzes
    2 Labs
    5.5 Hr

    Why We Use the Move Command to Rename Directories

    In Linux, the mv (move) command is the go-to utility for renaming directories, and understanding its use is key to mastering file system management. At first glance, it might seem odd to use a command named ‘move’ for renaming. However, the logic behind this becomes clear once you grasp how Linux handles files and directories.

    Linux views renaming as a form of moving an item from one name to another within the same directory. Essentially, when you rename a directory, you’re moving it from its old name to a new one within the same location in the file system. This conceptualization aligns with Linux’s philosophy of keeping commands simple but versatile. The mv command is designed to not only move files and directories from one location to another but also to rename them by moving them within their current directory.

    Moreover, this approach minimizes the number of commands a user needs to remember, streamlining the user experience. Rather than having separate commands for moving and renaming, the mv command elegantly handles both tasks. It’s an excellent example of the efficiency and practicality at the heart of Linux systems.

    Rename a Directory Using the Rename Command

    While the mv command is suitable for straightforward renaming tasks, the rename command in Linux offers more flexibility, especially when you need to rename multiple directories or files according to a pattern. This command is particularly powerful when dealing with batch operations and regular expressions.

    To install the rename command in Ubuntu, follow these steps:

    sudo apt install rename

    Verify the installation. After installation, you can verify that rename is correctly installed by typing:

    rename --version

    While the mv command is suitable for straightforward renaming tasks, the rename command in Linux offers more flexibility, especially when you need to rename multiple directories or files according to a pattern. This command is particularly powerful when dealing with batch operations and regular expressions.

    To use the rename command, you should first understand its syntax and options. Here’s a basic structure of the rename command:

    rename 's/oldname/newname/' *

    This command will search for files and directories matching the pattern oldname in the current directory and replace it with newname. The s stands for “substitute”. It’s a part of the Perl regular expression syntax, which the rename command utilizes.

    For example, if you have multiple directories named project-v1, project-v2, etc., and you want to rename them too project-v1-old, project-v2-old and so on, you can use the following command:

    rename 's/project-/project-old/' project-*

    This command searches for every directory that starts with project- and adds -old to their names.

    Keep in mind that the rename command is powerful and should be used with caution. Always double-check the patterns you’re using to avoid unintended renaming of files and directories.

    Rename a Directory with the Find Command

    In addition to the mv and rename commands, the find command in Linux offers a versatile way to search for directories and files based on criteria like name, size, or modification date. It can also be combined with other commands to perform actions on the found items, such as renaming directories.

    Here’s how you can use the find command to rename directories:

    Basic Syntax of the Find Command: The find command allows you to locate files and directories based on specific criteria. The basic syntax is:

    find [path] [expression]

    Combining Find with Exec for Renaming: To rename directories, you can combine find with the mv command using the -exec option. For example, if you want to rename all directories named oldname to newname within your current directory, you can use:

    find . -type d -name 'oldname' -exec mv {} newname \;

    This command searches (find) in the current directory (.) for directories (-type d) with the name oldname, and for each found item ({}), it executes (-exec) the mv command to rename it to newname.

    1. Careful with Naming Conflicts: It’s important to ensure that the new directory name (newname in our example) does not already exist in the same path. If it does, the mv command will return an error.
    2. Renaming with Patterns: If you need to rename directories following a pattern, you will need to integrate a shell loop or use a command like awk or sed to process each directory name. This approach is more complex but offers greater flexibility for bulk renaming tasks.

    Using the find command for renaming can be particularly useful when dealing with a large number of directories or when the directories are spread across different subdirectories. It allows you to precisely target which directories to rename based on their location and other attributes.

    Renaming Directories Using the mmv (Multiple Move) Command

    Another efficient method for renaming directories in Linux, particularly suitable for bulk operations, involves using the mmv (Multiple Move) command. This command is not installed by default in most Linux distributions, but it can be easily added and offers a flexible way to rename multiple files or directories at once based on patterns.

    Here’s how to use the mmv command for renaming directories. First, you need to install mmv. In Ubuntu, you can do this by executing:

    sudo apt install mmv
    1. This will download and install the mmv package.

    Understanding mmv Syntax:

    • The mmv command uses a pattern matching and replacement syntax. The basic format is:bash
    mmv [options] source_pattern destination_pattern
    • Where source_pattern is the pattern matching the current names of the files or directories, and destination_pattern is the pattern for the new names.

    Renaming Directories with mmv:

    • For example, if you want to rename all directories starting with project to start instead with archive, the command would be:bash
    mmv "project*" "archive#1"
    • In this command, * is a wildcard that matches any character, and 1 in the destination pattern refers to the first wildcard match in the source pattern.

    Safety and Testing:

    • One of the advantages of mmv is its safe-mode feature. By running it with the -n option, you can see what changes it would make without actually performing them:bash
    mmv -n "project*" "archive#1"
    • This will display the renaming that would occur, allowing you to verify the results before executing the command.

    The mmv command is a powerful tool for batch renaming operations in Linux. It’s particularly useful when you need to rename a large number of directories according to a specific pattern, offering a blend of flexibility and safety.


    Here are the key takeaways from this article are:

    • Simplicity with mv: The mv command is your quick go-to for simple renaming tasks.
    • Pattern Power with rename: For pattern-based renaming, the rename command offers flexibility, especially useful in batch operations.
    • Precision with find: Combine find with mv when you need precise control over which directories to rename, particularly in nested directory structures.
    • Batch Renaming with mmv: The mmv command shines in bulk renaming operations, providing a safe and efficient way to handle multiple directories simultaneously.

    Renaming directories might seem like a basic task, but as you’ve seen, there’s a range of methods to suit different needs and scenarios. Understanding these tools and techniques is essential for efficient file management and can greatly enhance your proficiency in Linux.

    We encourage you to experiment with these methods, find what works best for your needs, and become even more adept at navigating the Linux file system.

    And remember, whether you’re a beginner looking to strengthen your foundational skills or a seasoned professional seeking to refine your command-line capabilities, continuous learning is key. For structured and comprehensive IT training, don’t forget to check out Server Academy. Sign up for free and start your journey to becoming a Linux command line expert.

    We’d love to hear your thoughts and experiences with renaming directories in Linux. Share your insights, tips, or questions in the comments below!


    Want to improve your IT skillset? Start with a free account and get access to our IT labs!


    Sign up free and start learning today!

    Practice on REAL servers, learn from our video lessons, interact with the Server Academy community!