How to Rename a Folder (Directory) in Linux: A Step-by-Step Guide

In Linux, a folder and a directory are the same thing — two words for the same object. So when you want to rename a folder in Linux, you rename a directory, and the tool for the job is the same one you use for files: the `mv` command. There’s no dedicated “rename” command built into core Linux; `mv` (“move”) does double duty.

This guide walks you through it step by step. You’ll rename a directory in seconds, learn the one gotcha that catches almost everyone, and pick up the directory-specific notes that matter on a real server.

Key Takeaways
• Rename a directory with `mv oldfolder newfolder` — the same command used for files.
• The directory’s contents move with it automatically; you don’t touch them.
Gotcha: if a directory with the new name already exists, `mv` moves your folder *inside* it instead of renaming. Use `mv -T` to force a plain rename.
• You need write permission on the parent directory to rename a folder in it; use `sudo` for system-owned folders.
• For many directories at once, use a `for` loop or the `rename` utility.

How do you rename a folder in Linux?

Here is the short version. Do these three steps and you’re done.

  1. Move to the parent folder (or plan to use full paths):

“`bash cd /home/priscilla/work “`

  1. Rename it with `mv`, old name first, new name second:

“`bash mv reports archive “`

  1. Verify the new name exists:

“`bash ls “`

That’s it. The folder `reports` is now `archive`, and everything that was inside it is still inside it.

If you prefer not to `cd` anywhere, use absolute (full) paths instead:

“`bash mv /home/priscilla/work/reports /home/priscilla/work/archive “`

Both approaches do exactly the same thing. The first is faster when you’re already in the area; the second is safer in scripts where you can’t assume the current directory.

Note: This post focuses on directories. Renaming a single file works the same way — `mv oldfile newfile` — but files have a few different edge cases. See for the file-specific walkthrough.

Why is `mv` used to rename in Linux?

Linux doesn’t ship a standalone `rename-folder` tool because renaming *is* moving. When you “rename” `reports` to `archive` in the same parent directory, the filesystem simply updates the directory’s entry — its name — and leaves the data where it is. Moving and renaming are the same low-level operation, so `mv` handles both.

This is why the command is identical for files and folders: Linux treats them the same way at this level. A directory is just a special kind of file that holds a list of other files. Rename the entry, and you’ve renamed the folder.

Renaming a folder that has contents

A common worry: *”My folder has 200 files and three sub-folders inside it. Will `mv` mess them up?”*

No. When you rename a directory, its contents come along automatically. You are renaming the container, not its items. So:

“`bash mv project-v1 project-final “`

…instantly gives you `project-final/` with every file, sub-folder, and permission intact. Nothing inside changes. This is true even for large directories, because `mv` within the same filesystem just relinks the entry — it doesn’t copy anything.

What’s the gotcha when the new name already exists?

This is the trap that surprises almost everyone, so read this part carefully.

If you `mv` a directory to a name that already exists as a directory, `mv` does not overwrite it and does not throw an error. Instead, it moves your folder *inside* the existing one.

Example. You have a folder `project` and a folder `backup` that already exists. You run:

“`bash mv project backup “`

You might expect `backup` to be replaced by `project`’s contents. What you actually get is:

“` backup/project/ “`

Your `project` folder is now nested inside `backup`. No warning, no error — `mv` saw an existing directory and treated it as a *destination* to move into, not a name to rename to.

How to avoid it:

  • Check first. Before renaming, confirm the target name doesn’t already exist:

“`bash ls -d backup 2>/dev/null && echo “EXISTS — careful!” “`

  • Force a plain rename with `-T`. The `-T` flag tells `mv` to treat the target as a normal name, never as a directory to move into:

“`bash mv -T project backup “` With `-T`, if `backup` is a non-empty directory, `mv` refuses and tells you — which is exactly the safe behavior you want. (`-T` is a GNU coreutils option, available on virtually all standard Linux distributions.)

Make `mv -T` a habit when there’s any chance the target name exists, and you’ll never accidentally nest folders again.

The `mv` command for directories at a glance

What you want Command Notes
Rename a folder `mv old new` Same parent; contents move with it
Rename using full paths `mv /path/old /path/new` Safe in scripts
Force a true rename (no nesting) `mv -T old new` Errors instead of moving into an existing dir
Rename a folder with spaces `mv “old name” “new name”` Quote both names
See what `mv` is doing `mv -v old new` Verbose: prints the action
Don’t clobber an existing target `mv -n old new` “No-clobber” — skips if target exists
Rename a system folder `sudo mv old new` Needs elevated permission
Verify the result `ls -l` Confirm the new name

How do you rename a folder with spaces in the name?

Spaces confuse the shell because it uses them to separate arguments. To rename a folder whose name contains spaces, quote the names (or escape each space with a backslash):

“`bash

mv “old project” “final project”

mv old\ project final\ project “`

Without the quotes, `mv old project final project` looks like four separate arguments and fails. Quoting keeps each name as a single unit. When in doubt, quote both names.

What permissions do you need to rename a folder?

Here’s the rule that trips people up: to rename a folder, you don’t need permission on the *folder itself* — you need write and execute permission on its parent directory. That’s because renaming changes an entry *in the parent’s* list.

  1. Check who owns the parent and what you can do there:

“`bash ls -ld /var/www “`

  1. If you own it (or have write access), rename normally:

“`bash mv /var/www/old-site /var/www/new-site “`

  1. If it’s owned by `root` or another user, use `sudo`:

“`bash sudo mv /var/www/old-site /var/www/new-site “`

A typical “Permission denied” error when renaming almost always means you lack write access to the parent directory — not the folder you’re trying to rename. Reach for `sudo` only when the folder genuinely belongs to the system or another user, and double-check the path before you run it.

How do you rename many directories at once?

When you have a batch of folders to rename, don’t do them one at a time. Two practical approaches:

Option 1: A `for` loop

Say every folder is named `temp_*` and you want them as `archive_*`:

“`bash for dir in temp_*/; do mv -T “$dir” “archive_${dir#temp_}” done “`

This loops over each matching directory, strips the `temp_` prefix, and renames it with an `archive_` prefix. The `-T` flag keeps it a clean rename. Always test a loop on a copy or a couple of folders first.

Option 2: The `rename` utility

Many distributions ship a `rename` command for pattern-based renaming. The popular Perl-based version uses a regular expression:

“`bash rename ‘s/^temp_/archive_/’ temp_*/ “`

This replaces a leading `temp_` with `archive_` across all matching directories in one shot.

Heads up: there are two different `rename` programs. The Perl `rename` (also called `prename` / `file-rename`) uses the syntax above. The `util-linux` version uses a simpler `rename FROM TO FILES` form: `rename temp_ archive_ temp_*/`. Run `man rename` to see which one you have before relying on it.


Manage and rename directories on your own server with DarazHost

Renaming folders is something you’ll do constantly once you run real workloads — organizing web roots, rotating backups, restructuring app directories. To do it on the command line, you need proper shell access to your server.

DarazHost gives you that access. Our VPS and dedicated hosting plans include full SSH root access, so you can `cd`, `mv`, `sudo`, and script directory renames exactly as shown above — with the freedom to organize your filesystem however your project demands. Prefer a graphical approach? On shared hosting, the cPanel File Manager lets you rename folders with a couple of clicks, no command line required.

Either way, you get reliable, well-resourced hosting and 24/7 expert support ready to help if a permission issue or a tricky rename has you stuck. ·


Quick recap

To rename a directory in Linux:

  1. `cd` to the parent folder (or use full paths).
  2. Run `mv oldfolder newfolder`.
  3. Verify with `ls`.

Remember the gotcha — if the new name already exists as a directory, `mv` nests instead of renames, so use `mv -T` to be safe. Quote names with spaces, use `sudo` for system folders, and reach for a `for` loop or `rename` when you have many to do.

Frequently asked questions

Does renaming a folder in Linux move the files inside it? The files stay inside the folder — you’re renaming the container, not its contents. After `mv project final`, every file and sub-folder that was in `project` is now in `final`, untouched. Within the same filesystem, nothing is actually copied; only the directory’s name changes.

Why did `mv` put my folder inside another folder instead of renaming it? Because a directory with that target name already existed. When the target is an existing directory, `mv` moves the source *into* it rather than renaming. Run `mv -T old new` to force a true rename, or check with `ls -d new` before you run the command.

How do I rename a folder owned by root? Use `sudo`: `sudo mv /etc/old-config /etc/new-config`. Renaming requires write permission on the *parent* directory, so for system locations owned by `root` you need elevated privileges. Verify the path carefully before running anything with `sudo`.

Can I rename a folder and a file the same way? Yes — `mv old new` works for both, because Linux treats a directory as a special file. The command is identical. See for file-specific edge cases like overwriting an existing file.

Is there a command to undo a folder rename? There’s no automatic undo, but the reverse is just another `mv`: if you ran `mv reports archive`, you restore it with `mv archive reports`. If you accidentally nested a folder (the gotcha above), move it back out: `mv backup/project project`.

About the Author

Leave a Reply