rename directories in linux 1

How to Rename Directories in Linux

On any operating system, everything is in the form of directories. Naming your directories efficiently is one way to keep your file system organized and well structured. In this article, we will be discussing how to rename directories in Linux.

Overview

One of the most basic tasks to perform on any operating system is renaming directories. The Linux CLI offers various ways to rename directories using scripts and commands. If your Linux distro comes with a desktop environment, you most likely would have a file manager already installed, with this, you may also rename directories via GUI.

There are several commands you can use to rename folders or directories on Linux. There is no such thing as a traditional command to use for renaming directories. In this article, we will cover the commands “rename” and “mv”. Yes, the command “mv” is used not only for moving files from one directory to another but also to rename directories.

Many users are more comfortable utilizing the GUI on any operating system for their usual tasks. Conveniently, almost all distributions of Linux have a version that comes with a desktop environment. In this article, we will also discuss how to rename directories via GUI using a file manager on Linux.

Prerequisites

  • A Linux distribution that comes with a desktop environment
  • A sudo privileged user account
  • Access to the Command Line Interface / Terminal
  • “rename” and “mv” command installed

How to Rename Directories on Linux

First, we will discuss one of the simplest and most basic ways to rename directories on Linux, and that would be using the mv command. Next to that would be renaming directories via GUI or using a file manager. Lastly, for something a bit advanced, we will be discussing how to rename directories on Linux using the rename command.

Rename Directories Using mv Command

The mv command is not something dedicated to renaming folders or directories on Linux. It is basically used for moving objects like folders or files from one location/path to another. It is a multi-purpose command, and in Linux, this is simply how things are, the command used for moving is also the same command used for renaming directories or folders.

How to Create New Directories

Before anything else, let us first create a couple of folders for us to work on. For our example, we’ll be naming our folders as “SampleFolder” and “SampleFolder2”. Use the command below to create new folders in the home directory. We will also use the ls command to list all files and folders.

Syntax:

$ mkdir <FOLDER_NAME>
$ ls <OPTIONS>

Example:

$ mkdir SampleFolder

and 

$ mkdir SampleFolder2
rename directories in linux

Here you can see that we have successfully created two new folders or directories in the Home directory. 

How to Move Directories on Linux

Next, we will move “SampleFolder2” into “SampleFolder” using the mv command. We will use the command below to move a directory/folder from one location to another.

Syntax:

$ mv <DIRECTORY TO BE MOVED> <TARGET DIRECTORY>

Example:

$ mv SampleFolder2 SampleFolder

How to Rename Directories using the mv command on Linux

To rename a directory using the mv command, the target directory must not be already existing. If the target directory already exists, the mv command will move the source directory along with all of its contents into it. 

This is basically how the mv command works when moving and renaming directories. If you try to move a folder to a non-existing folder, the folder gets renamed, if it exists, the folder gets moved. Use the command below to rename directories on Linux.

Syntax:

$ mv <DIRECTORY TO BE RENAMED> <NEW DIRECTORY NAME>

Example:

$ mv SampleFolder Renamed_With_MV
rename directories in linux

In this screenshot, we have moved “SampleFolder2” from the home directory to “SampleFolder”, which is also located in the home directory. Then afterward, we rename the directory from “SampleFolder” to “Renamed_With_MV”.

ubuntu

You can see in this screenshot that SampleFolder2 stayed intact even after renaming the folder it was moved into. If SampleFolder2 contains files and folders, they would also stay intact.

How to Rename Directories Using a File Manager

If you are more comfortable performing tasks from the desktop environment or via GUI, you can also use the file manager on your Linux distro. In the screenshot below, you can see that if you right-click on a folder or directory, you will get a menu of options to choose from, which includes the option to rename the folder.

file manager

Upon clicking on “Rename…”, the entire folder name gets highlighted immediately for you to change it. As you can see in the screenshot below, you may choose to change only a part of the folder name or change the folder name entirely. 

desktop

For our example, we will rename the folder from “Renamed_With_MV” to “Renamed-via-GUI”.

renamed

In this screenshot, you can see that we have successfully renamed the folder by changing the part “_With_MV” to “-via-GUI”. You simply press enter or click somewhere else to save the new name you have entered.

GUI

Here you can see that SampleFolder2 remained intact after renaming its parent directory. All of SampleFolder2’s contents should also be intact after renaming its parent folder.

How to Rename Directories Using the rename Command

The rename command is a bit more advanced than the mv command, using the rename command, you will be able to rename multiple directories in one go.

Install rename command on Linux

The rename command may or may not be readily available on your Linux distribution, but it is pre-installed on most. If you need to install the rename command, you may use the command below to do so. For installing this command, you will need a sudo privileged user account.

Syntax:

$ sudo apt install rename

In the example below, we have created four sample directories that we can rename using the rename command. We have named our directories “SampleFolder1”, “SampleFolder2”, “SampleFolder3”, and “SampleFolder4”.

rename directories in linux

In this screenshot, you can see the terminal on the left and the file manager on the right which shows our four sample directories. We used the ls command to verify the names of our sample directories.

Rename Multiple Directories Using the rename Command

Use the command below to change the Letter case of all four sample directories in one go. In this example, we will use “rename” with how we want the directories and the target directories to be renamed.

Syntax:

$ rename <EXPRESSION> <DIRECTORY>

Example:

$ rename ‘y/a-z/A-Z/’ *
rename directories in linux

In this screenshot, you can see that we have successfully changed the name of all four sample directories to upper case letters.

Now, let’s change all four directory names from upper case letters to lower case letters. To do so, we will use the command below.

Syntax:

$ rename ‘ARGUMENT / FROM EXPRESSION / TARGET EXPRESSION /‘ *

Example:

$ rename ‘y / A-Z / a-z /’ *
rename directories in linux

Here you can see that the directory names were changed from upper case letters to lower case letters by changing the command expression.

Ok, now let’s try another way to use the rename command. This time let’s change the name of all four sample directories from “samplefolder” to “DIRECTORY”. Use the command below for this task.

Syntax:

$ rename <OPTION> ‘ ARGUMENT / FROM EXPRESSION / TARGET EXPRESSION / ‘ *

Example:

$ rename -v ‘s / samplefolder / DIRECTORY /’ *
rename directories in linux

In the screenshot above, you can see that we have changed the name of all four sample directories from “samplefolder1”, “samplefolder2”, “samplefolder3”, and “samplefolder4” to “DIRECTORY1”, “DIRECTORY2”, “DIRECTORY3”, and “DIRECTORY4”, all in one go. This command comes in handy if, let’s say, for example, you need to rename hundreds of directories.

And that’s about it for this tutorial. We looked at how to rename Linux directories via GUI and CLI, and discussed how to use the mv command. We also discussed the different ways to use the rename command and how to install the rename command on Linux.

If this guide helped you, please share it. ?

Leave a Reply
Related Posts