How To Rename A Directory Linux – Rename Folder Using Terminal

Linux directory names can be changed quickly using the terminal’s rename functionality, and knowing how to rename a directory linux is a fundamental skill for anyone managing files on this operating system. Whether you’re organizing projects or fixing a typo, the process is straightforward once you understand the core commands.

This guide walks you through every method to rename directories in Linux, from the basic mv command to batch renaming tools. You’ll learn practical steps, avoid common mistakes, and gain confidence navigating the terminal.

Why Renaming Directories In Linux Matters

Directory names serve as labels for your data. A clear, consistent naming system helps you locate files faster and prevents confusion when collaborating. Renaming becomes essential when you:

  • Fix spelling errors in folder names
  • Reorganize project structures
  • Standardize naming conventions across teams
  • Prepare directories for scripts or automation

Linux gives you multiple ways to accomplish this task, each suited to different scenarios.

How To Rename A Directory Linux Using The Mv Command

The mv command is the most common method for renaming directories. Despite its name (short for “move”), it works perfectly for renaming because moving a directory to a new name is essentially the same operation.

Basic Syntax For The Mv Command

The syntax is simple:

mv [options] source_directory target_directory

Replace source_directory with the current name and target_directory with the new name.

Step-By-Step Example: Renaming A Single Directory

  1. Open your terminal (Ctrl+Alt+T on most distributions).
  2. Navigate to the parent directory containing the folder you want to rename using cd.
  3. Type: mv old_folder new_folder
  4. Press Enter. No output means success.
  5. Verify with ls to see the renamed directory.

For instance, if you have a directory named “project_data” and want to rename it to “project_archive”, run:

mv project_data project_archive

Renaming Directories With Spaces In The Name

Spaces require special handling. Use quotation marks or escape the space with a backslash:

mv "my old folder" "my new folder"

Or:

mv my\ old\ folder my\ new\ folder

Both methods work, but quotation marks are easier to read.

Renaming Directories In A Different Location

You can rename a directory without changing your current working directory by providing the full or relative path:

mv /home/user/documents/old_name /home/user/documents/new_name

Using The Rename Command For Batch Operations

When you need to rename multiple directories at once, the rename command is your best friend. It uses Perl expressions to transform names.

Installing The Rename Command

Some distributions don’t include rename by default. Install it with:

  • Debian/Ubuntu: sudo apt install rename
  • Fedora: sudo dnf install prename
  • Arch: sudo pacman -S perl-rename

Basic Rename Syntax

rename 's/old_pattern/new_pattern/' directories

The s/old_pattern/new_pattern/ is a substitution expression similar to search-and-replace.

Example: Replacing Text In Multiple Directory Names

Suppose you have directories named “report_2023”, “report_2024”, and “report_2025”. To change “report” to “summary”:

rename 's/report/summary/' */

This renames all directories in the current folder that contain “report”.

Using Regular Expressions For Complex Renames

You can use regex patterns for more control. For example, to add a prefix “backup_” to all directories starting with “data”:

rename 's/^data/backup_data/' */

Dry Run Mode To Preview Changes

Always test with the -n (dry run) flag before executing:

rename -n 's/report/summary/' */

This shows what would happen without making changes. Remove -n when you’re ready.

Renaming Directories With A Graphical File Manager

Not everyone prefers the terminal. Most Linux desktop environments offer graphical ways to rename directories.

Using Nautilus (GNOME)

  1. Open the Files application.
  2. Right-click the directory you want to rename.
  3. Select “Rename” from the context menu.
  4. Type the new name and press Enter.

Using Dolphin (KDE)

  1. Open Dolphin file manager.
  2. Right-click the folder.
  3. Choose “Rename” or press F2.
  4. Enter the new name and confirm.

Using Thunar (XFCE)

  1. Open Thunar.
  2. Right-click the directory.
  3. Select “Rename…” from the menu.
  4. Type the new name and click “Rename”.

Graphical methods are intuitive but less efficient for batch operations.

Renaming Directories With A Bash Script

For advanced automation, you can write a bash script. This is useful when renaming follows complex rules.

Simple Script Example

Create a file named rename_dirs.sh:

#!/bin/bash
for dir in */; do
  new_name="${dir//old/new}"
  mv "$dir" "$new_name"
done

Make it executable with chmod +x rename_dirs.sh, then run ./rename_dirs.sh.

Script With User Input

#!/bin/bash
echo "Enter pattern to replace:"
read old_pattern
echo "Enter new pattern:"
read new_pattern
for dir in */; do
  new_name="${dir//$old_pattern/$new_pattern}"
  mv "$dir" "$new_name"
done

Common Mistakes And How To Avoid Them

Renaming directories seems simple, but errors can cause data loss or confusion.

Accidentally Moving Instead Of Renaming

If you specify a target path that doesn’t exist, mv will move the directory instead of renaming it. Always double-check the target name.

Overwriting Existing Directories

If a directory with the target name already exists, mv will overwrite it without warning (unless you use the -i flag for interactive mode). Use mv -i to get a prompt before overwriting.

Case Sensitivity Issues

Linux filenames are case-sensitive. “Data” and “data” are different directories. Be precise with capitalization.

Renaming System Directories

Never rename system directories like /etc, /usr, or /var unless you know exactly what you’re doing. This can break your system.

Best Practices For Directory Naming

Adopting good naming habits saves time and reduces errors.

  • Use lowercase letters to avoid case-sensitivity confusion
  • Replace spaces with underscores or hyphens
  • Include dates in YYYY-MM-DD format for sorting
  • Avoid special characters like ! @ # $ %
  • Keep names descriptive but concise

Renaming Directories With Special Characters

Directories with characters like hyphens, underscores, or dots are common. They work fine with mv as long as you handle them correctly.

Hyphens And Underscores

These are safe and don’t require escaping:

mv my-folder my_folder

Dots In Directory Names

Dots are valid but can be confused with file extensions. Use them carefully:

mv project.v1 project.v2

Using Wildcards For Pattern-Based Renaming

Wildcards like * and ? let you rename multiple directories with similar patterns.

Example With The Mv Command

To rename all directories starting with “temp” to “backup”:

for dir in temp*/; do
  mv "$dir" "backup_${dir#temp}"
done

This loops through each directory, removes the “temp” prefix, and adds “backup_”.

Renaming Directories Across The Filesystem

You can rename directories in any location, not just your current directory.

Using Absolute Paths

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

Using Relative Paths

mv ../projects/old_name ../projects/new_name

Automating Renames With Cron Jobs

For periodic renaming tasks, use cron. For example, rename log directories daily.

Cron Job Example

Edit your crontab with crontab -e and add:

0 2 * * * mv /home/user/logs/current /home/user/logs/$(date +\%Y\%m\%d)

This renames the “current” directory to a date-stamped name every night at 2 AM.

Troubleshooting Common Errors

“Permission Denied” Error

You don’t have write permissions for the directory. Use sudo or change permissions with chmod.

“No Such File Or Directory” Error

The source directory doesn’t exist. Double-check the path and spelling.

“Invalid Argument” Error

Usually caused by special characters or syntax errors. Check for spaces or unescaped characters.

Advanced Techniques: Renaming With Find And Exec

Combine find with mv for complex renaming across subdirectories.

Example: Rename All Directories Named “Old” To “New”

find . -type d -name "old" -exec mv {} new \;

This finds every directory named “old” under the current location and renames it to “new”.

Using Find With Rename

find . -type d -name "*temp*" -exec rename 's/temp/temporary/' {} \;

Renaming Directories With Python Or Perl

For programmers, scripting languages offer flexibility.

Python Example

import os
os.rename('old_dir', 'new_dir')

Perl Example

rename('old_dir', 'new_dir');

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 operation doesn’t affect open file handles.

What’s the difference between mv and rename?

mv renames one directory at a time. rename uses Perl expressions for batch renaming with pattern matching.

How do I rename a directory with spaces in Linux?

Use quotation marks around the names, like mv "old name" "new name", or escape spaces with backslashes.

Is it possible to undo a directory rename?

Not directly. You must rename it back manually. Always double-check before executing.

Can I rename directories in bulk without a command?

Some file managers like Nautilus support bulk rename via plugins, but the terminal is more reliable for batch operations.

Final Thoughts On Renaming Directories In Linux

Mastering how to rename a directory linux opens up efficient file management. Start with the mv command for simple tasks, then explore rename for batch jobs. Always verify your commands with ls or dry runs before making changes.

Practice with test directories until you feel comfortable. The terminal becomes less intimidating with each command you learn. Remember to back up important data before performing bulk operations, and you’ll handle directory renames like a pro.