File-system management is an important skill to have if you are working with Linux systems often. If you are from a Windows background, you may not yet be familiar with the ways and commands to rename directories on Linux. This article aims to help you out if you need to rename a directory on Linux, or multiple directories at the same time. We will start by giving some simple examples of how to do so using the command line tools commonly available on Linux distributions, then move onto some more advanced examples. I’ll be using my CentOS system for the examples in this article, but it will be much the same for other distributions.
The command most often used to rename directories in Linux is the mv
command, so that is where we will start!
Rename Directories on Linux
The mv
command is a command line utility that moves files or directories from one place to another. It supports moving single files, multiple files and whole directories. And, we can use it to ‘move’ a directory to a new name. The syntax to use mv
to rename a directory on Linux is as follows:
$ mv sourcedir targetdir
Nice and easy! So as a practical example, if I had a directory in my home folder called dir1
and I wanted to rename the directory to dir2
I could use the mv
command like this:
$ mv /home/centos/dir1 /home/centos/dir2
If the directory contains any files, they will be left unchanged. The mv
command simply changes the directory name, it doesn’t affect any of the files contained within. You can confirm the that renaming the directory has worked as expected by listing the contents of the parent directory, which in this example is my home
folder:
$ ls -lah /home/centos/
As you can see in the image above, I now have a directory called dir2
. That’s how straight forward it is to rename Linux directories using the mv
command. However, mv
isn’t the only way to do it, and whilst mv works well for renaming single directories, it doesn’t have any inbuilt capability for renaming multiple directories (though there are ways to have mv
do this, more on that later).
Rename Directories using rename command
Most Linux distributions will also have the rename
command available. If not you should be able to easily install it. On Ubuntu and Debian-derived distributions you can install rename
like this:
$ sudo apt-get install rename
And on Fedora and RedHat-derived distributions you can install it with this:
$ sudo dnf install prename
Ok, so how does it work? Rename
works by having you specify an expression, to tell it how to rename the directories, and the target(s). For example, say we have a bunch of directories all starting with dir
:
$ ll
total 0
drwxrwxr-x. 2 centos centos 6 Dec 11 17:02 dir1
drwxrwxr-x. 2 centos centos 19 Dec 11 16:14 dir2
drwxrwxr-x. 2 centos centos 6 Dec 11 17:02 dir3
What if we wanted to rename these directories to be directory1, directory2 and directory3. We could use the rename command as follows (including the -v switch to get a verbose output):
$ rename -v dir directory dir?
`dir1' -> `directory1'
`dir2' -> `directory2'
`dir3' -> `directory3'
Now if I do the file listing again:
All the directories have been renamed successfully.
Using prename on Fedora and RedHat to rename a Linux directory
prename
is available on Fedora and Redhat-derived distributions. Very similar to rename
, it offers a different way to write the expression used to rename the specified directories, which is based on sed
. With prename
we can use syntax such as the following:
$ prename -v 's/directory/dir/' *
This will replace all the directory names that have ‘directory’ in their name, with ‘dir’. Another example would be if we wanted to rename all the directory names to be uppercase rather than lower. To do so:
$ prename -v 'y/a-z/A-Z/' *dir*
dir1 -> DIR1
dir2 -> DIR2
dir3 -> DIR3
As you can see, all the directories were renamed to use uppercase characters. Now, a word of caution when using rename
or prename
to rename linux directories – as you can see in the examples, there was no prompt to ask if we were sure we wanted to proceed. Always ensure you have tested the expression you are going to use, so that you don’t get any unexpected surprises and end up renaming more than you intended! One thing you can do to help with this is use the -n
switch when running the command, which will perform a dry run. It will show you the potential output, but won’t make any changes.
So that’s how to rename directories on Linux using mv
and how to rename multiple directories on linux using rename
. The should cover the vast majority of use cases. But, we’re not finished yet! Lets have a look at a few other examples.
Rename Multiple Directories
I mentioned earlier that mv couldn’t rename multiple directories, but that there are ways to make it do so. Once such way is to use a FOR loop. This is going into the realms of bash shell scripting, but I think it’s worth talking about here. The following is an example of a FOR loop, which can be saved as a bash script:
#!/bin/bash
for d in *; do
if [ -d "$d" ]; then
mv -- "$d" "${d}_$(date)"
fi
done
When this script is run it will rename all the directories where this script is run from by appending the date to the existing directory name.
Using Find and rename to locate and rename directories
We can use the Linux find
command to find directories and then use the rename
command to rename them. For example, to find all directories in the current location, and then rename them all to be in upper case we could use:
$ find . -mindepth 1 -prune -type d | rename -v 'y/a-z/A-Z/'
As mentioned earlier, its always a good idea to use the -n
switch with the rename command to do a dry run.
Using mmv to rename Linux directories
Finally, I just wanted to mention mmv
, which is another tool which can be used for renaming folders. It can be installed using your distribution’s package manager. On my CentOS system I used yum
:
$ sudo yum install mmv
As a couple of examples, to use mmv to rename all directories starting with ‘dir’ to upper case, you can use:
$ mmv "dir*" "#u1"
And to do the opposite you could use:
$ mmv "dir*" "#l1"
mmv
isn’t a tool I’m overly familiar with, but I have heard it has lots of options. It’s certainly worth checking out the mmv man page.
Summary
We have shown you a number of ways how to rename linux directory. We’ve looked at how to use mv both on it’s own and for renaming multiple directories as part of a bash FOR loop. We have also taken a look at the rename command and how you can identify and rename linux directories using expressions.