How To Rename a Directory on Linux part 2 (ok)
https://devconnected.com/how-to-rename-a-directory-on-linux/
Last updated
Was this helpful?
https://devconnected.com/how-to-rename-a-directory-on-linux/
Last updated
Was this helpful?
written by
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.
Table of Contents
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.
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.
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.
Awesome, you just renamed a directory on Linux.
In some cases, you may not know directly where your directories are located on your system.
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.
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.
Now that you know where your directory is, you can rename it by using the “execdir” option and the “mv” command.
To rename multiple directories on Linux, create a new script file and use the “mv” command in a “for” loop to iterate over directories.
In this script, we are listing all the files and directories that are located in the current working folder (where the script is located).
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.
Congratulations, you just renamed directories using a Bash script on Linux.
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.
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
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.
When using one of those two options, your folders will be renamed to have a “_temp” extension.
Awesome, you successfully renamed your directories using the rename command!
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!
Luckily for you, there is a command that helps you : the find command.
As described in our previous tutorials, can also be used in order to rename multiple directories on your filesystem.
Save this script as “change_name” and if you want to use it on your entire system.
We are testing if the entry is a directory and using the “-d” option.
The syntax used by the rename command is the same one as the sed command : you can use to have more information about this syntax.
Use in order to filter directories to be renamed