If you want to rename a folder in Linux, the `mv` command is your go-to tool for this task. It stands for “move,” but it works perfectly for renaming directories too. Many beginners think renaming a folder is complicated, but it’s actually quite simple once you know the basics. In this guide, we’ll cover everything you need to know about how to rename folder in Linux, from simple commands to advanced techniques.
You might be wondering why you can’t just right-click and rename like on Windows or macOS. The Linux terminal gives you more control and speed, especially when handling multiple files. Once you get used to it, you’ll probably prefer the command line for most file operations.
Let’s start with the absolute basics and work our way up to more complex scenarios. By the end of this article, you’ll be renaming folders like a pro, even if you’re completely new to Linux.
How To Rename Folder In Linux Using The Mv Command
The `mv` command is the standard way to rename folders in Linux. It’s short, simple, and available on every distribution. To rename a folder, you use this basic syntax:
mv old_folder_name new_folder_name
For example, if you have a folder called “project_old” and want to rename it to “project_new,” you would type:
mv project_old project_new
That’s it. The folder is renamed instantly. No confirmation prompts, no extra steps. The command works the same whether the folder is empty or full of files.
One important thing to remember: if a folder with the new name already exists, `mv` will move the old folder inside that existing folder instead of renaming it. This can cause confusion if you’re not careful. Always check that the new name isn’t already taken.
Renaming A Folder In The Current Directory
When you’re already in the directory containing the folder you want to rename, the command is straightforward. Just type `mv` followed by the current name and the new name. No paths needed.
For instance, if you’re in your home directory and want to rename “Documents” to “MyDocs,” you would run:
mv Documents MyDocs
This works for any folder in your current location. You can rename folders with spaces in their names by using quotes or escape characters:
mv "Old Folder" "New Folder"
Or with backslashes:
mv Old\ Folder New\ Folder
Renaming A Folder In A Different Directory
If the folder you want to rename isn’t in your current directory, you need to provide the full path. The syntax changes slightly:
mv /path/to/old_folder /path/to/new_folder
For example, to rename a folder located in /var/www/html from “old_site” to “new_site”:
mv /var/www/html/old_site /var/www/html/new_site
You can also use relative paths if you’re close to the target directory. For instance, if you’re in /var/www and want to rename the “html” folder to “public”:
mv html public
This approach saves typing when you’re working in nested directory structures.
Using The Rename Command For Batch Operations
The `rename` command is more powerful than `mv` when you need to rename multiple folders at once. It uses Perl expressions to match and replace text in folder names. This is extremly useful for bulk renaming tasks.
First, check if `rename` is installed on your system. Some Linux distributions don’t include it by default. You can install it using your package manager:
- On Debian/Ubuntu:
sudo apt install rename - On Fedora:
sudo dnf install prename - On Arch:
sudo pacman -S perl-rename
Once installed, the basic syntax is:
rename 's/old_text/new_text/' folder_names
For example, to rename all folders containing “2023” to “2024”:
rename 's/2023/2024/' */
The `s/old/new/` pattern is a substitution command. It finds “old_text” and replaces it with “new_text” in every folder name that matches. The `*/` at the end tells `rename` to look at all items in the current directory.
You can also use regular expressions for more complex patterns. For instance, to add a prefix to all folders:
rename 's/^/backup_/' */
This adds “backup_” to the beginning of every folder name. The `^` symbol represents the start of the string.
Dry Run With Rename Command
Before making changes, it’s wise to do a dry run. The `-n` flag shows what would happen without actually renaming anything:
rename -n 's/2023/2024/' */
This prints the proposed changes so you can review them. If everything looks correct, remove the `-n` flag to execute the rename.
Another useful flag is `-v` for verbose output, which shows each rename as it happens:
rename -v 's/old/new/' */
This helps you track what’s being changed, especially when dealing with many folders.
Renaming Folders With Spaces And Special Characters
Folder names with spaces or special characters require extra care. The shell interprets spaces as separators between commands and arguments, so you need to escape them properly.
There are three ways to handle spaces:
- Quotes: Enclose the entire name in double or single quotes.
- Backslashes: Place a backslash before each space.
- Tab completion: Press Tab after typing part of the name to auto-complete it.
For example, to rename “my folder” to “your folder”:
mv "my folder" "your folder"
Or with backslashes:
mv my\ folder your\ folder
Special characters like `!`, `@`, `#`, `$`, `%`, `^`, `&`, `*`, `(`, `)`, `{`, `}`, `[`, `]`, `|`, `\`, `;`, `’`, `”`, `<`, `>`, `/`, `?`, and `~` also need escaping. The safest approach is to always use quotes around folder names that contain anything other than letters, numbers, underscores, or hyphens.
For example, to rename a folder called “project#1” to “project#2”:
mv "project#1" "project#2"
If you forget the quotes, the shell might interpret the `#` as the start of a comment, causing errors.
Renaming Folders With Find And Mv
When you need to rename folders that match specific criteria across multiple directories, combining `find` with `mv` is a powerful approach. This is useful for batch renaming in complex directory structures.
The basic pattern is:
find /path -type d -name "pattern" -exec mv {} /new_path/ \;
For example, to find all folders named “temp” and rename them to “temporary”:
find /home -type d -name "temp" -exec mv {} /home/temporary \;
But this has a problem: it moves all folders to the same location. A better approach uses a shell loop:
find /home -type d -name "temp" -exec sh -c 'mv "$1" "${1%/*}/temporary"' _ {} \;
This renames each “temp” folder to “temporary” in its original location. The `${1%/*}` part extracts the directory path from the full path.
For more complex renaming, you can use a while loop with `find`:
find /home -type d -name "old*" | while read folder; do mv "$folder" "${folder/old/new}"; done
This renames all folders starting with “old” to start with “new” while keeping the rest of the name.
Using Find With Rename Command
You can also pipe `find` results to the `rename` command for even more flexibility:
find /home -type d -name "*.bak" -print0 | xargs -0 rename 's/\.bak$//'
This finds all folders ending with “.bak” and removes the extension. The `-print0` and `-0` flags handle spaces in filenames safely.
This combination is especially usefull for system administrators who need to rename folders across large directory trees.
Renaming Folders Using A Bash Script
For repetitive renaming tasks, creating a bash script saves time and reduces errors. A simple script can handle complex renaming logic that would be difficult with single commands.
Here’s a basic script that renames folders by adding a date prefix:
#!/bin/bash
for folder in */; do
mv "$folder" "$(date +%Y%m%d)_$folder"
done
Save this as `add_date_prefix.sh`, make it executable with `chmod +x add_date_prefix.sh`, and run it in the directory containing the folders you want to rename.
For more advanced renaming, you can use parameter expansion to manipulate folder names:
#!/bin/bash
for folder in *; do
if [[ -d "$folder" ]]; then
new_name="${folder// /_}"
mv "$folder" "$new_name"
fi
done
This script replaces all spaces in folder names with underscores. The `${folder// /_}` syntax performs a global substitution.
You can also add error handling to your scripts:
#!/bin/bash
for folder in *; do
if [[ -d "$folder" ]]; then
if mv "$folder" "prefix_$folder"; then
echo "Renamed $folder successfully"
else
echo "Failed to rename $folder" >&2
fi
fi
done
This provides feedback and captures errors, making debugging easier.
Renaming Folders With Graphical File Managers
If you prefer a graphical interface, most Linux file managers support folder renaming. The process is similar to other operating systems:
- Nautilus (GNOME): Right-click the folder and select “Rename,” or press F2.
- Dolphin (KDE): Right-click and choose “Rename,” or press F2.
- Thunar (XFCE): Right-click and select “Rename,” or press F2.
- PCManFM (LXDE): Right-click and choose “Rename,” or press F2.
You can also rename folders by clicking the name twice slowly (not a double-click) in most file managers. This enters inline editing mode.
For bulk renaming in graphical mode, some file managers have built-in tools. Nautilus has a “Rename” option in the right-click menu that allows you to find and replace text in multiple folder names. Dolphin has a similar feature under “Edit” > “Rename.”
If your file manager doesn’t support bulk renaming, you can install a dedicated tool like `gprename` or `krename` for more advanced graphical renaming.
Common Mistakes And How To Avoid Them
Even experienced Linux users make mistakes when renaming folders. Here are the most common pitfalls and how to avoid them:
- Accidentally moving instead of renaming: If the new name matches an existing folder, `mv` moves the source folder inside it. Always check for name conflicts first.
- Forgetting quotes for spaces: This causes the command to fail or rename only part of the folder. Always use quotes or escape spaces.
- Using relative paths incorrectly: When you’re not in the expected directory, relative paths can point to the wrong location. Use absolute paths when in doubt.
- Overwriting important folders: The `mv` command doesn’t warn you if you’re about to overwrite an existing folder. Double-check before executing.
- Case sensitivity issues: Linux filenames are case-sensitive. “Folder” and “folder” are different names. Be consistent with capitalization.
To avoid these mistakes, always use the `-v` (verbose) flag with `mv` to see what’s happening. For critical operations, do a dry run with `rename -n` or test your script on a copy of the data first.
Renaming Folders With Symbolic Links
If a folder has symbolic links pointing to it, renaming the folder will break those links. The links still point to the old path, which no longer exists. You have two options:
- Update the links manually: Use `ln -sf` to recreate each link with the new path.
- Use relative symlinks: If the links are relative, they might still work if the folder structure hasn’t changed.
For example, if you have a symlink at `/home/user/link_to_project` pointing to `/home/user/project_old`, and you rename the folder to `project_new`:
mv project_old project_new
The symlink is now broken. To fix it:
ln -sf /home/user/project_new /home/user/link_to_project
You can automate this with a script that finds and updates broken symlinks after renaming.
Renaming Folders With Case Changes
Renaming a folder to change only its case (e.g., “Project” to “project”) can be tricky on case-insensitive filesystems. The `mv` command might think the source and destination are the same.
To rename a folder to a different case, use a temporary name as an intermediate step:
mv Project temp_name
mv temp_name project
This works because the temporary name is different from both the old and new names. Alternatively, you can use the `rename` command with a Perl expression:
rename 'y/A-Z/a-z/' Project
This converts uppercase letters to lowercase. The `y` operator translates characters from one set to another.
Frequently Asked Questions
Can I rename a folder while it’s in use?
Yes, you can rename a folder even if it’s being used by a process. The rename operation only changes the directory entry, not the inode. However, any processes that have the folder open will still use the old path until they close and reopen it.
What’s the difference between mv and rename?
The `mv` command renames a single folder at a time and is simpler to use. The `rename` command uses Perl expressions and can rename multiple folders at once based on patterns. For bulk operations, `rename` is more efficient.
How do I undo a folder rename?
There’s no built-in undo for `mv`. To revert a rename, you must rename the folder back to its original name. If you’re unsure, use the `-i` (interactive) flag with `mv` to prompt before overwriting, or keep a backup of folder names before making changes.
Can I rename a folder using a GUI?
Yes, most Linux desktop environments have file managers that support renaming via right-click or F2. For bulk renaming, you may need additional tools like `gprename` or `krename`.
Why does mv sometimes move folders instead of renaming?
If the destination name matches an existing folder, `mv` interprets the command as “move the source folder inside