Linux Rename Directory Command | Rename Folder in Linux

Rename directory Linux is a simple task that can be accomplished using the “mv” command from the command line. To learn how to rename folder in Linux, read this post.

Updated: 05 Mar, 23 by Susith Nonis 6 Min

List of content you will read in this article:

Linux Rename Directory Command is similar to renaming files in a Linux system. It is one of the simple and commonly performed operations on the Linux system. You can rename directory in linux from the command-line commands or use the GUI file manager. 

In this article, we will focus on various commands that will help to Rename Folder in Linuxy, along with various options that will somehow affect the working of the rename command.

A directory is a location on a computer where files and other folders are stored. In Linux, directories are also called "folders." You can think of a directory as a folder in Windows or macOS.

You are placed in your home directory when opening a terminal window. This is the default location for many users when they start using Linux. Your home directory will be filled with files and folders that store your settings and preferences.

To view the contents of your home directory, type ls at the command prompt. This will list all of the files and folders in your current location if you want to see what is inside a particular folder, type ls followed by the name of the folder. For example, type ls Desktop to see what is inside the Desktop folder.

To navigate to different directories, use the cd command. For example, to change to the Documents directory, type cd Documents. To go up one level in the directory structure (for example, from the Documents directory back to your home directory), type

cd ..

There are several reasons you might want to rename directories. For example, you may want to change the name of a directory to reflect the contents of that directory. Alternatively, you may need to rename a directory to avoid confusion with another similarly named directory. Additionally, renaming directories can help keep your file system organized and tidy.

  • Renaming folders in Linux offers a simple command, “mv” that will solve the purpose of renaming the directories. The “mv” command can be used for renaming and moving the file from one source location to the destination location. 

You can follow the below-mentioned syntax for the “mv” command.

mv [OPTIONS] source destination

  • Suppose you are trying to rename the dir_name1 to dir_name2, as shown below.

mv dir_name1 dir_name2

If the dir_name2 already exists, then the dir_name1 is moved to the existing dir_name2 directory.

  • To rename the folder that is not present in your current working directory, you need to specify the path to that directory while renaming it. You can consider the following example.

mv /home/user/dir_name1 /home/user/dir_name2

You can do it effortlessly if you want to rename a single directory with linux rename folder command. But if you want to rename multiple directories simultaneously, you have to face some challenges if you are a new Linux user.

The “mv” command will help move only one directory at a time. Still, you can use the “mv” command in conjunction with other commands that will solve the purpose of renaming multiple directories at the same time.

  • You can consider the following example, where we have used the bash “for” loop for appending the current date to all the directory names that we are considering renaming and are present within the current working directory.

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

Now we will see how the above bash code will process the directories one by one to rename them. The first line in the above code will iterate through all the files. The second line will check if the specified file is a directory. Then the third line will append the current date to the end of the directory.

  • You can use the “find” command if you want a less complex solution. Consider the following example.

find . -mindepth 1 -prune -type d -exec sh -c 'd="{}"; mv -- "$d" "${d}_$(date +%Y%m%d)"' \;

The find command will pass all the directories to the “mv” command one by one with the help of the “-exec” option. The string{} will take the directory's name currently being processed.

Rename command is considered more powerful than the mv command as it will require the basic knowledge of the regular expressions and make your complex work easier.

The example below will show how you can replace the spaces in the name of the directories present in the current working directory with an underscore.

find . -mindepth 1 -prune -type d | rename 'y/ /_/'

How can I find out the path of my home directory?

Assuming you are using the bash shell, the command to find out the path of your home directory is:

echo $HOME

This will print out the path of your home directory to the terminal.

Well, we have learned how to rename a directory in Linux with simple tasks that can be accomplished using the “mv” command from the command line. But if you want to rename multiple directories simultaneously, you might find it difficult as you need prior knowledge of regular expressions and bash scripting.

This article focuses on two commands for renaming the directories- the “mv” command and the “rename” command.

People are also reading:

Susith Nonis

Susith Nonis

I'm fascinated by the IT world and how the 1's and 0's work. While I venture into the world of Technology, I try to share what I know in the simplest way with you. Not a fan of coffee, a travel addict, and a self-accredited 'master chef'.