How to Rename Directory in Linux (CLI) with Examples

Renaming directories is a common task for Linux users, whether for better organization or clarity. This guide will cover two primary methods: using the mv command for straightforward renaming and the rename command for more complex, bulk operations.

Using the mv Command

The mv command in Linux is primarily used for moving files and directories from one location to another. However, it can also be used to rename files and directories without changing their location. The mv command is a fundamental tool in the Linux command line toolkit and is available on all Linux distributions.

Basic Syntax of the mv Command

The basic syntax of the mv command for renaming a directory is straightforward:

mv [options] old_directory_name new_directory_name

Here:

  • old_directory_name is the current name of the directory.
  • new_directory_name is the new name you want to give to the directory.

Examples of Renaming Directories

To rename a directory from old_dir to new_dir, use the following command:

mv old_dir new_dir

This command changes the name of old_dir to new_dir in the same location.

Rename with Absolute Paths

If the directory is located in a different path, you can use absolute paths to specify the directory names:

mv /path/to/old_dir /path/to/new_dir

For example, to rename a directory from /home/user/old_dir to /home/user/new_dir, use:

mv /home/user/old_dir /home/user/new_dir

Handling Directory Names with Spaces

If the directory names contain spaces, you need to enclose them in quotes or use a backslash before the space:

mv "old dir" "new dir"

or

mv old\ dir new\ dir

Overwriting Existing Directories

If a directory with the new name already exists and you want to overwrite it, use the -f (force) option:

mv -f old_dir new_dir

Using the rename Command

The rename command in Linux is used to rename multiple files or directories by replacing or transforming portions of their names using regular expressions or patterns. It is particularly useful when you need to rename a large number of directories in a consistent manner.

Installing the rename Command

The rename command might not be pre-installed on all Linux distributions. You can install it using your package manager.

sudo apt-get install rename

Basic Syntax of the rename Command

The basic syntax of the rename command is as follows:

rename [options] 's/old_pattern/new_pattern/' directories

Here:

  • s/old_pattern/new_pattern/ is the substitution expression.
  • directories specifies the directories to be renamed.

Examples of Renaming Directories

To rename directories by replacing a specific pattern, use the following command:

rename 's/old/new/' old_directory_name

For example, to rename a directory from old_dir to new_dir:

rename 's/old/new/' old_dir

Renaming Multiple Directories

You can rename multiple directories that match a certain pattern:

rename 's/old/new/' old_dir*

This command renames all directories starting with old_dir to start with new_dir.

Using Regular Expressions

The rename command supports Perl-compatible regular expressions, allowing for complex renaming operations. For instance, to change the prefix dir_ to folder_ in all directories:

rename 's/^dir_/folder_/' dir_*

Case Insensitive Renaming

To perform case-insensitive renaming, use the -i option:

rename -i 's/old/new/' OLD_DIR*

This command renames directories ignoring case sensitivity.

Renaming with Confirmation

If you want to be prompted for confirmation before each rename, use the -v (verbose) option along with -i (interactive):

rename -vi 's/old/new/' old_dir*

Applying Complex Transformations

For directories with names containing dates, you might want to reformat the date part. For example, changing the date format from YYYYMMDD to DD-MM-YYYY:

rename 's/(\d{4})(\d{2})(\d{2})/$3-$2-$1/' 20230101_dir

This command transforms 20230101_dir to 01-01-2023_dir.

Batch Renaming Directories

For batch renaming, especially when dealing with complex patterns, combining commands and scripting can be powerful. Tools like find and xargs can be used alongside rename or mv.

To prepend 2024_ to all directories in the current folder:

find . -type d -maxdepth 1 -exec mv {} 2024_{} \;

This command finds all directories in the current directory and renames them by adding 2023_ to the beginning of each directory name.

Conclusion

Renaming directories in Linux can be efficiently handled using the mv and rename commands. The mv command offers a straightforward approach for simple renames, while the rename command provides powerful capabilities for bulk and pattern-based renaming. By understanding and utilizing these commands, you can effectively manage your directories and maintain an organized file system. For any questions or further clarification, feel free to leave a comment below.

Share your love

Newsletter Updates

Stay updated with our latest guides and tutorials about Linux.

Leave a Reply

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