How to rename a directory in Linux
Renaming a directory in Linux is easy and there are many ways to do it. From renaming a single directory to finding and renaming many, here’s how to do it.
Your data is safe
Renaming directories is something we all have to do from time to time.
We can create a directory and misspell its name, and we want to fix it. Often the purpose of a directory changes over time or over the life of a project, and you want to adjust the name to reflect its new use. Maybe you unzipped an archive file and it created a directory tree with the directory names in uppercase and you’d like them to be lowercase.
Whatever the reason. renaming a directory does nothing to the data it contains. It changes the path to this data, but the files and directories inside your renamed directory are not affected.
Do not rename system directories. Changing the path to system files and commands will have a detrimental effect on the operation of your computer, to say the least. If you must use
sudo to rename a directory – unless you really know what you’re doing – chances are you shouldn’t rename it.
Using the mv command
In the simplest cases, all we really need is the
mv ordered. This is an integral part of every Linux distribution, so there is nothing to install.
mv command is over 50 years old at the time of writing. It comes from the dawn of Unix, when short, cryptic commands were all the rage, probably to reduce the number of characters that had to pass along slow serial lines from teletypes and dumb terminals to the real computer.
It actually means “move”, and it can be used to move files from one directory to another. If you move a file to the same location it’s already in and give it a new name, you’ve renamed the file. And we can do the same with directories.
There are two subdirectories in this directory.
To rename a directory, we use the mv command. We need to provide the current directory name and the new name.
mv old-work archive-2
If the directory you want to rename is not in your current directory, provide the path and directory name.
mv ~/htg/old-work ~/htg/archive-2
Using the File Browser
File browsers are able to rename directories. The keystroke in the GNOME Files application is F2. Highlighting a directory and pressing the F2 key opens the “Rename Folder” dialog.
Type in the new name and click the green “Rename” button.
The directory is renamed for you.
It’s that simple.
The rename command
If your needs are more complicated than just renaming a directory, you may need to use the
rename ordered. This lets you use Perl expressions to rename files and directories. It provides an altogether more powerful and flexible way of renaming directories.
We will talk about the basics of Perl
rename ordered. There is another older command called
rename which is part of the basic utilities of Linux. You will probably need to install the Perl
rename command we want to use.
To avoid name conflicts with the existing
rename command, the Perl
rename the command is called
prename on Fedora, and
perl-rename on Manjaro. On Ubuntu, the
prename The commands are both symbolic links that resolve to a binary called
So on Manjaro the command you will need to use
perl-renameand on Fedora it is
prename . On Ubuntu you can use
To install Perl rename, on Ubuntu you must type:
sudo apt install rename
On Fedora, the command is:
sudo dnf install prename
On Manjaro, the package is called
sudo pacman -Sy perl-rename
Be sure to use the appropriate command for your distribution if you want to work on the samples.
Get started with renaming
rename The command takes Perl regular expressions and applies them to a file or directory, or a group of files or directories.
In our directory we have a collection of other directories.
Their names are a mixture of lowercase, uppercase, and mixed case. We can convert them all to lowercase with an appropriate expression.
rename 'y/A-Z/a-z/' *
All directories are now lowercase, whether they were all uppercase before, or contain the odd uppercase letter.
All the magic is contained in the expression. The expression is surrounded by single quotes “
'“. That’s what the whole command means.
- there: it means to search for any character in the first character range and replace it with the corresponding character from the second character range.
- /AZ/az/: The first range consists of all the letters from “A” to “Z”, and the second of all the characters between “a” and “z”.
- *: The asterisk wildcard means apply this to all directories.
In other words, the command reads: “for all directories, replace all uppercase letters with the equivalent lowercase letter”.
Obviously you can rename a single directory with
rename, even if that smacks of exaggeration. You will be faster using
rename 's/gamma/epsilon-2/' *
The “s” in this expression stands for substitute. It checks each directory to see if its name is “gamma”. If so, it replaces it with “epsilon-2”. Be aware though that this would also have matched a directory called “gamma-zeta”, for example, renaming it “epsilon-2-zeta”.
We can avoid this by adding the start of string “
^” and end of string “
$” metacharacters to the first proposition of the expression.
rename 's/^gamma$/epsilon-2/' *
This leaves the “epsilon-2” directory intact.
Using rename with other commands
We can use other commands to locate the directories we want
rename work on. If we have a set of nested directories and we want to rename all those that end in “-old” so that they end in “-archive”, we can achieve this using
We must use
rename does not accept piped inputs. The
xargs The command overcomes this problem by accepting piped input and adding it to another command’s command line as a command line parameter.
Our command looks like this:
find . -depth -type d -name "*-old" | xargs -r rename "s/old$/archive/"
- .: We say find to start searching in the current directory. It could be any path, of course.
- -depth: Use a depth-first search. This means that the contents of deeper nested subdirectories are processed before higher subdirectories.
- -type d: Search for directories, not files.
- -name “*-old”: The search index. We are looking for directories whose names end in “-old”.
- |: We pipe the output of find to the
- xargs-r: The
-r(no execution if empty) means do not execute the command if there are no matching directories.
- rename “s/old$/archive/”: The
renamecommand to execute.
Our directory tree looks like this before the command.
We launch our order:
And we can see that all the corresponding directories including the nested ones have been renamed.
Renaming a directory needs nothing more than
mv. If you prefer GUI applications, you can use your file browser. If you have a lot of directories to rename, and especially if they’re scattered across a directory tree, you’ll need the flexibility of
RELATED: How to Manage Files from Linux Terminal: 11 Commands You Need to Know