How to Rename a Directory in Linux (with Examples)

Adam Bullock
Adam Bullock
June 28, 2026 · 2 min read
#linux#command-line#devops
How to Rename a Directory in Linux (with Examples)

To rename a directory in Linux, use the mv command: mv old-name new-name. There is no dedicated "rename" command for a single directory - mv ("move") handles both moving and renaming.

mv old-name new-name

For example, to rename project to archive:

mv project archive

Rename a directory with mv

mv takes the current name and the new name:

mv reports reports-2026

If reports-2026 does not already exist, the directory reports is renamed to it. Run ls to confirm:

ls -d reports-2026

It is just a rename, not a copy

mv does not duplicate anything - it renames in place, instantly, even for huge directories, because only the directory entry changes.

Renaming a directory that has files in it

You do not need to empty a directory first. mv renames the directory itself, and everything inside comes along:

mv src lib        # 'src' and all its contents are now under 'lib'

Directory names with spaces

Quote the name (or escape the space) so the shell treats it as one argument:

mv "old name" "new name"
# or
mv old\ name new\ name

Fixing "permission denied"

Renaming a directory needs write permission on its parent directory, not on the directory itself. If you see mv: cannot move ...: Permission denied:

# you own it but lack parent write perms - check the parent:
ls -ld .
# system path you are allowed to change:
sudo mv old-name new-name

Only use sudo for paths you are genuinely allowed to modify.

Renaming many directories at once

mv does one at a time. For bulk pattern renames, use the rename utility or a loop:

# replace spaces with underscores in every item:
rename 's/ /_/g' *

# or a shell loop:
for d in */; do mv "$d" "${d// /_}"; done

Quick reference

Task Command
Rename a directory mv old new
Rename with spaces mv "old name" "new name"
Rename a system directory sudo mv old new
Bulk rename by pattern rename 's/old/new/' *

That is the whole job: mv old new for one directory, rename for many.

Want to try these on a real shell without installing anything? Open the free online Linux terminal - a genuine Linux shell in your browser - and run them yourself.


Want to practice Linux the way the job actually uses it - not just commands, but fixing real broken systems? That is what HeyDevJob is for: real Linux and DevOps work in a live cloud workspace, with every fix on a portfolio you can show.

Frequently asked questions

How do I rename a directory in Linux?

Use the mv command: mv old-name new-name. For example, mv project archive renames the directory project to archive. It works whether the directory is empty or full.

What is the rename command in Linux?

There are two. mv renames a single file or directory (mv old new). The separate rename utility renames many files at once using a pattern, e.g. rename 's/ /_/g' * to replace spaces with underscores.

How do I rename a folder from the command line?

The same way: mv old-folder new-folder. Linux treats folders (directories) and the command line as the normal way to rename - there is no separate 'folder' command.

Can I rename a directory that has files in it?

Yes. mv renames the directory itself, so everything inside moves with it automatically. You do not need to empty it first.

Why do I get 'permission denied' when renaming a directory?

Renaming needs write permission on the PARENT directory, not the directory itself. Fix it with the right ownership/permissions, or run the command with sudo if it is a system path you are allowed to modify.

Stop proving you can build what AI builds for free.

Fix a real broken production system in a live cloud workspace. Every fix lands on your portfolio.

Fix your first ticket →