Geekdosage
  • Home
  • Computer hardware
    • GPU
      • AMD
      • Nvidia
    • Processor
  • Linux
    • CentOS
    • Ubuntu
  • Docker
  • Computer Software
    • Operating System
      • Windows
      • MacOS
      • Android
  • Gaming
  • Computer Science
    • Network Engineering
    • Comparisons
  • About Us

Geekdosage

  • Home
  • Computer hardware
    • GPU
      • AMD
      • Nvidia
    • Processor
  • Linux
    • CentOS
    • Ubuntu
  • Docker
  • Computer Software
    • Operating System
      • Windows
      • MacOS
      • Android
  • Gaming
  • Computer Science
    • Network Engineering
    • Comparisons
  • About Us
CentOSLinuxUbuntu

How to rename a Directory in Linux?

by GeekDosage January 5, 2021
written by GeekDosage January 5, 2021
Rename a Directory on Linux

Renaming directories on Linux is the primary function that every Linux user performs in their lifetime. The most common way to do the renaming is using the GUI file manager. But when working with head-less distributions, knowing how to rename a directory in Linux will ease your work. In this tutorial, we are going to explain how to use the command-line to rename a directory/folder in the Linux file-system. Ubuntu GUI

Rename Directories on Linux using mv command

The renaming of a directory using the 'mv' command allows the user to specify the directory name and instruct the computer with the directory’s path. 

For example:

$ mv <directory_current_name> <directory_new_name>

In the above command, the mv command instructing Linux to rename'directory_current_name' directory as 'directory_new_name'

When you run this common, the Linux operating system check for the name and destination URL to verify the directory name. If the file exists, you will be able to rename it with the new name.

Let say you want to rename a directory name “temp” on your Linux computer. It is located in your home directory. So your source directory name would be “temp.” 

In the next input, you will mention the path of the directory, as shown below.

$ mv /home/user/temp /home/user/directory

Note: The mv command is considered the safest way to rename any directory without deleting it. 

The command line does not delete the content saved in the directory. It only renames the file with the new name. 

After you run the command, you will see the existing file in your home directory changed with the new name.

Renaming Directory using the find command

Another way of renaming the directory is by using the “find” command. There will be a situation where you do not now know the directory path you want to rename. 

The find command allows you to locate the file on the Linux system. 

Find command is the simplest way to detect the file and get the path. To find the directory, search the name with the “type” option and get the result on your screen.

Use the “mv” command with the “-exec dir” option to locate the file, as shown below.

$ find . -depth -type d -name <source_directory> -execdir mv {} <target_directory> \;

Rename Multiple Directories with mv command

The mv command has a limitation when it comes to renaming the specific directory. You will be able to rename the only single file with this command. To rename more than one file, you need a different command line. 

Using the conjunction with the find command or the inside loops would work in the multiple directories naming process. 

The following example would tell you how to use the Bash for loop to apply the current date to instruct the system to rename all the mention directories. 

Example

#!/bin/bash

for d in *; do 

  if [ -d "$d" ]; then

   mv -- "$d" "${d}_$(date +%Y%m%d)"

  fi

done

Let’s understand the above code.

The first line in the above code creates a loop. It iterates through a list of files. After that, the command line moves to the second line to check if the file is a directory. 

The final line, third line apply the current date to each directory.

Rename Directories using the rename command

If you do not want to use the “mv” command, then the formula you can use is the dedicated built-in command. 

The rename command permits you to rename any directory without any restriction. However, the command might not be directly accessible to the user.

To rename the directories on Linux, you have to use the file and target directory name. 

$ rename <expression> <directory>

For example, if you want to change the file from uppercase to lowercase, the command line would help you make the changes.

Why rename a directory in Linux?

There will be occasions while working on the Linux operating system. You may have to go through various files and applications. Linux gives you the freedom to access any file using the command line.

You do not have to manually search or even make changes going through many files stored in the directory. 

The command line is the shortcut way to apply changes and get the job done. Linux rename directory is one of the command lines used to rename the Linux system’s directory. 

You can easily rename it using the above-given commands and get the job done instantly. 

Moreover, any changes that you do use the “mv” command are a non-destructive process. It means the rename process would not delete a file which there in the directory.

 It only follows the rename instruction and changes the file name as per the written command. 

The find command would help you locate the exact file you are searching for in the Linux system. It would not be easy to find the directory in the folder stack without using the find command. 

The find command saves time and gives you the option to filter the data to find the exact file based on the input you place in the search.

About “mv” command in the Linux

In the Linux operating system, the “mv” command serves as a multi-purpose command that facilitates the users to run the command to move or rename the directory. 

Today the rename folder or directory is not done using the traditional rename command. Instead, the “mv” command is used to rename the file.

There will be an occasion where we create temporary files on the system while working on the Linux system. Over the period, the files become large, and now you want to take some action to avoid trouble in the long run. 

You may rename them in the later process. Hence, the rename process is done through the new “mv” command. 

Several other alternatives do the same job, such as find command, rename command, using Bash, and many more. All of them have a common function. Some command plays multiple roles and grants access to the system file.

Every command line has a predefined job. You have to mention the name of the new file and the path. 

The command line would search for the file name in the system, confirm the name with the given instruction, and make the changes. 

Sometimes the file name is case sensitive, so while inserting the input in the command line, ensure that the name is proper and provides needed information. Else, the command line would not identify the files from the list, and the output will be null. 

man command manual page

Conclusion:

I hope you have gained adequate knowledge about renaming the directories on the Linux system. The command line has the power to rename the directory without needing to do it manually. So it is always recommended to use the command line to save time. 

The most common way that developers use is the “mv” command. It is fast and reliable that protects your data from lost. 

Additionally, the find command is the alternative to the “mv” command. You could locate the directory using the find command, which is not available directly in the system. 

Apply the renamed directory in Linux code while working and get the job done in a minute. 

Read More:

What is a Linux OS? Why Linux is so popular?
How to change the forgotten root password in CentOS 8
GeekDosage
how to rename a directory in linuxlinux rename directoryrename directory linuxrename folder linux
0 comment
0
FacebookTwitterPinterest
previous post
How to Install VirtualBox in Ubuntu 20.04
next post
How to remove/delete user in Linux

Related Posts

2 Simplest Methods to Take/Capture Desktop Screenshot in...

January 17, 2021

How to remove/delete user in Linux

January 11, 2021

How to Install VirtualBox in Ubuntu 20.04

January 3, 2021

How to Create a Private Docker Registry in...

December 23, 2020

How to Build a Docker Image from Dockerfile

December 14, 2020

How to Install Docker in Ubuntu 20.04

December 12, 2020

How to Install Docker on CentOS 7

December 10, 2020

How to change the forgotten root password in...

November 25, 2020

What is a Linux OS? Why Linux is...

August 12, 2020

What is an operating system ? ~ Why...

August 12, 2020

Leave a Comment Cancel Reply

Save my name, email, and website in this browser for the next time I comment.

In This Article

  • 1 Rename Directories on Linux using mv command
  • 2 Renaming Directory using the find command
  • 3 Rename Multiple Directories with mv command
  • 4 Rename Directories using the rename command
  • 5 Why rename a directory in Linux?
  • 6 About “mv” command in the Linux
  • 7 Conclusion:
  • 8 Read More:

Keep in touch

Facebook Twitter Google + Instagram Pinterest Email

Popular Posts

  • How to change the forgotten root password in CentOS 8

  • How to Install Docker on CentOS 7

  • How to Build a Docker Image from Dockerfile

  • Best GPU stress test software to test your GPU

About Geekdosage

Geekdosage is a technology based website which mainly focuses on network engineering, computer science, automation, computer hardware and software and internet technologies. Our purpose is to create a platform where anyone can access the subject areas without barriers.

Recent Posts

  • 2 Simplest Methods to Take/Capture Desktop Screenshot in Ubuntu

    January 17, 2021
  • How to remove/delete user in Linux

    January 11, 2021

Find us

Facebook Twitter Instagram Pinterest Email
  • About Us
  • Contact Us
  • Disclaimer
  • Terms and Conditions
  • Cookie Privacy policy
  • Privacy Policy
  • DCMA

@2021 - All Right Reserved | Designed and Developed by GeekDosage