3 Techniques to Rename a Directory in Linux

Published October 27, 2023

In this article, you’ll discover 3 handy techniques to efficiently rename directories in Linux and effectively organize your files.

Rename directory in Linux

Keep Exploring; 7 prime Linux Application and Software

1. Using ‘rename’ Command to Rename a Directory

It can be frustrating when you need to rename multiple directories and files at once, but luckily the rename command is here to help.

This dedicated command allows you to easily rename your directories with just a few simple steps.

However, it’s important to keep in mind that you’ll need sudo privilege to make any changes to your directories and files in Linux. We understand that this may seem like an extra step, but it’s necessary to ensure the security and integrity of your system.

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

Once you’ve got the rename command line tool installed on your Linux system, renaming directories and files in Debian-based systems is a breeze. Just remember to use the syntax below:

rename [options] ‘s /<original_name>/<new_name>/’ <

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/*

In the previous command, we used the ‘s’ command to substitute the original_name with the new_name. To perform this substitution, 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.

 

To make the search for names matching the provided expression in the Home directory, don’t forget to include an asterisk (*) in your command. This will ensure that all relevant names are considered.

 

After executing the rename command, you can verify the success of the operation by using the ‘ls’ command. By checking the directories available in the current working directory, you can ensure that the desired directory has been renamed accordingly. This step allows you to confirm the assignment of the new name and ensure that everything is in order.

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>/’ <

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/<

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

The mv (move) command in Linux is a versatile tool that not only relocates files and directories within the file system, but also enables effortless renaming of said directories and files. This feature makes the mv command an invaluable asset for those seeking to streamline their file management processes in the Linux environment.

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

Explore More ; What is Manjaro Linux

Get Connected with us on Facebook,TwitterInstagram.