How to Rename a Directory in Linux
What Windows calls folders, Linux-based operating systems call directories.
Linux Command to Rename a Directory (Quick Examples)
If you want the quick answers, here they are.
Using relative paths, to rename a directory called Backups
to Backups_2024
, we'd run a command like this:
mv Backups Backups_2024
Using absolute paths, we might use a command like this:
mv /home/alex/Backups /home/alex/Backups_2024
So it's just a matter of running one of these commands:
# For relative paths to a sub-directory in our current directory:
mv old_directory_name new_directory_name
# For absolute paths to the target directory:
mv /path/to/old_directory_name /path/to/new_directory_name
Now let's dive into a more detailed answer. And see what "current directory" and "relative path" mean.
How to Use the "mv" Command to Rename Directories
There are many ways to use the mv
command for this purpose. But let's focus on just one way; the one using relative paths, since it's slightly more beginner-friendly.
Step 1 – Navigate to Your Parent Directory
When we're at the command line, we're always "inside" a certain directory. This is called the working directory, or current directory.
The command line itself will usually show us where we are. Here's an example, where the command line shows us, with blue text, our current working directory, /usr/games
:
If we can't see our current / working directory, we can run this command:
pwd
And it will output where we are, /home/alex
, in this case:
pwd
is short for "print working directory".
So, back to the issue. Where is our (target) directory located? The one we want to rename. Because every directory is located inside another directory, called the parent directory.
(Exception to this is the /
root directory. Since that's the absolute top, there is nothing "above it". So it has no parent directory.)
But let's say we have a directory called Backups
that we want to rename. And let's say the full path (also called absolute path) to this directory is:
/home/alex/Backups/
Essentially, the absolute path is the "full address" to a certain target directory. A complete path, of how to get to a lower-level directory, step-by-step, directory-by-directory.
If we break this down into smaller pieces, we can visualize it this way:
- /
- home
- alex
- Backups
- alex
- home
In this case:
- The parent directory of
Backups
isalex
. - The parent directory of
alex
ishome
. - The parent directory of
home
is/
(called the root directory; think of it as the "root of it all", the top-level directory).
So if the full path to the directory we want to rename is /home/alex/Backups/
, we want to move to the parent directory of Backups
. Which is alex
. But we can't just tell Linux "move me to the alex
directory". Because it won't know where that is. So we can tell it something like this:
Hey Linux. You know the top-level directory,/
? It has another directory in it,home
. And thenhome
, also has another directory inside, calledalex
. I want to step inside that directory. Go to/
-->home
-->alex
The command to do this is:
cd /home/alex
cd is short for "change directory".
Step 2 – Rename Your Directory (Using Relative Paths)
Now that we've stepped inside the parent directory we need, let's list what this directory contains. Just to double-check we're in the right spot.
To list the contents of the current (working) directory, we can run a simple command:
ls
ls
is short for "list". And it will output what files and (sub)directories this directory contains.
Ok, we are in the right spot. We can see the Backups
directory here.
To rename it from Backups
to something else like Backups_2024
we can run a command like this:
mv Backups Backups_2024
mv
is actually short for "move". Because we can also move files and directories to other places with this command. But it also has this added use-case of renaming files or directories. Because, when we are renaming a directory / file, we are essentially still "moving it". Sure, it stays in the same (parent) directory; but it's still "moved" to a file / directory with a different name.
In this command:
mv Backups Backups_2024
We've used relative paths. Because, we didn't tell our operating system "exactly" where this Backups
directory is located. (We didn't specify an absolute path like /home/alex/Backups
).
The path is relative to our current / working directory. Since we're already in /home/alex
, specifying Backups
in our mv
command is like saying:
I am referring to theBackups
directory in my current directory,/home/alex
.
We can think of a relative path as "adding" to the current directory.
/home/alex
+ (relative path) Backups
= /home/alex/Backups
After a rename operation it's a good idea to list the contents of our current / working directory again. To make sure that we got the expected result.
ls
We can see our directory was renamed correctly.
But why do this? Why run ls
before, and after renaming a directory? What can go wrong? Since the mv
command will output errors in some cases, helping us catch some mistakes.
The problem is, there is at least one potential user error it might not catch. Here's an example.
Avoid this Mistake with the "mv" Command
We currently have this structure:
One directory called Backups_2024
, and one file called notes.txt
.
We can imagine that we renamed our old Backups
directory to Backups_2024
because a year went by. And we've collected all of this year's backups in the directory simply called Backups
. Now that the year has passed, we rename it, so we know 10 years later where to find our stuff from 2024.
But now, we want to collect other backups. So what can we do? Create another directory simply called Backups
:
mkdir Backups
Now we'll have this structure:
- One directory called
Backups
. - One directory called
Backups_2024
.
But let's imagine we don't know the first directory (Backups
) exists. And we say to ourselves:
Hmm, I've changed my mind. I don't want to have this directory namedBackups_2024
. Let me change it back to the way it was:Backups
.
So we run this command:
mv Backups_2024 Backups
Now if we look at the contents of our current directory, it will seem like it worked correctly:
But remember, there already was a directory called Backups
. What happened to it? Did it get overwritten? We had two directories, now we only have one. Surely one is missing.
Fortunately, nothing was lost. We still have both directories. But what actually happened can be misleading.
Backups_2024
was moved inside the pre-existing Backups
directory.
So instead of renaming a directory we accidentally moved it inside another directory.
We can confirm, if we list the contents of the Backups
directory:
ls Backups
We now have this structure:
- /
- home
- alex
- Backups
- Backups_2024
- Backups
- alex
- home
The gist of the story is: When we want to rename a directory, we must verify that nothing with that same (target) name already exists (in the same parent directory). Otherwise, instead of renaming, we'll end up accidentally moving our stuff to another location / parent directory.
It's a mistake that seems unlikely, but it does happen, on occasion. Especially in parent directories that have a lot of sub-directories inside.
Using the "mv" Command with Absolute Paths Instead
We recommended to first navigate to the parent directory, because the mv
commands become simpler / shorter this way. Especially if we have to rename multiple directories. We then just have to enter commands like:
mv archives old_archives
mv Backups Old_Backups
mv website_v2 website_v3
And so on.
Whereas, if we'd not "step inside" that parent directory first, we'd need to use absolute paths. And the commands to do the same renames might become:
mv /home/alex/archives /home/alex/old_archives
mv /home/alex/Backups /home/alex/Old_Backups
mv /home/alex/website_v2 /home/alex/website_v3
A lot more to type. Although there is a trick, to make these slightly shorter:
mv /home/alex/{archives,old_archives}
mv /home/alex/{Backups,Old_Backups}
mv /home/alex/{website_v2,website_v3}
Essentially, these two commands are equivalent:
mv /home/alex/archives /home/alex/old_archives
mv /home/alex/{archives,old_archives}
But we can see that switching to the parent directory first, and then using relative paths, leads to the shortest commands:
mv archives old_archives
Suggestion
Do you often work with a large number of files, or directories? Sick of entering hundreds of commands just to copy, move, rename files / directories? Then check how you can install and run Midnight Commander on your Linux distribution.
It will greatly speed up the process, giving you a visual interface for working with files, and directories. Yes, a visual interface, even in a command-line environment. Even mouse clicks will work if you use a capable SSH client like PuTTY (or the included SSH client on Linux and MacOS machines).
This is how Midnight Commander looks like:
Have fun!