Home Linux System Administration How To Rename a Directory on Linux

How To Rename a Directory on Linux

by schkn

If you have been working with Linux systems for quite some time, you already know how important it is to keep your filesystem structured.

In some cases, you may need to create temporary directories with random names that need to be renamed later on.

Renaming directories on Linux is not done with a dedicated renaming command but with a command that serves multiple purposes : the “mv” command.

The “mv” command is used on Linux in order to be able to move files but also to rename directories.

In this tutorial, we are going to learn how you can rename directories on Linux.

Rename Directories on Linux using mv

To rename a directory on Linux, use the “mv” command and specify the directory to be renamed as well as the destination for your directory.

$ mv <source_directory> <target_directory>

For example, let’s say that you want to rename a specific directory on your filesystem named “temp” (located in your home directory) to “directory” (also in your home directory)

To rename this directory, you would use the “mv” command and specify the two directory names.

$ mv /home/user/temp /home/user/directory

Note : using the mv command will not delete the content stored inside your directories, you won’t lose any files by renaming your directories on Linux.

Now if you take a look at all the directories stored in your home directory, you will see a new entry for your “directory” folder.

$ ls -l /home/user

drwxr--r-x   2 user user 4096 Nov  9 16:41 Desktop/
drwxr-xr-x   2 user user 4096 Nov  9 16:41 Documents/
drwxr-xr-x   2 user user 4096 Nov  9 16:41 Downloads/
drwxr-xr-x   2 user user 4096 Nov  9 16:41 Music/
drwxrwxr-x   2 user user 4096 Dec 20 10:53 directory/

Awesome, you just renamed a directory on Linux.

Rename Directories using find

In some cases, you may not know directly where your directories are located on your system.

Luckily for you, there is a command that helps you find and locate directories on a Linux system : the find command.

In order to find and rename directories on Linux, use the “find” command with the “type” option in order to look for directories. You can then remove your directories by executing the “mv” command with the “-execdir” option.

$ find . -depth -type d -name <source_directory> -execdir mv {} <target_directory> \;

For this example, let’s pretend that you want to rename a directory beginning with “temp” on your filesystem to “directory”.

The first part of the command will locate where your directory is located.

$ find . -depth -type d -name "temp"

./temp

Now that you know where your directory is, you can rename it by using the “execdir” option and the “mv” command.

$ find . -depth -type d -name temp -execdir mv {} directory \;

Rename Multiple Directories using Bash

As described in our previous tutorials, the Bash scripting language can also be used in order to rename multiple directories on your filesystem.

To rename multiple directories on Linux, create a new script file and use the “mv” command in a “for” loop to iterate over directories.

#!/bin/bash

# Takes directory entries specified and renames them using the pattern provided.

for directory in *
do
    if [ -d "$directory" ]
    then
      mv "${directory}" "${directory}_temp" || echo 'Could not rename '"$directory"''
    fi
done

Save this script as “change_name” and add it to your PATH environment variable if you want to use it on your entire system.

In this script, we are listing all the files and directories that are located in the current working folder (where the script is located).

We are testing if the entry is a directory and if the directory exists using the “-d” option.

Then, if the directory exists, it is renamed to have a “_temp” extension at the end. Feel free to customize this line in order to rename the directories however you want them to be renamed.

$ ls

folder1/  folder2/

$ change_name

$ ls 

folder1_temp/  folder2_temp

Congratulations, you just renamed directories using a Bash script on Linux.

Rename Directories using rename

Instead of using the “mv” command, you can use a dedicated built-in command, however this command may not be directly available on your distribution.

In order to rename directories on Linux, use “rename” with how you want the files to be renamed as well as the target directory.

$ rename <expression> <directory>

As an example, let’s say that you want to rename all your directories written in uppercases to directories names in lowercase letters.

In order to rename those directories, you would run the following command

$ rename 'y/A-Z/a-z/' *

$ ls -l 

drwxrwxr-x 2 user user 4096 Dec 21 02:26 a_temp
drwxrwxr-x 2 user user 4096 Dec 21 02:26 b_temp

Filtering directories to be renamed

In some cases, you may want to rename only a few directories using the rename command.

In order to achieve that, you essentially have two options :

  • Use wildcards in order to filter directories to be renamed.

For example, if you want to rename directories ending with a given string, you would run the following command.

$ rename 'y/_html/_temp/' *

The syntax used by the rename command is the same one as the sed command : you can use this reference to have more information about this syntax.

$ ls -d *_html | rename 'y/*_html/*_temp/'

When using one of those two options, your folders will be renamed to have a “_temp” extension.

$ ls -l

drwxrwxr-x 2 user user 4096 Dec 21 02:42 a_temp
drwxrwxr-x 2 user user 4096 Dec 21 02:42 b_temp

Awesome, you successfully renamed your directories using the rename command!

Conclusion

In this tutorial, you learnt all the ways of renaming directories on Linux, the most common way being the “mv” command.

You also learnt that it is possible to rename directories using the “find” command in order to locate your directories or by using the rename command (that may not be directly available on your system by default).

If you are interested in Linux System Administration, we have a complete section dedicated to it on the website, so make sure to check it out!

rename directories on Linux

You may also like

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.