Rename a Directory in Linux with Simple Methods

Rename a Directory in Linux means changing the folder’s name to organize or fix it. You can rename a directory using commands like mv or rename in the terminal, or through a graphical file manager for an easier approach.

🤖AI Overview:

Rename a Directory in Linux involves changing the name of an existing directory using commands or graphical interfaces. The most common methods include using the mv command to move and rename the directory, the rename command for batch renaming, the find command combined with mv, bash scripts, or a graphical file manager. These methods allow efficient organization and management of directories within the Linux file system.

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 a directory in Linux is a fundamental skill that enhances file organization and management. The mv command is the primary tool for this task and offers a straightforward way to rename directories. By following the steps outlined in this guide and understanding important considerations such as permissions and existing directory names, you can safely rename directories in various situations.

Always verify your actions by checking directory names before and after renaming to avoid mistakes. When in doubt, use the interactive option with mv or create backups beforehand.

With these instructions, even beginners can confidently rename directories in Linux, making file management more efficient and intuitive.

FAQ

To rename multiple directories simultaneously, you can use the rename command or write a bash script with the mv command. The rename command supports bulk renaming using patterns, while a bash script with a loop can rename multiple directories step-by-step.

The rename command is a dedicated Linux tool for renaming files and directories. It uses pattern substitution to change directory names. Its syntax varies by Linux distribution. It can rename multiple directories efficiently.

Yes, Linux supports renaming directories via a graphical user interface (GUI) using file managers like Nautilus. Right-click the directory, choose Rename, then type the new name. This method is beginner-friendly and straightforward for those not familiar with terminal commands.

After renaming a directory, you can verify the change by listing the files and directories using the ls command in the terminal. This command shows the current directory contents, confirming the new directory name appears.

Yes, renaming some directories might require sudo privileges, especially system or other users' directories. Using sudo before commands like "mv" or "rename" grants the necessary permissions to rename these directories.

Common options for the mv command are -v for verbose output showing the renaming steps, -i to prompt before overwriting, -f to force rename without prompts, and --backup to create a backup of the original directory before renaming.

You can combine the "find" and "mv" commands to rename directories located with find. For example, use "find . -depth -type d -name old_name -execdir mv {} new_name \" to find directories named old_name and rename them to new_name.

Yes, with the rename command, you can use the -n option for a no-action dry run. This shows what the command would do without making any actual changes, helping you verify the renaming process safely before applying it.

If rename is not pre-installed, you can install it using your package manager. For example, on Ubuntu or Debian run "sudo apt install rename", on CentOS use "sudo yum install prename", and on Arch Linux use "sudo pacman -S rename".