Ubuntu

How to Create a Folder using Terminal in Ubuntu 22.04?

Folders, also known as directories in Linux jargon, are primarily used to organize and save files. In Ubuntu 22.04, mkdir (Make Directory) command is employed to create a directory. The “mkdir” command facilitates additional options that further assist users in creating a folder(s). This article will demonstrate different ways of creating folders using Terminal in Ubuntu 22.04 LTS. This article will discuss:

  • How to Create a Folder/Directory in Ubuntu 22.04?
  • How to Create Multiple Folders/Directories in Ubuntu 22.04?
  • How to Create a Folder/Directory in Another Location in Ubuntu 22.04?
  • How to Create Nested Folders/Directories in Ubuntu 22.04?
  • How to Create Folders/Directories with Specific Permissions in Ubuntu 22.04?
  • How to Create a Folder/Directory Only When It Does Not Exist in Ubuntu 22.04?

How to Create a Folder/Directory in Ubuntu 22.04?

mkdir command is used to create single or multiple directories by taking directory names as its arguments along with an option and creating a directory/directories accordingly. The syntax for the mkdir command for creating a directory is as follows:

$ mkdir <DirectoryName1><DirectoryName2>

For example, to create a folder “directory1”, the following command will be used

$ mkdir directory1

From the above image, it can be observed that a folder “director1” is created.

How to Create Multiple Folders/Directories in Ubuntu 22.04?

Users can create multiple folders by using the mkdir command discussed in the above section several times but this is not a good approach as it consumes a lot of time. This can be avoided by either adding folder names separated by spaces or by folder names enclosed in curly brackets {}, and comma(,) separated as shown in the syntax below:

$ mkdir <DirectorName1> <DirectorName2> <DirectorName3>

or

$ mkdir {<DirectorName1>,<DirectorName2>,<DirectorName3>}

For example, to create a folder “directory2”, “directory3”, and “directory4” the following command will be used

$ mkdir {directory2,directory3,directory4}

From the above image, it can be observed that a folder “director2”, “director3”, and “director4” are created.

How to Create a Folder/Directory in Another Location in Ubuntu 22.04?

A folder in another location is created by mkdir command followed by the absolute path as shown by the below syntax:

$ mkdir <path>

For example, to create a folder “directory1” in another director, i.e., “Downloads”, the following command will be used

$ mkdir /home/linuxuser/Downloads/directory1

From the above image, it can be observed that a folder “director1” is created in the “Downloads Folder”.

How to Create Folders/Directories with Specific Permissions in Ubuntu 22.04?

By default, all directories have rwx permissions, i.e., read, write, and execute permission for a user. A user can specify the permissions/mode by using -m flag while creating a new folder. After a folder/directory is created, the permissions can be changed by using the chmod command by executing the following command:

$ mkdir -m 700 directory5

From the above image, it can be observed that a folder “director5” is created with special permissions that the user who has created the file is able to read, write, and execute that file only. More information about permissions and ownership in Ubuntu can be checked from the articles: How to Change File Permissions and Ownership in Linux/Ubuntu? and How does Chmod 777 Work in Linux/Ubuntu?

How to Create a Folder/Directory Only When It Does Not Exist in Ubuntu 22.04?

It happens frequently that while creating multiple folders, a folder or a subfolder already exists. The “-p flag” followed by the mkdir command is used to create a parent folder along with the target folder if it does not exist. A parent folder is a folder that is above another folder in a directory/folder tree. If the “-p” flag is not used and a parent or target folder doesn’t exist, the following error occurs

$ mkdir {directory6/sdirectory1}

In order to avoid the error, the -p flag can be added followed by mkdir command as follows

$ mkdir -p directory6/sdirectory1

From the above image, it can be observed that both parent (directory6) and target folders (sdirectory1) are created as both didn’t exist previously. Additionally, if one or few folders are missing then only those will be created:

$ mkdir -p directory6/sdirectory1/ssdirectory1

From the above image, it can be observed that only the ssdirectory1 folder is created as directory6 and sdirectory1 folders existed previously.

How to Create Nested Folders/Directories in Ubuntu 22.04?

The mkdir command enables the creation of nested folders by running a single command. An example of the creation of nested folders is shown below:

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

From the above image, it can be observed that a nested directory “OS” is created which has two subfolders: Windows and Linux. Linux and Windows further have 3 and 2 sub-folders respectively. Additionally, it is recommended not to use spaces within curly brackets {} while utilizing mkdir command to avoid the creation of folders with special characters.

Conclusion

Single and multiple Folders are created in the Terminal by using the mkdir command in Ubuntu 22.04. Additionally, the “mkdir” command allows options for creating folders with specific permissions, creating a folder only when it doesn’t already exist, and creating nested folders. This article demonstrated different ways of creating folders using Terminal in Ubuntu 22.04 LTS.

 

Similar Posts