Rename a Directory with mv
mv old-name new-name
mv /var/www/staging /var/www/staging-old
mv -i old-name new-name
mv -T old-name new-nameLinux has no separate command for renaming one folder: mv does it. The first line renames a folder in the current directory, the second uses full paths, the third asks before it replaces anything, and the fourth never moves the folder into another one by mistake. On one file system, the rename is instant, however much the folder holds. All mv options are in the mv command guide.
Check That the New Name Is Not Taken
ls -ld new-nameThis is the trap with folders. If new-name does not exist, mv old-name new-name renames the folder. If new-name already exists as a folder, mv moves old-name inside it, and you end up with new-name/old-name. Run ls -ld new-name first: "No such file or directory" is the answer you want.
Use -T to Rename, Never Move Into
-T treats the destination as a name, never as a folder to move into. If new-name is a folder with files in it, mv stops with an error instead of nesting one folder inside the other; an empty folder of that name is replaced. Together with -T, -n skips the rename when the destination exists, and -v prints what was done. A trailing slash does not help here: mv old-name new-name/ still renames the folder when new-name does not exist, as the mv guide explains.
Names with Spaces, Dashes, or Only a Case Change
mv "Old Reports" old-reports
mv -- -tmp tmp-old
mv Photos photosQuote names that contain spaces, or press Tab and let the shell complete and escape them. -- ends the options, so a name that starts with a dash is not read as one. Linux file systems are case-sensitive, so a rename that only changes letter case works like any other.
Rename Many Directories at Once
Two different programs are called rename. On Ubuntu and Debian, sudo apt install rename installs the Perl version, which takes a sed-style expression; -n shows what it would do without doing it:
rename -n 's/^backup-/archive-/' backup-*
rename 's/^backup-/archive-/' backup-*The expression works like the one in sed replace. On AlmaLinux and Rocky Linux, rename comes from util-linux and takes the old text, the new text, and the files: rename backup- archive- backup-*. A bash loop works everywhere; put echo before mv to print the commands first:
for d in backup-*/; do d="${d%/}"; mv -nT -- "$d" "archive-${d#backup-}"; doneRenaming Across File Systems
When the new path is on another disk or mounted volume, mv copies everything and then deletes the original, which takes time and needs room on the target. df -h with both paths shows whether they are on the same disk. For large folders, copy with cp -a or rsync, check the copy, then remove the original; how to mount a drive in Linux explains mount points.
What to Update After a Rename
Anything that refers to the old path stops working: root lines in Nginx or Apache, systemd units with a working directory, cron jobs, scripts, and symbolic links that point into the folder. Find the references before you rename:
sudo grep -rn "/var/www/staging" /etc/nginx /etc/systemd/system /etc/cron.dProcesses that already have files open keep working until they restart. If you cannot update every reference at once, leave a link from the old name to the new one with ln -s, as described in how to create a symlink in Linux.
Common Errors
- Permission denied: renaming needs write permission on the parent folder. In a folder with the sticky bit, such as
/tmp, only the owner can rename an entry. Usesudofor system paths. - Directory not empty: the destination is a folder with content, either with
-Tor inside a target folder that already holds that name. - Device or resource busy: the folder is a mount point. Check with
findmnt. - Cannot move to a subdirectory of itself: the new path lies inside the old folder.
Updated:
Linux VPS with Full Root Access
MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.
More Guides
No Limits on Service or Support
You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

- 100% customer satisfaction goal
- Our own hardware
- 24/7 ticket and email support
- 4 countries with locations