How to Rename a Directory in Linux

May 7, 2024

Introduction

Renaming a directory is an essential file management task in various operating systems, including Linux. The Linux CLI offers several ways to rename directories using different commands. Additionally, there's a graphical way for those who prefer the GUI.

This tutorial covers different ways to rename a directory in Linux.

How to rename directories in Linux

Prerequisites

How to Rename Linux Directory via Command Line

Linux has different commands and methods for renaming directories. Some methods are convenient for renaming one directory, while others are used for bulk renaming.

Below are different examples demonstrating how to rename a Linux directory through the command line.

rename Command

rename is a dedicated command for renaming files and directories. It simplifies bulk renaming by using Perl regular expressions to match directory names.

The command is not available by default on all Linux systems. Use the system's package manager to install it. For example, on Debian or a Debian-based system (such as Ubuntu), run:

sudo apt install rename

The apt command installs it automatically, along with any dependencies.

Note: On Fedora, the package name is prename.

Renaming Single Directory via rename Command

To rename a directory via the rename command, use the following syntax:

rename 's/^[expression]$/[replacemnet]/' [current_name]

Replace [expression] with the original search term, [replacement] with the new term, and [current_name] with the current directory name. The command performs an exact search and replaces the matched substring with the provided new name.

For example, to perform a full rename, enter:

rename 's/^alpha$/beta/' alpha
rename directory Linux command output

The example searches the current directory for the "alpha" directory and replaces "alpha" with "beta." Doing an exact match avoids accidentally renaming directories that contain the search term as a substring.

To do a partial rename, see the following example:

rename 's/^alpha/beta/' alpha_1
partial rename directory terminal output

The command replaces the "alpha" string with "beta" in the "alpha_1" directory name. The partial rename preserves part of the original name and only replaces the provided expression.

Renaming Multiple Directories via rename Command

If there are multiple directories with the same substring, the rename command simplifies bulk renaming. The syntax is:

rename 's/[expression]/[replacement]/' */

Replace [expression] with the search condition and [replacement] with the replacement string. The command searches for the provided substring and replaces any found instance. For example:

rename 's/alpha/beta/' */
batch rename command terminal output

The command finds all directories that contain the word "alpha" and replaces it with "beta".

mv Command

The mv command in Linux is for moving files and directories. It uses the following syntax:

mv [source] [destination]

If the destination directory does not exist, the mv command renames the source directory to the provided destination name.

Note: Some mv versions have the -d option to specify directories. The command still works without it, but it also matches files that have the same name.

For example, to rename a directory:

mv alpha beta
mv alpha beta terminal output

The mv command renames the directory from "alpha" to "beta".

Note: To learn more about using the mv command, check out our guide to moving directories in Linux.

mmv Command

The mmv command is a batch renaming tool for files and directories. To use the command, first install it with:

sudo apt install mmv

The syntax for the command is:

mmv -r '[current_name]' '[new_name]'

The -r option specifies to do a renaming. Use complex wildcard patterns to match multiple directories when stating the current directory name. For example:

mmv -r 'alpha*' 'delta#1'
mmv command batch rename terminal output

The command matches all directories whose name begins with "alpha" and replaces it with "delta". The "#1" captures the wildcard match after "alpha" and adds it to the new name.

find Command

The find command does not have built-in directory renaming options. However, finding a directory's location before renaming it is a common practice.

Use the find command with one of the previously listed renaming methods to locate and rename a directory in a single line. For example:

find . -depth -type d -name [current_name] -execdir mv {} [new_name] \;

The -execdir option executes the mv command once the find command locates the directory in the current location. Replace the location (.), [current_name], and [new_name] placeholders with the actual values.

For example, to find all directories named "alpha" and rename them to "beta", use:

find . -depth -type d -name alpha -execdir mv {} beta \;
find and rename command terminal output

The command searches the current directory for a directory named alpha and renames it to beta if found.

Renaming Directories with Bash Scripts

Bash scripts are another way to rename multiple directories at the same time. They enable using a templated approach when renaming directories.

See the script below for an example:

#!/bin/bash

target_dir="[path]"

# Iterate over each directory in the target directory
for dir in "$target_dir"/*; do
    # Check if it's a directory
    if [ -d "$dir" ]; then
        # Extract the current directory name
        current_name=$(basename "$dir")
        # Define the new name based on your renaming criteria
        new_name="new_${current_name}"
        # Perform the renaming
        mv "$dir" "$target_dir/$new_name"        
        # Optionally, print a message indicating the renaming
        echo "Renamed directory: $current_name to $new_name"
    fi
done
Bash script rename directories code

Each line in the script does the following:

  • Line 1 is the shebang, indicating it's a Bash script.
  • Line 3 is the search directory. The script searches this location for directories. Replace the [path] placeholder with the actual location.
  • Line 6 iterates over each element (files and directories) in the target directory using a for loop.
  • Line 8 checks if the element is a directory. If it is, it continues the renaming procedure.
  • Line 10 extracts the current directory name. If the name is not required for the renaming, leave this line out.
  • Line 13 creates a string containing the new directory name. It appends new_ to the current directory name.
  • Line 15 utilizes the mv command to rename the directory.
  • Line 17 prints an informative message about the renaming operation.

The script checks if a directory exists in the provided location and appends new_ to the directory name. Replace the path and run the Bash script to see the results.

How to Rename a Linux Directory via GUI

If you prefer to rename directories (folders) using the GUI, follow the steps below:

1. Open the application menu. Use the Activities shortcut or press the Super key (Windows key).

2. Search for "Files" (or "Nautilus" on Ubuntu) and press Enter.

Ubuntu activities search nautilus output

3. Use the file manager to navigate to the directory (or directories) that should be renamed. Click through the directories to navigate or enter the path in the address bar.

File navigator example path

4. To edit a single folder's name, click the folder and press F2 to edit. To edit multiple folders, select the folders and press F2 to perform batch renaming.

5. Enter a new name and press Enter or click Rename to confirm the name change for a single directory.

Ubuntu GUI rename single folder

For multiple directories, choose whether to rename using a template or to find and replace text.

Ubuntu folder batch rename GUI

The prompt below shows a preview of the rename operation. Once done, click Rename to apply the changes.

Conclusion

This guide showed how to rename directories in Linux using various commands and Bash scripts. It also demonstrated how to rename folders through the GUI.

Next, see how to copy files and directories in Linux or learn more about using Linux commands.

Was this article helpful?
YesNo
Milica Dancuk
Milica Dancuk is a technical writer at phoenixNAP with a passion for programming. With a background in Electrical Engineering and Computing, coupled with her teaching experience, she excels at simplifying complex technical concepts in her writing.
Next you should read
How to List Running Processes in Linux
May 7, 2024

Tracking and managing running processes is an important part of Linux administration. This tutorial shows different methods you can use to list running processes in Linux.
Read more
How to Check CPU Temperature on Linux
March 10, 2021

High temperatures can damage the sensitive components in your machine. Read our tutorial to learn how to monitor CPU, GPU and HDD temperatures on machines running Linux, by using different utilities.
Read more
How to Ping Specific Port Number in Linux and Windows
September 5, 2024

This tutorial covers the use of several networking tools and utilities for pinging a specific port number. Even though the primary use of the ping command does not involve ports, it can be used...
Read more
How to Set Environment Variables in Linux
December 17, 2020

Environment variables play a significant role in setting up the system and shell configuration at the beginning of every shell session. This tutorial explains how to view, set, manage, and unset environment variables in Linux.
Read more