- Renaming a directory in Linux doesn’t harm the data inside it. It only changes the path to the data, leaving the files and directories intact.
- The “mv” command is the simplest and most commonly used method for renaming directories in Linux.
- If you need to perform more complex renaming tasks, the “rename” command with Perl expressions provides a powerful and flexible option. Make sure to install the appropriate version for your Linux distribution.
Renaming a directory in Linux is easy, and there are plenty of ways to go about it. From renaming a single directory to finding and renaming many, here’s how to do it.
Renaming a Folder Won’t Harm Your Data
Renaming directories is something we all need to do from time to time.
We might create a directory and misspell its name, and we want to put it right. Often, the purpose of a directory changes over time or through the life of a project, and you want to adjust the name to reflect its new use. Perhaps you’ve decompressed an archive file and it’s created a directory tree with the directory names in uppercase and you’d like them in lowercase.
Whatever the reason, renaming a directory doesn’t do anything to the data held inside it. It changes the path to that data, but the files and directories inside your renamed directory aren’t touched.
Do not rename system directories. Changing the path to system files and commands is going to have a detrimental effect on the running of your computer, to say the least. If you need to use
sudo to rename a directory—unless you really know what you’re doing—the chances are you shouldn’t be renaming it.
Using the mv Command
In the most straightforward cases, all we really need is the
mv command. 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 hails from the dawn of Unix, when short and cryptic commands were in vogue, probably to reduce the number of characters that had to pass along slow serial lines from teletypes and dumb terminals to the actual computer.
It actually stands for “move”, and it can be used to move files from directory to directory. If you move a file to the same location that it is 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 name of the directory 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 as well as the 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 tapping 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 as simple as that.
The rename Command
If your needs are more complicated than the straightforward renaming of a directory you might need to use the
rename command. This allows you to use Perl expressions to rename files and directories. It provides an altogether more powerful and flexible way to rename directories.
We’re going to be talking about the Perl-based
rename command. There is another, older command called
rename which is part of the Linux core utilities. You’ll probably need to install the Perl
rename command we want to use.
To avoid name clashes with the existing
rename command, the Perl
rename command is called
prename on Fedora, and
perl-rename on Manjaro. On Ubuntu, the
prename commands are both symbolic links that resolve to a binary called
So, on Manjaro the command you’ll need to use
perl-rename, and on Fedora it is
prename . On Ubuntu, you can use
To install Perl rename, on Ubuntu you need to 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
Make sure you use the appropriate command for your distribution if you want to work through the examples.
First Steps With rename
rename command takes Perl regular expressions and applies them to a file or directory, or 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 a suitable expression.
rename 'y/A-Z/a-z/' *
All the directories are now in lowercase, whether they were wholly uppercase previously, or contained the odd uppercase letter.
All the magic is contained in the expression. The expression is wrapped in single quotes “
'“. This is what the entire command means.
- y: This means search for any character in the first range of characters, and substitute it for the corresponding character from the second range of characters.
- /A-Z/a-z/: The first range is all the letters from “A” to “Z”, and the second range is all the characters from “a” to “z.”
- *: The asterisk wildcard means apply this to all directories.
In other words, the command reads as “for all directories, swap any uppercase letters for the equivalent lowercase letter.”
Obviously, you can rename a single directory with
rename, although it does smack of overkill. You’ll be quicker using
rename 's/gamma/epsilon-2/' *
The “s” in this expression means substitute. It checks each directory to see if its name is “gamma”. If it is, it replaces it with “epsilon-2.” Be aware though, that this would also have matched a directory called “gamma-zeta”, for example, renaming it to “epsilon-2-zeta.”
We can avoid this by adding the start of string “
^” and end of string “
$” metacharacters to the first clause of the expression.
rename 's/^gamma$/epsilon-2/' *
This leaves the directory “epsilon-2” untouched.
Using rename With Other Commands
We can use other commands to locate the directories we want
rename to work on. If we have a set of nested directories and we want to rename any that end in “-old” so they end in “-archive”, we can achieve that by using
We need to use
rename doesn’t accept piped input. The
xargs command overcomes that problem by accepting the piped input and adding to the command line of another command as a command line parameter.
Our command looks like this:
find . -depth -type d -name "*-old" | xargs -r rename "s/old$/archive/"
- .: We tell find to start searching in the current directory. This could be any path, of course.
- -depth: Use a depth-first search. This means the contents of deeper nested subdirectories are processed before higher ones.
- -type d: Search for directories, not files.
- -name “*-old”: The search clue. We’re looking for directories with names ending in “-old.”
- |: We’re piping the output from find into the
- xargs -r: The
-r(no run if empty) means don’t run the command if there are no matching directories.
- rename “s/old$/archive/”: The
renamecommand to be run.
Our directory tree looks like this before the command.
We run our command:
And we can see that all of the matching directories including the nested ones have been renamed.
Horses for Courses
Renaming a directory doesn’t need anything more than
mv. If you prefer GUI applications you can use your file browser. If you’ve got a lot of directories to rename, and especially if they’re scattered throughout a directory tree, you’re going to need the flexibility of