Tech
How to Change Directory Name in Linux Safely.
Changing a directory name in Linux looks simple until the destination already exists, the folder contains spaces, or a script depends on the old path. The safest method is usually the mv command, but the right syntax depends on where the folder is, whether you are renaming one directory or many, and whether you need to avoid overwriting anything. This guide shows how to change directory name in linux from the terminal, from a GUI file manager, and in real-world situations where basic examples often fail.
Quick Overview: How Linux Directory Renaming Works
| Aspect | Key Details |
|---|---|
| Main command | Use mv old_directory new_directory to rename a directory in the same location. |
What mv means |
The mv command moves or renames files and directories, depending on the destination path. |
| Safest basic syntax | Use mv -i old_directory new_directory if you want confirmation before replacing anything. |
| Existing destination folder | If the destination already exists, mv old_directory existing_folder may move the old directory inside it instead of renaming it. |
| Spaces in names | Wrap names in quotes, such as mv "old folder" "new folder". |
| Bulk renaming | Use rename, find, or a Bash loop only after testing with a dry-run command. |
| GUI method | Right-click the folder in Files, Dolphin, Thunar, or another file manager and choose Rename. |
| Common errors | Permission denied, directory not found, destination exists, and directory busy are the most frequent issues. |
The Fastest Way to Change a Directory Name in Linux
The standard way to rename a directory in Linux is to use the mv command followed by the current directory name and the new directory name. The GNU Coreutils manual describes mv as a command that moves or renames files and directories, and its simplest form accepts a source and a destination. That means a directory rename is treated as a move from one pathname to another pathname.
mv old_directory new_directory
For example, if you have a folder named project_old and you want to rename it to project_new, run this command from the parent directory:
mv project_old project_new
This changes the directory name without copying the directory contents. The files inside remain in place under the new path, and the operation is usually instant when the source and destination are on the same filesystem. If the folder is large, the rename can still appear fast because Linux is updating the directory entry rather than duplicating every file.
Rename a Directory Using an Absolute Path
You do not have to be inside the parent folder to rename a directory. You can use the full path to the old directory and the full path to the new directory. This is useful on servers because you may be working from another location in the filesystem.
mv /home/user/oldname /home/user/newname
This format is especially helpful when writing documentation, deployment notes, or admin instructions because there is less ambiguity. A relative path depends on the current working directory, while an absolute path starts from / and points to the exact location. If you are not sure where you are, run pwd before using a relative rename.
Rename a Directory in the Current Folder
When the directory is inside your current working directory, keep the command short. First list the folders with ls, confirm the old name, and then rename it. This reduces mistakes when folder names look similar.
ls
mv reports-old reports-archive
ls
The second ls confirms the change. This habit is useful for beginners, but it also helps experienced Linux users avoid typos on production servers. A directory rename can break scripts, shortcuts, cron jobs, symbolic links, and application paths if those tools still reference the old name.
Rename a Directory with Spaces in the Name
Folder names with spaces need quotes or escaped spaces. Without quotes, the shell treats each word as a separate argument. That is why Old Project should be written as "Old Project" in a command.
mv "Old Project" "New Project"
You can also escape each space with a backslash:
mv Old\ Project New\ Project
Quotes are usually easier to read, especially when directory names include several spaces. They also help with names that include parentheses, ampersands, or other characters the shell might interpret. For scripts, quoting variables is even more important because unquoted paths can split unexpectedly.
Use mv -i to Rename More Safely
The -i option asks before overwriting a destination. The GNU Coreutils manual lists --interactive as the option that prompts before overwriting, while the Linux manual page also describes mv as renaming a source to a destination or moving sources into a directory. This is a practical safety switch when you are unsure whether the new name already exists.
mv -i old_directory new_directory
If there is a conflict, Linux asks for confirmation instead of silently continuing. This matters more when you rename files, but it is still a good habit when working around important directories. For cautious server work, mv -i is often better than the bare mv command.
What Happens If the New Directory Name Already Exists?
This is the mistake most short tutorials do not explain clearly. If the destination name already exists and is a directory, a normal mv old_directory new_directory command may move old_directory inside new_directory instead of renaming it. Ask Ubuntu’s long-running answer thread highlights this exact caveat, and GNU’s documentation explains that if the last argument is a directory and -T is not used, mv treats it as a target directory.
For example, this command can create new_directory/old_directory if new_directory already exists:
mv old_directory new_directory
To reduce that ambiguity, check first:
ls -ld old_directory new_directory
If you want the destination to be treated as a normal destination name rather than a target folder, GNU mv supports -T, also called --no-target-directory. This tells mv not to treat the destination as a directory container. It is a strong safety option on GNU/Linux systems when you want a true rename operation.
mv -T old_directory new_directory
Rename a Directory Only If the New Name Does Not Exist
If your goal is to avoid overwriting or merging into anything, use -n with GNU mv. The --no-clobber option tells mv not to overwrite an existing destination. GNU documentation notes that -i, -f, and -n interact, and the final one specified is the one that takes effect.
mv -n old_directory new_directory
This is helpful in scripts where an accidental overwrite would be worse than a failed rename. You can check the result afterward by testing whether the new directory exists. For critical automation, combine this with clear error handling rather than assuming the rename happened.
if mv -n old_directory new_directory; then
echo "Directory renamed."
else
echo "Rename failed or destination already exists."
fi
Rename a Directory with Different Capitalization
Changing only the case of a directory name can be tricky on some filesystems, especially when working with mounted drives, shared folders, or case-insensitive environments. A common workaround is to rename the folder to a temporary name first, then rename it to the final case. This avoids confusion when the filesystem or tool does not recognize case-only changes cleanly.
mv FolderName FolderName_tmp
mv FolderName_tmp foldername
This method is also useful when Git does not detect a case-only directory rename as expected. On Linux-native filesystems such as ext4, case sensitivity is normally expected, but cross-platform projects often involve macOS, Windows, WSL, Docker volumes, or network shares. The temporary-name method keeps the rename explicit.
Rename a Directory in Git Projects
If the directory belongs to a Git repository, use git mv when you want Git to track the rename cleanly. The filesystem command mv still works, but git mv stages the rename in a way that is easier to review. This is especially useful when changing folder names in a codebase.
git mv old_directory new_directory
git status
For case-only renames, the two-step method is often safer:
git mv FolderName FolderName_tmp
git mv FolderName_tmp foldername
git status
The final git status confirms what Git sees before you commit. This avoids confusing pull requests where the folder appears deleted and recreated unnecessarily. It also helps teammates on case-insensitive systems receive the rename correctly.
Rename a Directory You Do Not Own
If you get a permission error, the issue is usually ownership or write permission on the parent directory. Renaming a directory changes an entry in its parent folder, so you need permission to modify that parent location. You can inspect ownership and permissions before using elevated privileges.
ls -ld old_directory
ls -ld .
If the directory is in a system path, you may need sudo:
sudo mv old_directory new_directory
Use sudo carefully because it raises the risk of renaming the wrong system folder. Do not use elevated privileges just to silence an error. First confirm the exact path, the purpose of the directory, and whether services or applications depend on it.
Rename a Directory That Is “Busy” or Currently in Use
Linux may allow a directory rename even while files are open, but applications can still break if they expect the old path. A terminal session, running process, web server, database, or script may be using the directory. Before renaming a live application folder, check active processes and service paths.
pwd
lsof +D /path/to/old_directory
If lsof is not installed, you can install it through your distribution’s package manager or use process checks relevant to your service. For production systems, stop the service first, rename the directory, update configuration files, and then restart the service. This prevents hidden path errors that only appear after the rename.
Rename Multiple Directories with a Bash Loop
For several simple directory renames, a Bash loop can be easier than typing commands one by one. The safest approach is to preview the old and new names before executing the actual mv. This is important because one bad pattern can rename many folders incorrectly.
for dir in project_*; do
echo mv "$dir" "${dir/project_/client_}"
done
If the preview looks correct, remove echo:
for dir in project_*; do
mv "$dir" "${dir/project_/client_}"
done
This example changes names such as project_alpha to client_alpha. The quotes around $dir protect directory names that contain spaces. Avoid running bulk rename loops as root unless there is no safer option.
Rename Multiple Directories with rename
The rename command can be useful for pattern-based renaming, but it is also a common source of confusion. Some Linux systems use a substring-based rename, while others provide a Perl-expression version. The man7 Linux page describes a rename version that replaces the first occurrence of a substring, while the Arch Perl rename manual describes a version that accepts Perl expressions and supports dry-run output.
Check your version first:
rename --version
man rename
With a substring-style rename, the pattern may look like this:
rename old new old_*
With Perl-style rename, a dry run may look like this:
rename -n 's/^old_/new_/' old_*
The -n or dry-run option is valuable because it shows what would change before anything is renamed. Do not copy a rename command from a tutorial until you know which version your distribution uses. This one detail prevents many bulk-renaming mistakes.
Rename Directories Found with find
The find command is useful when directories are nested in subfolders. You can search for matching directory names and then run a rename action. The safe way is to print matches first.
find . -type d -name "old_*" -print
Once the matches look correct, you can use a loop:
find . -type d -name "old_*" -print0 | while IFS= read -r -d '' dir; do
parent=$(dirname "$dir")
base=$(basename "$dir")
mv "$dir" "$parent/${base/old_/new_}"
done
This handles spaces more safely because it uses null-delimited output with -print0. It also keeps each renamed directory in its original parent folder. For complex folder trees, test on a copy or a small sample before applying the command widely.
Rename a Directory from the Linux GUI
You can also rename a directory without using the terminal. Open your file manager, right-click the folder, choose Rename, type the new name, and press Enter. This works in common Linux desktop environments such as GNOME Files, KDE Dolphin, Xfce Thunar, Cinnamon Nemo, and others.
The GUI method is best for quick local changes where you can visually confirm the folder. It is not ideal for servers without a desktop environment or for repetitive renaming tasks. For bulk changes, command-line tools are more precise and easier to document.
Common Errors When Changing Directory Names
The error No such file or directory usually means the old name is typed incorrectly or you are in the wrong location. Run pwd to check your current directory, then run ls to confirm the folder name. Remember that Linux paths are usually case-sensitive, so Projects and projects can be different names.
The error Permission denied means your user cannot modify the directory entry or parent folder. Check permissions with ls -ld before using sudo. If you are on a shared server, changing ownership or permissions without understanding the application can create a bigger problem than the rename.
The message about an existing destination means the new name is already taken. Decide whether you want to move the old directory inside the existing folder, replace an empty destination, or choose a different name. GNU documentation notes that mv only replaces destination directories when they are empty, while conflicting populated directories are skipped with a diagnostic.
Best Practices Before Renaming Important Directories
Before renaming an important directory, check whether anything depends on the old path. Search configuration files, scripts, cron jobs, Docker files, systemd units, and application settings. A successful rename can still cause failures if another tool expects the old location.
Create a backup or snapshot when the folder contains critical data. For simple home-directory folders this may be unnecessary, but for production application folders it is worth the extra step. A rename is easy to reverse only when you know exactly what changed.
Use clear naming rules for directories. Lowercase words, hyphens, and underscores are easier to handle than spaces and mixed punctuation. Good names reduce quoting problems, script errors, and confusion when paths appear in logs.
Conclusion: Key Takeaways for Renaming Linux Directories
- Use
mv old_directory new_directorywhen you need the simplest way to rename a directory in the same location. - Add
-i,-n, or-Twhen you need safer behavior around existing destination names. - Put quotes around directory names with spaces or special characters so the shell treats each path correctly.
- Check permissions, active processes, scripts, and configuration files before renaming important system or application directories.
- Test bulk renaming commands with a preview or dry run before changing multiple directories at once.
FAQs
How do I change a directory name in Linux terminal?
Use the mv command with the old directory name followed by the new directory name, such as mv old_directory new_directory. If the folder is not in your current location, use the full path, such as mv /home/user/oldname /home/user/newname. Add quotes around names with spaces, and use mv -i if you want a confirmation prompt before a possible conflict.
Is rename the same as mv in Linux?
No, mv and rename are different tools even though both can change names. The mv command is best for renaming one file or directory, while rename is usually used for pattern-based or bulk renaming. The confusing part is that different Linux distributions can provide different versions of rename, so always check man rename or rename --version before using examples from another system.
How do I rename a folder in Ubuntu?
In Ubuntu, open Terminal and run mv old_folder new_folder from the folder’s parent directory. You can also open the Files app, right-click the folder, choose Rename, type the new folder name, and press Enter. For important folders, check that the new name does not already exist before running the command.
Why does mv move my directory instead of renaming it?
This happens when the destination name already exists as a directory. In that case, mv old_directory existing_directory can place the old directory inside the existing one instead of changing its name. To avoid confusion, check with ls -ld old_directory new_directory, choose a unique new name, or use mv -T old_directory new_directory on GNU/Linux when you want the destination treated as a final pathname rather than a target folder.
-
Fashion9 years agoThese ’90s fashion trends are making a comeback in 2017
-
Entertainment9 years agoThe final 6 ‘Game of Thrones’ episodes might feel like a full season
-
Fashion9 years agoAccording to Dior Couture, this taboo fashion accessory is back
-
Entertainment9 years agoThe old and New Edition cast comes together to perform
-
Business9 years agoUber and Lyft are finally available in all of New York State
-
Sports9 years agoPhillies’ Aaron Altherr makes mind-boggling barehanded play
-
Entertainment9 years agoDisney’s live-action Aladdin finally finds its stars
-
Sports9 years agoSteph Curry finally got the contract he deserves from the Warriors
