How To Change The Name Of A Directory In Linux : Linux Directory Rename Commands

Renaming a directory in Linux is a fundamental command for organizing your file system. If you have been wondering how to change the name of a directory in Linux, you are in the right place. This guide covers everything from basic commands to advanced techniques for renaming folders efficiently.

Whether you are a beginner or an experienced user, renaming directories is a daily task. The process is simple once you know the right commands. Let’s start with the most common method.

How To Change The Name Of A Directory In Linux

The primary command for renaming directories in Linux is mv, short for move. Despite its name, mv is used to both move and rename files and directories. To rename a directory, you simply move it to a new name in the same location.

The basic syntax is:

mv old_directory_name new_directory_name

For example, to rename a directory called project to project_2025, you would run:

mv project project_2025

This command works instantly. There is no confirmation prompt by default. The directory and all its contents are renamed.

Important Notes Before Renaming

  • You must have write permissions on the parent directory.
  • The new name must not already exist in the same location, or the mv command will overwrite it.
  • Use absolute or relative paths correctly. For example: mv /home/user/project /home/user/project_2025.
  • If the new name includes spaces, enclose it in quotes: mv "old folder" "new folder".

Now, let’s explore more scenarios and advanced methods.

Using The Mv Command With Paths

When you are not in the same directory as the folder you want to rename, you can specify the full path. This is common when working with scripts or managing multiple locations.

Example:

mv /var/www/html/old_site /var/www/html/new_site

This renames old_site to new_site inside the /var/www/html/ directory. The command works the same regardless of your current working directory.

Renaming Multiple Directories

Renaming multiple directories at once requires a loop or a tool like rename. The mv command handles one rename per execution. For bulk operations, use a bash loop:

for dir in */; do
  mv "$dir" "prefix_$dir"
done

This adds a prefix to every directory in the current location. Be careful with patterns to avoid unintended renames.

Using The Rename Command

The rename command is more powerful for batch renaming. It uses Perl expressions or simple substitution patterns depending on your distribution.

On Debian-based systems (like Ubuntu), the default rename uses Perl. On Red Hat-based systems, it uses a simpler syntax. Check your version with man rename.

Basic Rename Examples

  • Replace spaces with underscores: rename 's/ /_/g' */
  • Change extension of directories (not common but possible): rename 's/\.old$/.new/' */
  • Convert to lowercase: rename 'y/A-Z/a-z/' */

Always test with -n (dry run) first:

rename -n 's/old/new/' */

This shows what would happen without making changes.

Renaming Directories With Special Characters

Linux allows directory names with spaces, hyphens, and other special characters. Renaming these requires proper escaping or quoting.

Examples:

  • Spaces: mv "My Folder" "My Folder Backup"
  • Hyphens: mv my-folder my_folder (no issue)
  • Parentheses: mv "folder (1)" "folder (final)"

If you forget quotes, the shell interprets spaces as separate arguments. This can cause errors or unintended behavior.

Using Tab Completion

To avoid typing complex names, use tab completion. Type the first few characters and press Tab. The shell fills in the rest, including escaping spaces automatically.

This is a huge time saver and reduces typos.

Renaming Directories With Find And Mv

When you need to rename directories across a deep file tree, combine find with mv. This is useful for batch renaming based on patterns or dates.

Example: Rename all directories containing “temp” to “backup”:

find /path -type d -name "*temp*" -exec mv {} {}_backup \;

This finds directories matching the pattern and appends “_backup” to their names. Be cautious with recursive operations to avoid renaming parent directories incorrectly.

Using Find With Execdir

For safer renaming, use -execdir which runs the command in the directory containing the match:

find /path -type d -name "*old*" -execdir mv {} newname \;

This prevents path issues and is more reliable.

Renaming Directories With A Script

For complex renaming tasks, write a simple bash script. This gives you full control and can include error handling.

Example script:

#!/bin/bash
for dir in /path/to/dirs/*/; do
  if [[ -d "$dir" ]]; then
    newname="${dir%/}_renamed"
    mv "$dir" "$newname"
  fi
done

This renames every directory in the path by appending “_renamed”. You can modify the logic to suit your needs.

Adding Confirmation Prompts

To avoid accidents, add a confirmation step:

read -p "Rename $dir to $newname? (y/n) " -n 1 -r
if [[ $REPLY =~ ^[Yy]$ ]]; then
  mv "$dir" "$newname"
fi

This is useful for interactive scripts.

Common Mistakes And How To Avoid Them

Renaming directories seems easy, but errors happen. Here are frequent pitfalls:

  • Overwriting existing directories: If the new name exists, mv moves the source into it or overwrites it. Use -i (interactive) to get a prompt: mv -i old new.
  • Forgetting to escape spaces: Always quote names with spaces or special characters.
  • Renaming the wrong directory: Double-check your current directory with pwd before running commands.
  • Using wildcards carelessly: mv * newname can rename everything. Be specific.

Using The -V Flag For Verbose Output

Add -v to see what mv does:

mv -v old new

Output: renamed 'old' -> 'new'

This helps with debugging and logging.

Renaming Directories In Graphical File Managers

If you prefer a GUI, most Linux file managers allow renaming directories easily. Right-click the folder and select “Rename” or press F2.

This is intuitive but less efficient for bulk operations. For automation, stick with the command line.

Using Nautilus, Dolphin, Or Thunar

  • Nautilus (GNOME): Right-click → Rename
  • Dolphin (KDE): Right-click → Rename or F2
  • Thunar (XFCE): Right-click → Rename

These tools also support batch renaming with patterns in some versions.

Renaming Directories With Case Sensitivity

Linux filenames are case-sensitive. Folder and folder are different. To change case, use mv or rename.

Example:

mv folder Folder

This renames the directory from lowercase to uppercase. Be careful on case-insensitive file systems like some network mounts.

Converting All Directories To Lowercase

Use a loop:

for dir in */; do
  mv "$dir" "${dir,,}"
done

The ,, syntax converts to lowercase in bash 4+. Test first.

Renaming Directories With Timestamps

Appending timestamps is common for backups. Use the date command:

mv project project_$(date +%Y%m%d)

This renames project to project_20250315 (example date). You can customize the format.

Using Variables For Dynamic Names

Store the timestamp in a variable:

ts=$(date +%Y-%m-%d_%H%M%S)
mv data data_backup_$ts

This gives you precise control over the naming convention.

Renaming Directories Across Network Or Mounted Drives

The same mv command works on mounted drives, including NFS, Samba, or external USB. However, permissions and file system limitations may apply.

For example, FAT32 drives do not support case sensitivity or certain characters. Test with a small file first.

Handling Permission Denied Errors

If you get “Permission denied,” use sudo:

sudo mv /protected/dir /protected/newdir

But be careful with sudo—double-check the path to avoid system damage.

Using Alias For Frequent Renames

If you rename directories often, create an alias in your .bashrc:

alias ren='mv -v -i'

Then use ren old new for interactive, verbose renames.

Reload with source ~/.bashrc.

Renaming Directories With Symlinks

If a directory has symbolic links pointing to it, renaming the directory breaks those links. Update the symlinks afterward or use relative paths.

Example: If /home/user/link points to /home/user/project, renaming project to project_new breaks the link. Recreate it:

ln -sfn /home/user/project_new /home/user/link

The -n flag treats the destination as a normal file if it is a symlink.

Renaming Directories In Scripts For Automation

For cron jobs or deployment scripts, renaming directories is common. Always use full paths and error checking.

Example script snippet:

#!/bin/bash
SRC="/var/www/current"
DST="/var/www/backup_$(date +%Y%m%d)"
if [ -d "$SRC" ]; then
  mv "$SRC" "$DST"
  echo "Renamed $SRC to $DST"
else
  echo "Source directory not found"
fi

This is safe and logs the action.

Frequently Asked Questions

What Is The Command To Rename A Directory In Linux?

The command is mv oldname newname. It works for both files and directories. For example: mv docs documents.

Can I Rename A Directory With Files Inside?

Yes, the mv command renames the entire directory, including all its contents. No special flags are needed.

How Do I Rename Multiple Directories At Once?

Use a bash loop or the rename command. For example: for d in */; do mv "$d" "prefix_$d"; done.

What If The New Directory Name Already Exists?

The mv command will either overwrite the existing directory or move the source into it, depending on the situation. Use -i to get a prompt.

Is There A Way To Undo A Directory Rename?

No built-in undo. You must rename it back manually: mv newname oldname. Always double-check before renaming.

Renaming directories in Linux is straightforward once you master the mv command. For simple tasks, use mv directly. For bulk or complex operations, leverage rename, loops, or scripts. Always test with dry runs or verbose flags to avoid mistakes. With practice, you will rename directories quickly and safely, keeping your file system organized.

Remember to check permissions, avoid overwriting, and use quotes for spaces. The command line gives you full control, but a GUI is fine for occasional use. Now you know exactly how to change the name of a directory in Linux using multiple methods. Apply these techniques to streamline your workflow.