Skip to content

How to rename a directory in Linux?

  • by
  • 3 min read

We all create directories or folders in our systems to store our files in an organized manner. However, some situations may arise where you want to rename the Linux directory to make it more precise and understandable.

Depending on your LINUX system, you can use the Graphical User Interface (GUI) to rename your directory, or you can also use the terminal. Let’s see some ways using which you can rename your directory in a Linux system.

Also read: How to remove a directory in Linux?


Using the mv command

Here mv stands for ‘move’. You can use the mv command to rename your directory. Check the syntax and example given below:

Syntax:

mv [OPTIONS] <location of your source_directory> <location of your destination_directory>

For example, you’d use the following command to rename a directory ‘abc’ to ‘mno’.

mv /abc /mno

Output:

How to rename a directory in linux?

Using shell scripts

You can use shell scripting to rename multiple directories using the loops. It is useful when you want to add a specific word or number, or date in the name of your directories.

The example below will search for all the directories and append ‘_new’ at the end of the directory name. Save this script as rename_script.sh.

#!/bin/bash

for directory in *
do
    if [ -d "$directory" ]
    then
      mv "${directory}" "${directory}_new"
    fi
done

To run the above code, use this command.

bash rename_script.sh

Output:

How to rename a directory in linux?

Using the find command

You can use the find command to rename the directory. Check the syntax and example written below:

Syntax:

find . -depth -type d -name <source directory name> -execdir mv {} <destination directory name> \;

For example, if you’re renaming the pqr_new directory to pqrst, you’d run the following command.

find . -depth -type d -name pqr_new -execdir mv {} pqrst \;

The find command is useful when you don’t know the exact location of your directory.

Output:

How to rename a directory in linux?

Rename directory using the rename command

You can use the rename command to rename single or multiple directories at once. Use the ls command to filter out all the directories with the particular words or letters you want to rename.

Use pipeline to pass the output of ls command to rename command. In the rename command, after ‘s/’ write the name or letters you want to replace, and after the ‘/’ write the letters or name, you want to replace your current directory’s name.

Check the example given below:

ls -d -- *xyz | rename 's/xyz/abc/'

Output:

How to rename a directory in linux?

These were some ways using which you can rename your directory. You can use the GUI to rename your directories, provided the Linux distro you’re using supports a GUI.

Also read: How to rename a file in Linux?

Chetali Shah

Chetali Shah

An avid reader and an engineering student. Love to code and read books. Always curious to learn new things :)

>