Save 10%Pay yearly on any plan. 6% half-yearly, 3% quarterly. Help centreWhatsAppTelegram TeamsOpen a ticket

3 Techniques to Rename a Directory in Linux

Rename a directory in Linux with mv, rename many at once with the rename command, and use find for whole trees. With the traps: spaces, existing names, case.

3 min readBy Swati

Reviewed by the AmazingRDP technical team

Renaming a directory in Linux

A server of your own to practise on. A Linux VPS gives you root access. See Linux VPS from $6.99

Linux has no command called "rename directory". A directory is renamed by moving it to a new name, and that one idea covers the everyday case. For many directories at once there are two more tools. Here are all three, with the mistakes that catch people.

1. mv: one directory

mv oldname newname

That is all. The contents are untouched; only the name changes, and it takes no time however large the directory is.

Three things to know:

  • If newname already exists as a directory, mv does not rename. It moves oldname into it, giving newname/oldname. To be told instead, use mv -T oldname newname, which refuses.
  • Names with spaces need quotes: mv "old name" "new name".
  • Useful options: -i asks before overwriting, -n never overwrites, -v prints what was done.
mv -iv /var/www/site /var/www/site-old
sudo mv /opt/app /opt/app-2024

With full paths you can rename from anywhere. Directories that belong to root need sudo.

2. rename: many directories by pattern

The rename command changes many names in one go, using a search-and-replace expression. Install it on Ubuntu and Debian with sudo apt install rename.

rename -n 's/2023/2024/' */
rename 's/2023/2024/' */
rename 's/ /_/g' */
rename 'y/A-Z/a-z/' */
  • The first line is a rehearsal: -n prints what would happen and changes nothing. Always run it first.
  • The second replaces "2023" with "2024" in every directory name here. The pattern */ matches directories only.
  • The third replaces every space with an underscore; the fourth makes names lower case.

A warning about versions. On Rocky, AlmaLinux, CentOS and Fedora, the program called rename is a different, simpler one with another syntax:

rename 2023 2024 */

It replaces the first text with the second, with no expressions. If the lines above give an error there, that is why.

3. find or a loop: whole trees and special cases

To rename directories at any depth, combine find with mv:

find . -depth -type d -name 'tmp' -execdir mv {} temp \;

This renames every directory called tmp, however deep, to temp. -depth handles the deepest first, so that a renamed parent does not break the path to its children; -execdir runs mv inside each parent directory.

For anything a pattern cannot express, a short loop is clearest:

for d in */; do
  mv -v "$d" "backup-$d"
done

Every directory gets "backup-" in front. Put echo before mv for a rehearsal. As a script: see how to run a shell script.

Traps

SituationWhat happens
The new name existsThe directory is moved inside it (see above)
Changing only upper and lower case, on a Windows share or a Mac diskThose file systems see the names as the same. Rename in two steps: mv Data tmpname, then mv tmpname data
A name starting with a hyphenIt is read as an option. Write mv -- -old new
Permission deniedYou need write permission on the parent directory, not on the directory itself: see users and groups
Device or resource busyThe directory is a mount point. Unmount it first: see findmnt
Renaming across disksIt becomes a copy followed by removal of the original, and takes as long as the data needs

What a rename can break

The files do not care, but everything that refers to the old path does:

  • configuration files of web servers and applications,
  • scheduled jobs: see cron,
  • scripts, symbolic links and systemd units,
  • the working directory of a running program, which keeps working until it next looks up its own path.

Before renaming something on a server, search for the old path: grep -r "/var/www/site" /etc 2>/dev/null.

In a file manager

On a desktop, select the directory and press F2. Most file managers also rename several selected items at once with a pattern.

For a server to practise on, see our Linux VPS plans. Editing the files that mention the old name: the nano editor.

Questions

How do I rename a directory in Linux?

Use mv oldname newname.

How do I rename multiple directories at once?

Use the rename command with a search-and-replace expression, after a rehearsal with -n.

Why did mv move my directory into another one?

The new name already existed as a directory. mv -T refuses in that case.

How do I rename a directory with spaces in its name?

Put the names in quotes: mv "old name" "new name".