How to Rename a Directory in Linux

September 28, 2021

Introduction

Renaming a directory is one of the most basic tasks you will perform on any operating system. The Linux terminal offers several different ways to rename directories using commands and scripts.

In this tutorial, we will go over the different methods you can use to rename a directory in Linux through the terminal window.

How to rename directories in Linux

Prerequisites

  • A system running a Linux distribution
  • An account with sudo privileges
  • Access to the terminal window/command line
  • Access to a text editor, such as Vim or Nano

Renaming Directories With the mv Command

The primary function of the mv command in Linux is moving files and directories from one place to another. It uses the following command syntax:

mv [options] [source] [destination]

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

If the destination directory does not exist, the mv command renames the source directory instead. In this case, the syntax changes to:

mv [options] [current directory name] [new directory name]

As an example, let's say we have Directory1, Directory2, and Directory3 in our Home directory:

Checking the content of the Home directory using the ls command

To rename Directory1 into Example_Directory with the mv command, use:

mv Directory1 Example_Directory

There is no output if the command is successful, so we need to use the ls command to verify the name change:

ls -l
Verifying the name change using the ls command

Renaming Directories With the rename Command

The rename command in Linux is a dedicated command used to change the names of files and directories. Using this command makes it easier to rename multiple directories at the same time.

Note: The rename command is not included in all Linux distributions by default. If your system is missing the rename command, install it with:

  • For Ubuntu and Debian, use sudo apt install rename
  • For CentOS and Fedora, use sudo yum install prename
  • For Arch Linux, use sudo pacman -S rename

Renaming a Single Directory With the rename Command

The rename command uses the following syntax:

rename [options] 's/[expression]/[replacement]/' [file name]

The command renames the file by replacing the first occurrence of the expression with the replacement. For example, if we want to rename Directory1 to Example_Directory:

rename 's/Directory1/Example_Directory/' *

In this example, we can see that the rename command syntax consists of several sections:

  • rename: Invokes the rename command.
  • s: Short for substitute, indicates that we are replacing the expression with the replacement.
  • /Directory1: Specifies the expression or the part of the old directory name that you want to replace.
  • /Example_Directory/: Defines the replacement or the new directory name.
  • *: Searches the Home directory for names matching the provided expression.

Verifying the Home directory content with the ls command shows that the directory now has a new name:

Verifying the new directory name with the ls command

Renaming Multiple Directories With the rename Command

The rename command provides a way to rename multiple directories at the same time without using bash scripts. For instance, if we want to rename Directory1, Directory2, and Directory3 to Folder1, Folder2, and Folder3:

rename -v 's/Directory/Folder/' *

In the example above:

  • -v: Invokes the verbose output, listing each step of the process.
  • 's/Directory/Folder/': Replaces Directory in the names of the search results with Folder.
  • *: Searches the Home directory for names matching the provided expression.
Using the rename command to change multiple directory names

The rename command can also translate file names by using the y argument instead of the s argument. In this case, it translates one string of characters into another, character-for-character.

For instance:

rename 'y/abc/def/'

The command above translates every a character into d, every b into e, and every c into f.

In the example below, we translated blank spaces in directory names to underscores (_).

rename -v 'y/ /_/' *
Translating directory names using the rename command

Renaming Directories With the find Command

In case you are not sure where the directory you want to rename is located, using the find command with the mv command lets you search for it and rename it when it's found:

find . -depth -type d -name [current directory name] -execdir mv {} [new directory name] \;

In the example above, -execdir executes the mv command once the find command locates the directory.

For instance, the command below finds and renames Directory1 into Example_Directory:

find . -depth -type d -name Directory1 -execdir mv {} Example_Directory \;
Using the find command to rename a directory

Renaming Directories With Bash Scripts

Using bash scripts is another way of renaming multiple directories at the same time. Unlike the rename command, bash scripts allow you to save a template for future use.

Start by creating the script with a text editor, such as Nano:

sudo nano rename_directories.sh

The following example is a bash script that searches for directories and appends the current date to their name:

#!/bin/bash

#Searches for directories and renames them according to the specified pattern

for d in *
do
    if [ -d "$d" ]
    then
      mv -- "$d" "{d}_$(date +%Y%m%d)"
    fi
done

In the example above:

  • The first line instructs the script to go through all files and directories in the current location.
  • Lines 2 and 3 check for directories.
  • Lines 4 and 5 append the current date to the name of any directory found.

Press Ctrl+X, type Y, and press Enter to close and save the script.

As an example, let's use the script above to change the names of Directory1, Directory2, and Directory3, located in the Example directory.

Start by moving in to the Example directory:

cd Example

Learn more about Linux cd command in our guide on how to change and move to different categories.

Next, execute the script by using the sh command:

sh rename_directory.sh

Using the ls command allows us to verify the name change:

ls -l
Using the ls command to verify the name change

Conclusion

After reading this article, you should know how to rename directories in Linux using commands and bash scripts.

Learn more about using Linux commands in our Linux Commands All Users Should Know {Ultimate List}.

Was this article helpful?
YesNo
Aleksandar Kovačević
With a background in both design and writing, Aleksandar Kovacevic aims to bring a fresh perspective to writing for IT, making complicated concepts easy to understand and approach.
Next you should read
How to List Running Processes in Linux
September 2, 2021

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
March 8, 2021

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