Methods to Rename a Directory in Linux

5 Quick Methods to Rename a Directory in Linux

One of the simplest and most fundamental tasks for Linux administrators is interacting and working with files and directories, which are a crucial part of the file system. For Organizing files, correcting typographical errors in file names, preserving privacy, complying with standards for collaboration, maintaining consistency in file structures, and other reasons, renaming directories in Linux is essential. That’s why Most Linux administrators continuously perform this task for various purposes to have an organized and manageable file system. Although renaming directories is not complex, novice Linux users may find it challenging.

If you want to use Linux VPS and are new to the world of Linux, there is no need to worry because Linux is a powerful, high-speed operating system that is capable of fulfilling your objectives. Linux allows renaming a directory through the terminal and graphical user interface, supporting useful commands to simplify the renaming process. In this article, you will learn 5 quick and valuable methods to rename directories in Linux to manage your files efficiently.

1. Using ‘rename’ Command to Rename a Directory

The rename command, as its name suggests, is a dedicated command used to rename a directory, allowing the user to rename several directories and files simultaneously. But remember that you must have sudo privilege to rename the directory and file in Linux.

Installing the ‘rename’ command-line tool in Linux

All Linux distributions support the rename command by default. If this command-line tool is unavailable in your Linux distribution, install rename command on popular Linux distributions such as Debian, Ubuntu, and CentOS using the following commands.

To install the rename command on Ubuntu/Debian, run the following command:

sudo apt install rename

Install the rename command line tool on CentOS/Fedora by running the following command:

sudo yum install prename

Install the rename command line on Arch Linux by running the following command:

sudo pacman -S install rename

Main syntax of ‘rename’ for renaming directory

After installing the rename command line tool in Linux, you can use the following syntax to rename directories and files in Debian-based systems:

rename [options] 's /<original_name>/<new_name>/' <directory_to_rename>

And the following syntax for renaming directories in Fedora-based systems is as follows:

rename <options> <original_name> <new_name> <directory_to_rename>

Instead of <directory_to_rename>, you can replace file and directory names or wildcards representing a specific pattern that all directories match.

For example, to rename a directory from “old_directory” to “new_directory_name,” use the following pattern:

rename 's/old_directory/new_directory_name/' /path/to/directory/*

Replace old_directory with the current directory name, new_directory_name with the desired new directory name, and /path/to/directory with the directory containing the directory you want to rename.

In the previous command, ‘s‘ stands for substitute, and it is used to replace the original_name with the new_name. Also, don’t forget to use an asterisk (*), which is responsible for searching for names matching the provided expression in the Home directory.

To verify the rename of a directory or file, you can use the ‘ls’ command to check the directories available in the current working directory. This allows you to ensure that the desired directory has been successfully renamed and verify the assignment of the new name:

ls

rename command Options

The rename command, like other Linux commands, supports options to change its behavior according to different purposes; the most common options are as follows:

  • -v (verbose): invokes verbose output and provides information about the current operation and process steps.
  • -n (no action): does not apply the name change and only displays the directory’s name after the change. This option is usually used for testing purposes.
  • -f (forcefully): changes the directory’s name without displaying any prompt.
  • y: Translates one string of characters into another( character for character).

Renaming a single directory with rename command

The rename command allows you to rename a directory; the syntax for renaming a single directory or file in linux is as follows:

rename -v ‘s/<original_name>/<new_name>/’ <file_name>

For example, to rename the directory from “old_dir” to “new_dir“, run the following command:

rename -v ‘s/ old_dir / new_dir /’ old_dir

Renaming Multiple directories with rename command

One of the valuable advantages of the rename command is renaming multiple directories and files simultaneously. The basic syntax for renaming multiple directories using the rename command is as follows:

rename <options> ‘s/<pattern_to_replace>/<pattern_to_replace_with>/’ <directories_to_rename>

For example, to rename Folder1, Folder2, Folder3, Folder4 to Directory1, Directory2, Directory3 and Directory4, run the following command:

rename -v ‘s/ Folder /Directory /’ *

Output:

Folder1 renamed as Directory 1
Folder2 renamed as Directory 2
Folder 3 renamed as Directory 3
Folder4 renamed as Directory 4

To simplify renaming directories, you can filter files and directories that use a specific naming pattern through a command, and using a bash pipeline, use the output of the filtering command as input for the rename command. Therefore, to achieve this goal, first, use the following command to filter directories that match a specific name pattern:

ls -d <common_directory_pattern>

Then, to use the bash pipeline to send the output of the previous command to the rename command, run the following command:

ls -d <common_directory_pattern> | rename 's/<pattern_to_replace>/<pattern_to_replace_with>/'

For example, if your directory contains files named Folder.txt, Folder.sh, and Folder.c in addition to sub-directories Folder1, Folder2, Folder3, and Folder4, run the following command to rename sub-directories:

ls -d Folder*/ | rename -v '/s/Folder / Directory /'

As a result, by executing the ls -d Folder* command, the names of the directories that start with Folder are filtered and used as input for the rename command to change their names from “Folder” to “Directory.”

2. Using mv command to rename a directory

Typically, the mv (move) command is used in Linux to move files and directories to a new location within the file system. However, this command also allows you to rename directories and files during the move. Therefore, the mv command is a helpful method for renaming directories and files in linux.

Main mv syntax for renaming a directory

The basic mv syntax for renaming a directory or file in Linux is as follows:

mv <options> <original_directory_name> <new_directory_name>

As a result, you can use this command to rename a directory by moving it to a new location with a different name.

For example, to rename a directory from “old_directory” to “new_directory” using the mv command, run the following command:

mv old_directory new_directory

Output:

renamed 'old_directory ' -> ' new_directory '

To verify the rename of the directory, use the following command:

ls -l

Or

ls

By running the previous command, a list of directories and files available in the current working directory is displayed, allowing you to ensure the renaming of directories and folders.

If you need further guidance on using the rename command, running rename -help  and man rename commands will be helpful.

mv command options

The mv command offers various options to expand its functionality, which are as follows:

  • -v: lists the steps of the current process.
  • -i: prompts before operation is applied to files and directories.
  • –backup: Makes a backup copy of the target file.
  • -f: applies any operation on files and directories without prompt.

Renaming Multiple Directories in linux with mv Command

Like the rename command, the mv command allows users to rename multiple files and directories simultaneously. However, the difference lies in the fact that while the rename command does not require a bash script for renaming directories and files, the mv command needs to use a bash script for this purpose. To rename multiple files and directories using the mv command, use the following syntax:

c=<unique_identifier>
for d in *; do
mv -v "$d" "<new_name_>$c"
((c=c+1))
done

c=<unique_identifier>: This command creates the variable “c” with a specific value as a unique identifier for each directory.

for d in *; do: This loop iterates over the contents of the directory to store the name of each item in the variable “d” during each iteration.

mv -v “$d” “<new_name_>$c“: The symbol “$” in the bash script is used to store the value in a variable and is typed at the beginning of the variable name for this purpose. Therefore, this line in the previous command renames the directory one by one as per the standard syntax.

((c=c+1)): This command increments the current value of “c” by one.

3. Using find Command to Rename Directories

In Linux, the find command plays a useful role in searching for files and directories. Moreover, combined with the mv command, find enables renaming directories and files after locating them. To rename files and directories via the find command, use the following syntax:

find . -depth -type d -name <current directory name> -execdir mv {} <new directory name> \;

When executing the previous command for renaming a directory, ensure to run the mv command along with the’ -execdir’ option because using this option is necessary to execute the mv command after finding the directory by the find command.

For example, to find and rename “Directory1” to “Test_Directory,” use the following command:

find . -depth -type d -name Directory1 -execdir mv {} Test_Directory  \;

To verify the changes you made to the directory name, run the following command:

ls

4. Using a Bash Script to Rename Directories and Files

Using a Bash script is another method for renaming directories and files that is worth mentioning. Renaming multiple directories and preserving a specific pattern for future use are features that Bash scripts provide, making it one of the common and efficient methods for renaming files. You should also use the mv command to rename a directory using a Bash script. Here, we present a simple script for renaming a directory:

#!/bin/bash

# Define the old and new directory names
old_directory="/path/to/old_directory"
new_directory="/path/to/new_directory"
# Check if the old directory exists
if [ ! -d "$old_directory" ]; then
echo "Old directory not found: $old_directory"
exit 1
fi
# Check if the new directory already exists
if [ -e "$new_directory" ]; then
echo "New directory already exists: $new_directory"
exit 1
fi
# Rename the directory
mv "$old_directory" "$new_directory"
echo "Directory renamed from $old_directory to $new_directory"

Replace “/path/to/old_directory” with the actual path of the directory you want to rename, and “/path/to/new_directory” with the desired new directory name

Then save the script in a file, for example, rename_directory.sh, and run the following command to make the script executable:

chmod +x rename_directory.sh

Then use the following command to run the script:

./rename_directory.sh

As a result, using this script and the mv command, we assigned a new name to the specified directory. Through this script, we checked for the old directory’s presence and the new directory’s absence. Adjust the script as needed for your specific renaming requirements.

5. Using a GUI File Manager to Rename a Directory in Linux

Linux beginners often prefer using a graphical user interface for managing files and other system settings. Renaming directories through the GUI is much simpler than using the Linux terminal. File manager is one of the very simple methods to rename directories. If you are using a distribution where a file manager is not available by default, you can install a File Manager using the following commands for popular Linux distributions like Debian, Ubuntu, and Fedora:

Install File manager on Debian-based systems with the following command:

sudo apt install nautilus -y

Install File manager on Fedora-based systems with the following command:

sudo dnf -y install nautilus

Install File manager on Arch -based systems with the following command:

sudo pacman -S nautilus

After making sure that File Manager is available in the distribution you are using, now you can rename the directory you want through File Manager by following the steps below:

  • Click the “Files” icon on the desktop or the applications menu to open the file manager.
  • Navigate to the directory you want to rename.
  • Select “Rename” from the menu that appears when you right-click on the directory.
  • Enter the new name in the input box and press Enter.

If you intend to rename multiple directories and files simultaneously via GUI, select the directories you want to rename and right-click on one of them. In the dialogue box that appears for entering the new name for the directory, select the option “rename using a template.” Press the backspace key on the keyboard in the input field to clear it, then enter a common name for all the selected directories you intend to rename. Next, click the “Add” button to specify a new unique identifier. For this purpose, you should choose the “type of unique identifier” from the drop-down menu.

Renaming a Directory in Linux

For automated numbering order, select Original Name (Ascending). Finally, click the “Rename” button at the top right of the screen.

Conclusion

Renaming directories is a common task in system and file management that Linux administrators constantly perform. Therefore, upgrading file and directory management skills is helpful for every user. In this guide, you have learned 5 quick methods to rename a directory in Linux ubuntu, debian, centos and other distributions, enabling you, as a Linux administrator, to organize and manage your file system and directories efficiently. While renaming via the Linux graphical user interface is more user-friendly and simpler than the terminal, learning Linux commands for renaming is beneficial and essential for quick and professional management of directories.

We hope this article has benefited you and enriched your knowledge and expertise in file and directory management in Linux.