How to Create and Rename Directories in Linux?

Let’s look at the various ways to create and rename directories in Linux. In Linux, files are organized in the form of directories and subdirectories. The root directory (“/”) is at the base of the file system. There can be directories within directories, these are called sub-directories. It is important to know how to create and rename these directories when working with Linux.

Methods to Create and Rename Directories in Linux

The command used for creating directories is mkdir, short for make directory. We will see how to use the command in this tutorial.

1. Using mkdir to create a directory

The command is really simple to use. You just need to mention the name of the directory you want to create with the mkdir command.

$ mkdir directory_name
Mkdir

You can use ls command to check the creation of a new directory. One thing to remember is that the directory would be created in the current working directory. You can check your current directory by using the pwd command. You can use the cd command to enter the directory.

Cd And Pwd

2. Creating multiple directories

The mkdir command can also create multiple directories in the same location. For this you just need to specify the directories separated with a space.

$ mkdir directory_name_1 directory_name_2 directory_name_3 
Multiple Dir

This creates directories that are separate and present within the same directory. This is different from directories being contained in the parent directory. We will see this next.

3. Creating a directory within a directory

To create directories that are contained inside a parent directory use the -p flag with mkdir command. This creates directories in such a way that a directory is contained within the one mentioned before it.

$ mkdir -p directory_name_1/directory_name_2/directory_name_3 
Directory Within Directory
Directory Within Directory

You can see that each directory is within the other directory. test_mkdir_3 is the innermost directory.

4. Creating directories with customised permissions

To give permissions at the time of creating the directory use mkdir command with the -m flag. This determines the mode for the directory. This is the same as the chmod command.  700 means that only the creator of the directory will be able to access it.

$ mkdir -m 700 test_dir_4

Renaming a Directory in Linux

To rename a directory in Linux, use the mv command. The syntax for this is:

$ mv <old_name> <new_name>
Rename

test_mkdir_3 is successfully renamed to test_mkdir_4.

Conclusion

In this tutorial we saw how we can create and rename directories in Linux. If you’re interested to learn more abut Linux, continue to follow our LinuxForDevices and become proficient in everything that’s related to Linux!