How Do I Rename a Directory Via the Command Line


Renaming directories and files is the most common practice among computer users. Most of the time this task is performed while managing files and folders. To rename a directory or folder in Linux, there exist multiple methods to do this job. In this learning-based post, we will teach you how to rename a directory via the command line in Linux distributions.

The following two methods will rename a directory through the command line in Linux:

  • Method 1: Rename a Directory with the command “mv”
  • Method 2: Rename a Directory via the command “rename”

How Do I Rename a Directory Via the Command Line?

Below are some examples that will improve your understanding of renaming a directory using a command line in Linux.

Method 1: Rename a Directory with the command “mv”

Display the Directory’s List

Run the command “ls” from your Linux terminal to display the list of available directories in your drive/path:

ls

Create a Test Directory

To create a directory in Linux using a command, run the written below command from the CLI. The flag “-v” will show the creation process of the new directory:

mkdir -v test_May


Verify the New Directory via ls command

Repeat the “ls” command to verify whether the new folder is created or not:

ls

Change Directory using “cd” Command

You can jump into the “test_May” directory using the following command:

cd test_May


Create a New File

The touch command will create a new file through the command line in the Linux. For instance:

touch file1 file2


Show the Available Files

Again use the “ls” command from your Linux terminal to display the files within the “test_May” directory:

ls

Rename a Directory

In Linux distribution, the “mv” command is used to rename a directory. Run the following syntax to rename:

mv test_May test_June


The “-v” can also be used with the “mv” command to display a verbose output. The example code is given below:

mv -v test_May test_June


Verify With the “ls” Command

Execute the command as attached-below, to show the current directories in your drive:

ls

As per the snapshot as mentioned above, the directory name is renamed without any error.

Method 2: Rename a Directory via the command “rename”

In this method, we will guide you on how to install and use the “rename” command to rename a directory in Linux.

Install the “rename” Package Using “apt”

The below-written command will install the “rename” package along with its libraries and dependencies in your Linux:

sudo apt install rename -y


Display the Existing Directories in Your Drive

Before executing the “rename” command, run the “ls” command to display the existing directories:

ls

Rename the Directory via “rename”

Within the double quotes, the “s” stands for the substitute, the first pattern is for search (test_June) while the second pattern is used to replace the name. The last option such as “test_June” is the specified directory on which the operating will be performed:

rename -v 's/test_June/test_May/' test_June


Brief: rename ‘s/current_directroy_name/new_directory_name/’ current_directory_name

Confirm the Rename Directory

With the help of the “ls” command from the terminal will then confirm if the directory “name” has been renamed successfully:

ls

Conclusion

In Linux distributions, you can rename a directory via the command line. There are two different methods to do this job for you, the first is the “mv” command while the second is the “rename” command. The “name” is not the built-in package, thus it can be installed using the “apt install rename”. In this post, we have demonstrated both methods with practical examples.

Print Friendly, PDF & Email