If you are learning Linux, one of the first tasks you will need to master is file and folder management. Understanding how to change directory name in linux is a fundamental skill that keeps your system organized and your workflow efficient. Whether you are renaming a project folder or fixing a typo in a directory name, the process is straightforward once you know the right commands.
In this guide, we will walk you through every method to rename directories in Linux. You will learn the mv command, the rename utility, and how to handle spaces and special characters. By the end, you will be confident in managing your file system like a pro.
How To Change Directory Name In Linux
The most common way to rename a directory in Linux is using the mv (move) command. Despite its name, mv is used for both moving and renaming files and directories. To rename a directory, you simply “move” it to a new name in the same location.
Here is the basic syntax:
mv old_directory_name new_directory_name
For example, if you have a directory called project_old and you want to rename it to project_new, you would run:
mv project_old project_new
This command works instantly. No confirmation prompts, no undo button. So double-check your spelling before hitting Enter.
Prerequisites For Renaming Directories
Before you start renaming, make sure you have the following:
- A Linux terminal (command line) open
- Proper permissions to modify the directory
- The correct current working directory (use
pwdto check)
If you are renaming a directory that belongs to another user, you may need sudo privileges. For example:
sudo mv /home/otheruser/oldname /home/otheruser/newname
Be careful with sudo—it gives you full power, and mistakes can be hard to fix.
Renaming Directories With Spaces In The Name
Directory names with spaces require special handling. If you try to run mv my folder new folder, Linux will think you are refering to multiple files. Use quotation marks or escape the spaces with a backslash.
Method 1: Quotation marks
mv "my folder" "new folder"
Method 2: Backslash escaping
mv my\ folder new\ folder
Both methods work equally well. Choose the one that feels more natural to you. Personally, I prefer quotation marks because they are easier to read.
Renaming Directories With Special Characters
Special characters like $, &, *, or ? can cause unexpected behavior in the shell. Always wrap such names in single quotes to prevent variable expansion or globbing.
Example:
mv 'data$2024' 'data_2024'
Single quotes tell the shell to treat everything literally. Double quotes still allow some expansion, so single quotes are safer for tricky names.
Using The Rename Command For Bulk Operations
If you need to rename multiple directories at once, the rename command is your best friend. It uses Perl expressions to match and replace patterns in names.
First, check if rename is installed on your system. Some Linux distributions include it by default, others require installation:
sudo apt install rename # Debian/Ubuntu
sudo yum install rename # RHEL/CentOS
The basic syntax is:
rename 's/old_pattern/new_pattern/' directory_names
For example, to rename all directories that contain “backup” to “archive”:
rename 's/backup/archive/' */
This command only affects directories in the current location. Use -n (dry run) to preview changes before applying them:
rename -n 's/backup/archive/' */
Always test with -n first. It saves you from accidental mass renaming.
Renaming Directories Recursively
Sometimes you need to rename directories inside subdirectories. The rename command does not handle recursion by default. You can combine it with find for this purpose.
Example: Rename all directories named “temp” to “temporary” in the current folder and all subfolders:
find . -type d -name "temp" -exec rename 's/temp/temporary/' {} \;
This command finds every directory named “temp” and applies the rename. Be cautious—it will modify deeply nested folders.
Using A File Manager (GUI Method)
If you prefer a graphical interface, most Linux desktop environments allow renaming directories with a simple right-click. Here is how:
- Open your file manager (Nautilus, Dolphin, Thunar, etc.)
- Navigate to the directory you want to rename
- Right-click on the folder and select “Rename” (or press F2)
- Type the new name and press Enter
This method is intuitive and requires no command knowledge. However, it is less efficient for bulk renaming or automation.
Renaming Directories Over SSH
When managing a remote server via SSH, you use the same mv command. The only difference is that you are working on a remote machine. Make sure you are in the correct directory before renaming.
Example:
ssh user@server
cd /var/www
mv old_site new_site
If you need to rename directories on a remote server without an interactive shell, you can combine SSH with the command:
ssh user@server "mv /path/to/oldname /path/to/newname"
This runs the rename command directly on the remote system.
Common Mistakes And How To Avoid Them
Even experienced users make errors when renaming directories. Here are the most frequent pitfalls:
- Typing the wrong path: Always use
pwdandlsto confirm your location - Overwriting existing directories: If a directory with the new name already exists,
mvwill move the source into it, not overwrite it - Forgetting to escape spaces: This causes the command to fail or affect wrong files
- Using
sudounnecessarily: It can change ownership and permissions
To avoid overwriting, check if the target name exists first:
ls -d new_directory_name 2>/dev/null && echo "Exists" || echo "Does not exist"
If it exists, choose a different name or move the old directory elsewhere.
Renaming Directories With Case Changes
Linux file systems are case-sensitive. Renaming Project to project is a valid operation, but it can be tricky. On some file systems, mv Project project will fail because the source and destination appear identical in case-insensitive contexts.
To work around this, use a temporary intermediate name:
mv Project temp_name
mv temp_name project
This two-step process ensures the rename succeeds regardless of file system behavior.
Automating Directory Renames With Scripts
If you regularly rename directories following a pattern, consider writing a simple Bash script. For example, to append a date to all directories:
#!/bin/bash
for dir in */; do
mv "$dir" "${dir}_$(date +%Y%m%d)"
done
This script loops through every directory in the current folder and appends today’s date. Test it on a copy of your data first.
You can also use find with -exec for more complex conditions:
find . -type d -name "old_*" -exec bash -c 'mv "$0" "${0/old_/new_}"' {} \;
This replaces “old_” with “new_” in all directory names starting with “old_”.
Using Tab Completion For Accuracy
One of the best ways to avoid typos is using tab completion. In the terminal, start typing the directory name and press Tab. The shell will auto-complete the name or show matching options.
Example:
mv proj[Tab] new_project
If there is only one directory starting with “proj”, it will complete to the full name. This prevents errors from misspelling.
Renaming Directories With Symbolic Links
If a directory has symbolic links pointing to it, renaming the directory will break those links. The links will still point to the old name, which no longer exists.
To handle this, you have two options:
- Update the symbolic links manually using
ln -sf - Use
symlinksorfindto locate and fix broken links
Example of finding broken symlinks:
find . -type l -xtype l
This lists all broken links. Then you can recreate them with the correct target path.
Permissions And Ownership After Renaming
Renaming a directory does not change its permissions or ownership. The mv command preserves these attributes. However, if you use sudo to rename, the new directory will still belong to the original owner.
To change ownership after renaming, use:
chown user:group new_directory_name
This is important when moving directories between users or system locations.
Frequently Asked Questions
Can I rename a directory while it is in use?
Yes, you can rename a directory even if files inside are open. The rename operation is instantaneous and does not affect open file handles. However, applications that rely on the absolute path may break.
What is the difference between mv and rename?
mv is for single renames or moves. rename is for bulk pattern-based renaming using Perl expressions. Use mv for simple tasks and rename for complex batch operations.
How do I undo a directory rename?
There is no built-in undo. You must rename it back manually using the same mv command. Always double-check before executing.
Can I rename a directory with a leading dot (hidden directory)?
Yes, hidden directories (names starting with a dot) can be renamed like any other. For example: mv .hidden_folder visible_folder. The directory will no longer be hidden after renaming.
Does renaming a directory affect its contents?
No, the contents remain unchanged. Only the directory’s name is modified. All files and subdirectories inside are preserved exactly as they were.
Conclusion
Mastering how to change directory name in linux is a small but powerful skill. Whether you use the simple mv command for quick renames, the rename utility for batch jobs, or a GUI file manager for visual ease, you now have multiple tools at your disposal.
Remember to always verify your current directory, escape spaces and special characters, and test bulk operations with a dry run. With practice, renaming directories will become second nature, saving you time and keeping your file system tidy.
Go ahead and try renaming a directory right now. Open your terminal, create a test folder with mkdir test_folder, and rename it to my_folder using mv test_folder my_folder. You will see how quick and satisfying it is.
If you run into any issues, refer back to this guide. Linux gives you full control over your files—use it wisely.