Linux Commands Ubuntu

How to Create a Directory and Subdirectory in Linux/Ubuntu?

Directories, also known as folders, are locations to store files in an Operating System. Similarly, a Subdirectory is a directory within a directory. In Unix/Linux systems, mkdir command is used to create directories and subdirectories using the command line. In this article, we will demonstrate different ways of creating a directory and subdirectory on Linux/Ubuntu 22.04 LTS.

Prerequisites

In order to follow this article, you will require:

  • A system with Linux/Ubuntu 22.04 LTS Installed.
  • Basic knowledge of command line usage.

How to Create/Make a Directory and Subdirectory in Linux/Ubuntu?

Directories and subdirectories can be created In Linux/Ubuntu 22.04 by following two methods:

  • Method 1: Create a Directory and Subdirectory by Using Command Line
  • Method 2: Create a Directory and Subdirectory by Using Ubuntu’s Desktop’s File Manager

We will discuss both methods in the following sections.

Method 1: Create a Directory and Subdirectory by Using Command Line

mkdir (Make Directory) command is used to create directories and subdirectories using the command line in Unix/Linux systems. One or more directory names can be passed as arguments to mkdir command. The syntax for the mkdir command for creating a directory and subdirectory is as follows:

$ mkdir [Option] [Directory Name]
$ mkdir [Option] [Directory Name] / [Sub Directory Name]

mkdir offers various options for creating directories that are demonstrated in the following sections:

How to Create/Make a New Directory in Linux?

A new directory is created by passing single or multiple directory names as arguments to mkdir command. For example, if we want to create a directory by the name of “directory1” we can use the following command:

$ mkdir directory1

We can verify the new directory creation by executing the following command:

$ ls -l

This will create a directory in the current working directory. If a user wants to create a directory at any other location, the absolute path must be provided, as shown below:

$ mkdir /home/linuxuser/Documents/directory1

From the above image, it can be seen that the new directory is created at the specified location.

How to Create Multiple Directories?

Users can use the mkdir command several times to create multiple directories, but this is not a good approach as it consumes a lot of time. This can be avoided by adding directory names with mkdir command using a space-separated syntax. For example, if we want to create three directories, directory1, director2, and director3, we will run the following “mkdir” command:

$ mkdir directory1 directory2 directory3

We can verify from the above image that three directories, i.e., directory1, directory2, and directory3 are created.

How to Create Subdirectories in Linux/Ubuntu?

Subdirectories are directories within a directory. We can create a sub-directory and multiple sub-directories by using the following commands:

Create Single Sub-Directory:

$ mkdir directory/subdirectory1

Create Multiple Sub-Directories:

$ mkdir directory/{subdirectory2,subdirectory3,subdirectory4}

Output

From the above image, we can see that sub-directories are created successfully.

How to Create a Parent Directory?

A parent directory is a directory that is one level up from the current directory. The “-p flag” is used to create a parent directory. For example, we can create a directory Country and subdirectories Provinces, Districts, and Cities within it. “-p flag” creates parent directories along with the target directory if it is not there:

$ mkdir -p Country/Provinces/Districts/Cities

The above image shows that the parent directories and the target directory are created successfully.

How to Create a Directory with Specific Permissions in Linux/Ubuntu?

By default, all directories have rwx permissions, i.e., read, write, and execute permission for a user. We can specify mode/permissions while creating a new directory using the “-m flag”. For example, if we want to create a new directory with the user who created the directory to be able to use it, we will use the following command:

$ mkdir -m 700 directory1

The mode can only be specified while creating a directory. After a directory is created, the mode/permissions can only be changed via the chmod command.

How to Create Nested Directory and Subdirectories Tree With One Command

mkdir allows us to create a nested directory and subdirectory tree structures by using one command. An example is shown below:

$ mkdir -p OS/{Windows/{Windows11,Windows10},macOs/{Monterey,Ventura,Catalina},Linux/{Ubuntu, Debian,RedHat}}

We can see that a nested directory “OS” is created which has three subdirectories: Windows, macOS, and Linux subdirectories within it. Windows, macOS, and Linux further have 2, 3, and 3 sub-directories respectively:

Additionally, it is recommended not to use spaces within {} while using mkdir command as it will create directories with special characters.

Method 2: Create a Directory and Subdirectory by Using Ubuntu Desktop File Manager

We can create a directory and sub-directory with ease using Ubuntu 22.04 LTS Desktop file manager. For this purpose, follow the steps mentioned in the following sections:

How to Create a Directory Using Ubuntu Desktop File Manager?

To create a new directory, first, navigate to the location where you desire to create a directory. A new directory can be created by utilizing a mouse’s “right-click”. It will generate a drop-down menu list. From the drop-down menu list, select the “New Folder” option as shown below:

We will be prompted to enter the “Directory Name”. After typing the directory name, we can press the “Create” button to create the directory:

From the following image, you can see that the directory named “Directory1” has been successfully created.

How to Create a Sub-directory Using Ubuntu Desktop File Manager?

A sub-directory is created similarly as the directory is created. We can navigate inside a specific directory and create a sub-directory by mouse “right-click”. After that, we can select the “New Folder” option as follows:

We will be prompted to name the sub-directory. After entering the name, we can press the “Create” button:

As seen from the above image, the sub-directory is successfully created:

This sums up the creation of a directory and subdirectory in Linux/Ubuntu.

Conclusion

Directories and sub-directories are created in Linux/Ubuntu systems either by command line using the “mkdir” command or by using Ubuntu Desktop File Manager. The “mkdir” command enables the creation of a directory/sub-directory, and multiple directories/sub-directories. Additionally, it enables options for creating a parent directory, specifying permissions while creating a directory, and creating nested directory/sub-directory structures. In this article, different ways of creating a directory and subdirectory on Linux/Ubuntu 22.04 LTS were demonstrated.

Similar Posts