How to Create a Directory in Linux? (A Comprehensive Guide: Create a Folder in Linux)

In the Linux system, a directory is a location in which the user stores files and subdirectories. In contrast, Windows users use the word folder instead of a directory. The directory-oriented file system is used in the Linux/Unix operating system. The beginner or Linux system administrator uses some set of commands to create and manage directories. 

Linux is highly favored and utilized for its stability and adaptability in simplifying a variety of tasks. A standout feature of Linux is its terminal, offering the ability to execute commands for tasks ranging from system modifications to downloading required software.

In this tutorial, we will explore different commands and options to create a directory or folder using the terminal. In addition, we will demonstrate how to create a directory in Linux and multiple directories using the “mkdir” command on a Linux system.

Prerequisites

Before using the “mkdir” command on a Linux system, make sure you have the following prerequisites:

1. Running Linux or Unix-like operating systems and having access to the command-line tool “terminal” for running Linux commands.

2. You should have “sudo” and read/write access to create new directories.

Linux “mkdir” Command (Make Folder in Linux)

The Linux create folder command “mkdir” is used to create a directory on a Linux system. This command allows you to create single or multiple directories at the same time on your system. Moreover, it enables you to set permissions or grant access privileges to a specific directory. 

It is important to note that each user should have read/write permissions on a parent directory to create subdirectories or folders in their Linux system. Otherwise, the user will receive a “permission denied” error.

Basic Syntax of the “mkdir” Command

The basic syntax of Linux make directory using the “mkdir” command is as follows:

$ mkdir [option] [directories]

How to Create a Directory in Linux? (Linux Create Directory)

The “mkdir” command in Linux is used to create a new directory or a folder. To create a directory, execute the “mkdir” command and specify the directory name in the following way:

$ mkdir directory_name

For example, if you want to create a new directory, you will run the “mkdir” command with the directory name:

$ mkdir testdirectory

how to create a directory in linux? (a comprehensive guide: create a folder in linux)

The above command creates a new directory with that name in the current working directory. You can display the directory path on the terminal using the “pwd” command:

$ pwd

how to create a directory in linux? (a comprehensive guide: create a folder in linux)

If you want to create a new directory in another directory instead of the current working directory, you will provide the path to create a directory in a specific location.

$ mkdir /path-to-directory directory_name

$ mkdir /home/samreena/Documents/testdirectory

The above command creates a directory named "testdirectory" at the specified directory path in Linux.

Linux “mkdir” Command with Options

The Linux “mkdir” command allows you to create a new folder or directory with different options. This command offers various options to meet specific requirements. In Linux, it allows us to specify the number of directories to be created simultaneously. 

In the following table, we have listed some useful “mkdir” commands with various options that will help you create folders in the Linux system:

Command

Description

mkdir dir_name

Creates a single directory in the current directory location.

mkdir {dir1,dir2,dir3,dir4}

Creates multiple directories in the current directory location. Ensure no spaces are used inside the {}.

mkdir –p dir/path/new_dir

Creates a directory structure with missing parent directories, if any, in the specified path.

mkdir –m777 dir_name

Creates a directory with full read, write, and execute permissions (777) for all users.

mkdir –v dir_name(s)

Creates a directory in the current location and provides verbose output with details about the operation. Can be used for a single directory (dir_name) or multiple directories (dir_name(s)).

How to Create Multiple Directories on a Linux System 

Using the “mkdir” command, you can create multiple folders in Linux inside a directory. If you want to create multiple directories in the same directory, you can utilize the “mkdir" command to create them individually. However, executing separate commands for each directory can be time-consuming. 

To create multiple folders or directories in the same directory, use the “mkdir” command with the directory names separated by commas:

$ mkdir {dir1,dir2,dir3}

The above command creates three different subdirectories in the current working directory.

Display Verbose or Process Information After Creating a Folder in Linux (Linux Make Directory with Option -v)

Once you create a directory in Linux, it doesn’t show any confirmation message on the terminal by default. If you want to display details of the running command that shows whether the directory has been successfully created on your system or not, you can use the option “-v” with the “mkdir” command. 

This option stands for "verbose" and provides more feedback about the operation. You don’t need to run the “ls” command to view the directories or folders. Let’s look at an example:

$ mkdir -v test_directory

When you run the above command, you'll see an output displaying information about the creation of the directory. If the operation is successful, you will see the following output:

how to create a directory in linux? (a comprehensive guide: create a folder in linux)

How to Create a Directory in Linux With a Parent Directory (Make Folder in Linux with Option -p)

Use the option “-p” with the “mkdir” command if you want to create a parent directory structure with multiple subdirectories. This option includes any missing parent directories during the creation process.

For example, suppose you want to add a directory named "testdir2" within the directory "testdir1" in the “personal” directory on the Linux system. In that case, you will need to specify the complete directory path with the “mkdir” command. Use the following command to demonstrate this:

$ mkdir –p /home/samreena dirparent/dirtest2

how to create a directory in linux? (a comprehensive guide: create a folder in linux)

After executing the above “mkdir” command with the complete path, you can use the "ls" command along with the option "-R" option to verify the directory's creation. This option facilitates the display of a recursive directory tree, showcasing the contents of each directory within the specified path.

How to Set Permissions While Creating a Folder in Linux

When you create a directory, it typically receives default "rwx" (read, write, execute) permissions, but these permissions are initially only granted to the user who created the directory. If you wish to modify the permissions for all users, you can utilize the "-m" option in conjunction with the “mkdir” command.

In the example below, we're granting permissions to the directory as "777," allowing any user to read, write, and execute the directory if necessary.

$ mkdir -m 777 mytestdir 

After creating the "mytestdir" directory with the specified permissions, you can use the "ls -l" command to list the contents of the directory with detailed information, including permissions.

$ ls -l

Now, you will get a list of all directories and their permissions. 

Create a Folder in Linux Using Shell Script

A shell script generates the executable files through a series of commands. One of these commands is the “mkdir” command that you can use in a shell script.

To illustrate, let's consider a script that takes a specified path as input. Upon execution, this script will generate a folder and include a text file named "script" using the touch command.

#!/bin/bash
mkdir testdir
cd testdir
touch script

Every script you create should begin with the first line (#!/bin/bash). This start line in a file indicates that it is a bash script.

The “mkdir” command is used to generate a folder. Replace "testdir" with your desired directory name. The $@, also known as input parameters, placed at the end of the second and third lines, gets substituted with the value you provide when executing the script.

The “cd” command switches to the specified directory, and “touch” is used to create an empty file named "script."

To execute the “mkdir” command, use the following:

$ nano mkdir.sh
$ ./mkdir.sh

That’s all about the “mkdir” command. In Linux, most of the distributions have a built-in GUI interface. Therefore, you can also create a folder or Linux directory using the graphical user interface.

Executing the “mkdir” command with the “--help” option will display a detailed list of available options along with their respective descriptions, and subsequently, it will terminate the operation.

$ mkdir --help

how to create a directory in linux? (a comprehensive guide: create a folder in linux)

As you can see in the screenshot above, all options and “mkdir” command usage are displayed on the terminal after executing the above-mentioned command.

Conclusion 

In this article, we demonstrated how to create a folder and multiple directories in Linux using the “mkdir” command. If you are using a Linux VPS server, by utilizing the “mkdir” command with the specified options, you’ll be able to make changes to the standard “mkdir” command's functionality and alterations to the output.

So, always use the proper option selection for specific scenarios to achieve accurate results. Given that options are case-sensitive, using the correct option is crucial to avoid errors. To learn more about the usage of the “mkdir” command, visit the “mkdir” main page.