How to Rename a Directory in Linux

how to rename a directory in Linux

In Linux, directories (or folders) are an essential part of the file system and renaming directories can be useful when organizing your files. While it is a simple task, and there are Linux Terminal commands to make it even easier for you, things might get confusing initially if you are a beginner. Renaming a directory in Linux is a simple process that can be done using the command-line or the graphical interface. In this article, we will show you how to rename a directory in Linux using both of these methods.

Rename Directory/Folder in Linux (2023)

Rename Directories in Linux (CLI Method)

Though the CLI method might seem intimidating at the start, you will see that it works the fastest and is probably the easiest of methods once you get a hold of all the commands. The commands used here come preinstalled on most distros and work pretty much the same on all Linux distributions.

Rename Directory Using the mv Command

The mv command stands for “move” and is generally used to move both files and directories from one location in the file system to another. The syntax to use the mv command in Linux is:

mv <options> <original_directory_location> <new_directory_location>

Now, you must be wondering that this command is used to move stuff around, how is it possible to rename directories using it? If you look closely at the syntax of the mv command, you will see that it is simply changing the location of a directory or file to a new location in the file system, which also includes the directory name. The syntax to rename a directory with the mv command in Linux is:

mv <options> <original_directory_name> <new_directory_name>

Some of the common options to pair with the mv command are:

OptionsDescription
--backupThis option is used to make a backup of each existing destination files
-fThis option forces overwrite of the files and folders without any prompt
-iThis option prompts before overwriting the files and folders
-vThis option explains what is being done by the operation.
Rename a Single Directory

The most common method to rename a single directory is to use the mv command. While renaming a directory, make sure to give a new and unique name, otherwise, it will conflict with other directory names. The syntax to rename a directory using the mv command in Linux is:

mv <options> <directory_old_name> <directory_new_name>

Let’s say, you want to rename the directory “example_dir” to “test_dir“, use the mv command as follows:

mv -v example_dir test_dir

Once you execute this command, you will get the following output:

renamed 'example_dir' -> 'test_dir'

renaming single directory using mv command
Renaming Multiple Directories

The mv command is generally used to rename a single file or directory in Linux, but with a bit of bash magic, you can even use it to rename multiple directories in Linux. The syntax to rename multiple files or directories using the mv command is:

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

This script may seem quite intimidating, but an example will clear out the syntax:

c=1
for d in *; do
  mv -v "$d" "test_$c"
  ((c=c+1))
done

Let’s break down the script in the above example:

  • The line “c=1” creates a variable named ‘c’ and assigns a value of 1 to it. This will be the unique identifier for each directory
  • The line “for d in *; do” loops over the entire directory contents and stores each item name inside the variable ‘d’ during every iteration.
  • In bash, if you need to use the value stored in a variable, you need to add a ‘$’ symbol at the beginning of a variable name. Therefore, “mv -v “$d” “test_$c” renames the directories one-by-one as per the standard syntax. The old directory names are stored in $d and the new name is test_$c where $c is the unique identifier which gets added at the end of the new name.
  • The line “((c=c+1))” increments the current value of c by one.
  • Finally, the last line demarcates the end of the loop.
Renaming multiple directories in Linux

Rename Directory Using rename command

When you need to rename multiple files without a complicated syntax, use the rename command. It generally comes preinstalled on most Linux distributions, but if you do not find it preinstalled, you can easily install it with the below-given commands:

1. For Debian-based systems –

sudo apt install rename

2. If you are on Fedora-based systems:

sudo yum install prename

3. For Arch-based systems:

sudo pacman -S install rename

The syntax to use the rename command on Debian-based systems is:

rename <options> 's/<old_name>/<new_name>/' <directory_to_rename>

You can use both the substitute and translate Perl-based regular expressions with the rename command (the rename command is written by the creator of Perl – Larry Wall!). In the above syntax, “s” specifies that you are using the “substitute” regular expression.

If you are on RedHat, or any other Fedora-based system, use the following syntax:

rename <options> <old_name> <new_name> <directory_to_rename>

Some of the common options to pair with the rename command are:

OptionsDescription
-vstands for “verbose” and shows information about the current operation taking place.
-nstands for “no action” and is used for testing to see how the directory names are going to be affected after the operation.
-fforcefully overwrites the directory name with the new name without any prompt
Rename a Single Directory

When you need to rename a single directory, the rename command works similarly to the mv command in Linux. The syntax to rename a single directory is:

rename -v 's/<original_name>/<new_name>/' <file_name>

For example, if you want to rename a directory from “example_dir” to “test_dir” in Linux, use this command:

rename -v 's/example_dir/test_dir/' example_dir

Once you execute the above command, you will get the following output:

example_dir renamed as test_dir

renaming single directory using rename command
Rename Multiple Directories

The main advantage of using the rename command over the mv command is that you can easily rename multiple directories using the former. The syntax to rename multiple directories is fairly simple compared to the mv command:

rename <options> 's/<pattern_to_replace>/<pattern_to_replace_with>/' <directories_to_rename>

For <directories_to_rename>, you can either directly specify the directory names or use wildcards for all the directories matching a pattern. For example, if you need to rename all the directories with names such as test_1, test_2, and test_3… to example_1, example_2, example_, and so on respectively, use this command:

rename -v 's/test/example/' *

Once you execute the above command, you will get the following output, confirming that all the directories are renamed:

test_1 renamed as example_1
test_2 renamed as example_2
test_3 renamed as example_3
test_4 renamed as example_4
test_5 renamed as example_5
test_6 renamed as example_6
test_7 renamed as example_7
test_8 renamed as example_8
test_9 renamed as example_9
test_10 renamed as example_10
renaming multiple directories using rename command

If there are both files and directories with similar naming patterns, you can filter only the directories using the command:

ls -d <common_directory_pattern>

Then, use the bash pipeline to send the output of the above command to the rename command as input:

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

For example, if your directory contains the sub-directories test1, test2, test3, test4 and also the files with the names test.txt, test.sh, test.c. To rename only the sub-directories, use this command:

ls -d test*/ | rename -v '/s/test/example/'

In the above command, the ls -d test* filters only the directories beginning with the name “test” and then sends the filtered directory names to the rename command in Linux, where it substitutes “test” with “example”.

renaming only directories using rename command

Rename Directories in Linux (GUI Method)

Most beginners prefer to use the GUI method as it is more user-friendly compared to the command line method. Here, we are using the Nautilus file manager and GPRename tools to rename folders but the steps mentioned below will work the same for other file managers and tools as well.

Rename Directory Using File Manager

Every desktop Linux distribution comes pre-installed with some file manager. In case it is missing, you can easily install it using the following command:

1. For Debian-based systems:

sudo apt install nautilus -y

2. For Fedora-based systems:

sudo dnf -y install nautilus

3. For Arch-based systems:

sudo pacman -S nautilus

Rename Single Directory

Renaming a single directory using a file manager is pretty straightforward. Follow these steps for the same:

1. Open Nautilus/Files from the application menu

2. Navigate to the directory you want to rename and select it

3. Now right-click on the directory name and select the “Rename” Option. Alternatively, you can press F2 on the keyboard to rename.

rename option in the right click menu

4. Type in the new directory name in the input box and hit enter on the keyboard.

renaming single direcotry in file manager
Rename Multiple Directories

If you feel that renaming multiple directories using the command line is cumbersome, we have also lined up a GUI method to rename directories. Follow these steps to rename multiple directories using the file manager:

1. Open Nautilus/Files from the application menu. Navigate to the directories you want to rename and select them.

2. Now right-click on the directory name and select the “Rename” Option. Alternatively, you can press F2 on the keyboard to rename.

right click menu to rename diretories

3. A new dialogue box will open asking for the new naming pattern for the selected directories. Here you will get two options to rename directories.

Rename Using a Template

1. In this method, all the directories will be renamed using a template. Press the backspace key on the keyboard to clear the input field.

renaming directories using a template in file manager

2. Type in the new common name for all the directories. Then click on the “+Add” button to add a new unique identifier. Then, select the type of unique identifier you want to choose from the drop-down.

choosing the unique identifier for each direcotry name

3. Then click on the “Original Name(Ascending)” button to choose the automated numbering order. When you are satisfied with the naming convention, finally click on the big Rename button on the top right-hand corner.

renaming using a template in file manager

Find and replace text

You can use this option when you need to replace common directory names with a new common name in Linux. Here’s how it works:

1. Enter the old common name in the “Existing Text”. You can see the common name will get highlighted character-by-character as you enter the name.

2. Enter the new replacement name in the “Replace With” text box.

3. Verify the changes to take place from the bottom pane and finally click on the Rename button on the top right-hand corner to make the changes.

replacing a part of directory with new name

Rename Directory Using GPRename Tool

GPRename is a lightweight tool that is very useful for renaming multiple files and directories with its various options. Renaming using this tool is even easier than the file manager and can prove to be very useful for complete beginners. GPRename does not come preinstalled, but can be easily installed using the following command:

1. For Debian-based systems –

sudo apt update && apt install gprename

2. For Fedora-based systems –

sudo dnf install gprename

3. For Arch-based systems –

sudo pacman -S gprename

Rename Single Directory with GPRename

Follow these steps to rename a single directory in Linux using GPRename:

1. Open the terminal using the keyboard shortcut “Ctrl + Alt + T” in Linux. Type gprename and press Enter to open the GPRename tool.

2. Once it opens, navigate to the desired directory from the left pane. On the right pane, you will see two tabs – Files and Directories. They show all the contents of the parent directory. Click on the directories tab to view all the directories

3. Now, select the directory you want to rename and head over to the bottom pane.

selecting the directory to rename in gprename

5. The bottom pane will contain all the options and actions you can use while renaming. Choose the options as per your requirements and click the “Preview” button. Once you are happy with the changes, click the “Rename” button to finally rename the directory. In the below example, we are inserting an underscore (_) before the last character of the selected directory name.

inserting an underscore before the last charcter of directory name
Rename Multiple Directories Using GPRename

The GPRename tool shines when it comes to renaming multiple directories in Linux. Follow these steps to rename multiple directories:

1. Open the terminal from the app or use the keyboard shortcut “Ctrl + Alt + T” in Linux. Type gprename and press Enter on the keyboard to open the GPRename tool.

2. Once it opens, navigate to the desired directory using the left pane. On the right pane, you will see two tabs – Files and Directories, which show all the contents of the parent directory. Click on the directories tab to view all the directories.

3. Now, select the directories you need to rename and head over to the bottom pane.

selecting directories to rename in gprename

5. In the bottom pane, select the options you need to rename the directory in Linux and click the “Preview” button to verify the changes. Finally, click the “Rename” button to make the changes permanent. In the below example, we are inserting an underscore (_) before the last character for each selected directory name.

inserting an underscore before last charcter of every directory name

Renaming Directories in Linux

Files and folder management is one of the most important skills that are useful for every user. In this article, we have shown four methods to rename directories in Linux. The GUI methods are the easiest for beginners, but once you learn the command line methods, you see it is much faster with more options to play with. Do let us know if you face any issues.

comment Comments 0
Leave a Reply