Renaming a directory in Linux requires using the `mv` command with a specific syntax. This guide covers exactly how to rename a directory in linux using simple commands, practical examples, and common pitfalls to avoid.
You don’t need special tools or root access for basic renaming. The `mv` command is your go-to solution, and it works on almost every Linux distribution.
How To Rename A Directory In Linux
The `mv` command stands for “move,” but it also handles renaming. When you move a directory to a new name within the same parent folder, Linux treats it as a rename operation.
Basic Syntax For Renaming
The syntax is straightforward:
mv [options] source_directory target_directory
For example, to rename “old_folder” to “new_folder”:
mv old_folder new_folder
This works as long as “new_folder” doesn’t already exist in the same location. If it does, Linux moves “old_folder” inside “new_folder” instead.
Step-By-Step Example
- Open your terminal (Ctrl+Alt+T on most systems)
- Navigate to the parent directory containing the folder you want to rename
- Type:
mv projects backup_projects - Press Enter
- Verify with:
ls -l
Thats it. The directory “projects” is now “backup_projects”.
Renaming Directories With Spaces
Directory names with spaces require special handling. Linux sees spaces as argument separators, so you must quote the name or escape the space.
Using Quotes
mv "my documents" "my files"
Using Backslashes
mv my\ documents my\ files
Both methods work identically. Pick whichever feels more natural to you.
Renaming Directories With Absolute Paths
You don’t have to be in the parent directory. Use full paths from anywhere in the filesystem:
mv /home/user/projects /home/user/archives
This renames the “projects” directory inside /home/user to “archives”.
Using The `Rename` Command
For batch renaming or pattern-based changes, the `rename` command is more powerfull. It uses Perl expressions or simple substitution patterns.
Installation
On Debian/Ubuntu:
sudo apt install rename
On Red Hat/Fedora:
sudo dnf install prename
Basic Usage
To rename all directories containing “old” to “new”:
rename 's/old/new/' */
This only affects directories because of the trailing slash in the pattern.
Renaming Multiple Directories At Once
Use a loop in bash to rename several directories with similar patterns:
for dir in project_*; do mv "$dir" "${dir/project_/archive_}"; done
This renames “project_1”, “project_2”, etc., to “archive_1”, “archive_2”.
Common Mistakes And How To Avoid Them
Accidentally Moving Instead Of Renaming
If the target directory already exists, `mv` moves the source inside it. Always check for existing directories first:
ls -d target_directory 2>/dev/null || mv source target
Forgetting To Quote Spaces
This creates two separate arguments and likely an error:
mv my documents my files
Always use quotes or escapes.
Renaming System Directories
Never rename directories like /bin, /etc, or /usr. This breaks your system and may require reinstallation.
Renaming Hidden Directories
Hidden directories start with a dot (.). Use the same `mv` command:
mv .config .old_config
Be carefull with hidden folders—they often contain application settings.
Using Wildcards For Pattern Matching
Wildcards help rename multiple directories that match a pattern:
mv project_* backup_*
But this only works if you have exactly matching names. For complex patterns, use `rename` or a loop.
Renaming Directories With Special Characters
Directories can contain characters like !, @, #, $, %, ^, &, (, ), {, }, [, ], ;, ‘, “, and backticks. Always quote these names:
mv "my folder (backup)" "my folder (archive)"
Or escape each special character:
mv my\ folder\ \(backup\) my\ folder\ \(archive\)
Verifying The Rename Operation
Always verify after renaming:
ls -la
Or check a specific directory:
ls -ld new_name
This shows the directory exists with the correct name.
Renaming Directories With Case Changes
Linux is case-sensitive, so “Documents” and “documents” are different. To change case:
mv documents Documents
This works because the filesystem sees them as distinct names.
Using Tab Completion To Avoid Typos
Press Tab after typing part of the directory name. Bash auto-completes the name, preventing spelling errors.
For example, type mv pro and press Tab to complete “projects”.
Renaming Directories With `Find` And `-Exec`
For advanced renaming across subdirectories, combine `find` with `mv`:
find . -type d -name "old_name" -exec mv {} new_name \;
This finds all directories named “old_name” under the current directory and renames them to “new_name”.
Scripting Directory Renames
Create a bash script for repeated renaming tasks:
#!/bin/bash
for dir in "$@"; do
if [ -d "$dir" ]; then
mv "$dir" "${dir}_backup"
fi
done
Save as “rename_script.sh”, make executable with chmod +x rename_script.sh, and run it.
Renaming Directories On Remote Servers
Use SSH to rename directories on remote machines:
ssh user@server 'mv /path/to/old /path/to/new'
This executes the rename command on the remote server.
Undoing A Rename
If you rename incorrectly, simply rename it back:
mv new_name old_name
No undo command exists, so double-check before pressing Enter.
Permissions And Renaming
You need write permission on the parent directory, not on the directory itself. If you get a “Permission denied” error, use sudo:
sudo mv /protected/folder /protected/new_name
Renaming Directories With Symbolic Links
Renaming a directory does not update symbolic links pointing to it. The links become broken. Update them manually or recreate them.
Using Graphical File Managers
Most Linux desktops let you rename directories with a right-click. But the command line is faster for bulk operations.
Frequently Asked Questions
Can I Rename A Directory While It’s In Use?
Yes, you can rename a directory even if files inside are open. The rename only changes the directory entry, not the inode.
What Happens If The New Name Already Exists?
The source directory moves inside the existing directory. To avoid this, ensure the target name doesn’t exist or use the `-T` flag with `mv` to treat it as a rename.
How Do I Rename A Directory With A Hyphen At The Start?
Use mv -- -old_folder new_folder or mv ./-old_folder new_folder to prevent the hyphen from being interpreted as an option.
Is There A Way To Preview A Rename Before Executing?
For `rename`, use the `-n` (dry-run) flag. For `mv`, you can test with `echo mv old new` to see what would happen.
Can I Rename Multiple Directories With Different Names At Once?
Yes, using a loop or `rename` command with pattern substitution. For example, rename 's/old/new/' */ renames all directories containing “old” to “new”.
Summary
Renaming a directory in Linux is simple with the `mv` command. Remember to quote names with spaces, avoid existing target directories, and verify your changes. For bulk operations, use `rename` or bash loops. Always double-check before executing, especially on system directories or remote servers.