The Wayback Machine - https://web.archive.org/web/20240917132139/https://www.geeksforgeeks.org/mkdir-command-in-linux-with-examples/
Open In App

How to Create Directory in Linux | mkdir Command

Last Updated : 13 Feb, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In Linux, the ‘mkdir’ command is like a magic wand for creating folders super easily. ‘mkdir’ stands for “make directory,” and it helps you organize your computer stuff by creating folders with just one command. Whether you’re making one folder or a bunch of them in a row, ‘mkdir’ is there to help you keep things neat and tidy on your computer. In this guide, we’ll talk about how to use ‘mkdir,’ what words to type, and some cool tricks to make your folders just the way you want in Linux.

This command can create multiple directories at once as well as set the permissions for the directories. It is important to note that the user executing this command must have enough permission to create a directory in the parent directory, or he/she may receive a ‘permission denied’ error. 

Syntax of `mkdir` Command in Linux

mkdir [options...] [directory_name]

Here, replace [directory_name] with the desired name of the directory you want to create. Let’s delve into the functionality of the ‘mkdir’ command with various examples.

Options and their Practical Implementation in mkdir

Options

Description

–help

Displays help-related information for the mkdir command and exits. Use this option to get assistance on how to use the command and its various features.

–version

Displays the version number and additional information about the license for mkdir, providing details about the software version installed. Use this option to check the installed mkdir version.

-v or –verbose

Enables verbose mode, displaying a message for every directory created. When used with the [directories] argument, it shows the names of the directories being created.

-p

A flag that allows the creation of parent directories as necessary. If the specified directories already exist, no error is reported. Useful for creating a directory hierarchy without errors.

-m

Sets file modes or permissions for the created directories. The syntax follows that of the chmod command. Use this option to specify permissions, such as read, write, and execute, for the new directories.

1) `–help` Option in `mkdir`Command in Linux

It displays help-related information and exits.

Syntax:

mkdir --help



mkdir --help

mkdir –help

2) `–version` Option in `mkdir`Command in Linux

It displays the version number, some information regarding the license and exits. 
Syntax: 

mkdir --version



mkdir --version

mkdir –version

3) `-v` or `–verbose` Option in to Create Directory in Linux

 It displays a message for every directory created. 
Syntax: 

mkdir -v [directories]



mkdir -v [directories]

Here we have used `ls` command to display all files and directories.

As we can see we have created tow directory with “names = jayeshghg_1 and jayeshgfg_2”, replace these names with the directory name you want.

 4) `-p` Option to Create Directory in Linux

A flag which enables the command to create parent directories as necessary. If the directories exist, no error is specified. 
Syntax: 

mkdir -p [directories]



Suppose you execute the following command – 

mkdir -p first/second/third



If the first and second directories do not exist, due to the -p option, mkdir will create these directories for us. If we do not specify the -p option, and request the creation of directories, where parent directory doesn’t exist, we will get the following output – 

mkdir first/second/third

mkdir first/second/third

If we specify the -p option, the directories will be created, and no error will be reported. Following is the output of one such execution. We’ve also provided the -v option, so that we can see it in action. 

 -p option

-p option

5) `-m` Option to Create Directory in Linux

This option is used to set the file modes, i.e. permissions, etc. for the created directories. The syntax of the mode is the same as the chmod command. 
Syntax: 

mkdir -m a=rwx [directories]



The above syntax specifies that the directories created give access to all the users to read from, write to and execute the contents of the created directories. You can use ‘a=r’ to only allow all the users to read from the directories and so on. 

mkdir -m a=rwx [directories]

mkdir -m a=rwx [directories]

Examples on How to Create Directory in Linux by Using`mkdir` command

1) How to create a directory in Linux using `mkdir` command?

To create a single directory, use the following syntax:

Syntax:

mkdir [directorie_name]


For Example: 

If we want to create a directory name “jayesh_gfg”.

Syntax:

mkdir jayesh_gfg

This command creates a directory named “jayesh_gfg” in the current location. You can replace “jayesh_gfg” with any name you prefer.

mkdir jayesh_gfg

mkdir jayesh_gfg

Here we have used `ls` command to display all files and directories.

2) How to create a directory with verbose output using `mkdir` command?

Syntax:

mkdir -v [directory_name]



For Example:

If we want to create a directory name “geeksforgeeks” and see verbose at same time. You can enter your directory_name.

Syntax:

mkdir -v geeksforgeeks



mkdir -v geeksforgeeks

mkdir -v geeksforgeeks

Here we have used `ls` command to display all files and directories.

3) How to create multiple directories in Linux using `mkdir` command?

To create multiple directories at once, you can specify multiple directory names separated by spaces:

Syntax:

mkdir [directorie_name_1] [directorie_name_1] [directorie_name_1] .......


For Example: 

If we want to create a directory name “jayesh_gfg_1, jayesh_gfg_2, jayesh_gfg_3”.

Syntax:

mkdir jayesh_gfg_1 jayesh_gfg_2 jayesh_gfg_3

This command creates three directories named “jayesh_gfg_1″, ” jayesh_gfg_2″ and “jayesh_gfg_3” in the current location.

mkdir jayesh_gfg_1 jayesh_gfg_2 jayesh_gfg_3

mkdir jayesh_gfg_1 jayesh_gfg_2 jayesh_gfg_3

Here we have used `ls` command to display all files and directories.

4) How to resolve permission denied error in `mkdir` command?

If you encounter a “permission denied” error while creating a directory, you may not have permission to create directories in that location. To resolve this you can give root access to the user by using “sudo” command.

For Example: 

If we want to create a directory name “geeksforgeek” with “sudo” permission. you can replace “geeksforgeek” directory_name with your directory_name. While using this command it may ask you to enter the password of root.

Syntax:

sudo mkdir geeksforgeek


sudo mkdir geeksforgeek

sudo mkdir geeksforgeek

5) How to Create Directory Using Absolute and Relative Paths

The ‘mkdir’ command also supports absolute and relative paths. For example:

mkdir /path/to/directory

This command creates a directory named “directory” at the specified absolute path.

mkdir my_folder/sub_folder

This command creates a directory structure with “my_folder” as the parent directory and “sub_folder” as its subdirectory.

How to Create Directory in Linux – FAQs

How do I create a directory with spaces in its name using the ‘mkdir’ command in Linux?

If you want to create a directory with spaces, you can enclose the entire directory name in quotes.

For example:

mkdir "My Documents"

Can I create multiple directories at once with the ‘mkdir’ command?

Yes, you can create multiple directories simultaneously by specifying their names separated by spaces.

For instance:

mkdir dir1 dir2 dir3

What is the purpose of the ‘-p’ option in the ‘mkdir’ command?

The ‘-p’ option allows you to create a directory hierarchy, including parent directories that don’t exist. It prevents errors if the specified directories already exist.

Example:

mkdir -p project/docs/images

How can I set specific permissions for a directory using the ‘mkdir’ command?

You can use the ‘-m’ option to set permissions for the created directory.

For example:

mkdir -m 755 public

This command sets read, write, and execute permissions for the owner and read and execute permissions for others.

Is there a way to create a directory and its subdirectories in one go with ‘mkdir’ in Linux?

Yes, you can use a combination of the ‘-p’ option and specify the directory structure you want to create.

For instance:

mkdir -p parent/child/grandchild

This command creates the “parent” directory and its subdirectories “child” and “grandchild” in one go.

Conclusion

If you’re a Linux user, you’ll find that the mkdir command is incredibly useful for creating directories or folders quickly and efficiently. It provides a range of options that can help you customize the way you create directories, such as verbose output, setting permissions, and creating multiple directories simultaneously. However, it’s worth noting that you’ll need the appropriate permissions to create directories in specific locations, or else you may run into “permission denied” errors. Learning how to use the mkdir command effectively can significantly improve your ability to organize files and directories and boost your productivity on Linux.



Previous Article
Next Article

Similar Reads

mkdir: Cannot Create Directory : File Exists
The error "Mkdir: Cannot Create Directory 'Directory': File Exists" occurs when attempting to create a directory using the mkdir command in a Unix-like operating system, but a file with the same name already exists in that location. To resolve this issue, you can either choose a different name for the directory or remove/rename the existing file ca
3 min read
How to Display Current Working Directory in Linux | pwd Command
The 'pwd,' which stands for "print working directory." In this article, we will delve into the 'pwd' command, exploring its functionality, usage, and various examples. It prints the path of the working directory, starting from the root. pwd is shell built-in command(pwd) or an actual binary(/bin/pwd). $PWD is an environment variable that stores the
4 min read
mindepth and maxdepth in Linux find() command for limiting search to a specific directory.
How to limit search a specified directory in Linux? There is a command in Linux to search for files in a directory hierarchy known as 'find' . It searches the directory tree rooted at each given starting-point by evaluating the given expression from left to right, according to the rules of precedence, until the outcome is known (the left-hand side
5 min read
How to Change the Directory in Linux | cd Command
Mastering the cd command in Linux is a fundamental skill for anyone working in the Linux environment. The cd command allows you to change directories in Linux, making it easier to navigate through the file system and manage your files efficiently. Whether you're a beginner or an experienced user, understanding how to use the cd command in Linux is
6 min read
Shell Script to Delete a File from Every Directory Above the Present Working Directory
In this article, we will discuss how to delete a file from every directory above the parent working directory using shell script in Linux. To delete a file from a directory we use the rm command in Linux/UNIX. Syntax: rm [OPTION]... [FILE]... Using shell script, we have to delete the input file from every directory above the present working directo
2 min read
Ccat – Colorize Cat Command Output command in Linux with Examples
ccat is a command-line tool for Linux and OSX, which is similar to the cat command in Linux. But the difference between cat and ccat is that the ccat shows the content of the file with the syntax highlighted. Currently, ccat supports the following programming languages. JavaScriptJavaRubyPythonGoCJSONInstallation of Ccat First, we are going to see
2 min read
How to Display Command History in Linux | history Command
The command-line interface in Linux provides powerful tools for users, and mastering command history is essential for efficient navigation and retrieval of previously executed commands. The history command is a valuable utility that allows users to view and search through their command history. In this comprehensive guide, we will explore the vario
4 min read
Copy and Create Destination Directory if it Does Not Exist in Linux
A directory is a file system location for storing and organizing files. A Linux system has many directories, and users can create their own directories. Copying and creating a directory are useful operations for Linux users. To create a new directory, use the command: mkdir new_directory. The 'm' stands for 'modify.' The name of the new directory m
6 min read
How to use 'cp' command to exclude a specific directory
cp command is used to create an exact copy of the contents of the files and directories but with a different name and location. It works using the terminal. The exact syntax goes like this: Syntax:cp [source_file/directory] [destination]Where cp is used for the copy, source_file provides the exact file or directory or folder from where you want to
3 min read
bd – Quickly Go Back to a Parent Directory Instead of Typing ‘cd’ Command
IntroductionEvery Linux user most probably suffers in a situation where we need to jump from one directory to another in a backward direction, multiple times. Conventionally most of us choose to use the '..' parameter with the 'cd' command to denote the previous directory, multiple times. This command might look like this, 'cd ../../../..'. This co
6 min read
Watcherd Shell Listener for Directory Changes in Linux
Watcherd is a tool for Linux that helps to monitor directory changes and print whatever changes are done like adding and deleting for a particular directory and execute commands and shell scripts according to a particular event. Installing Watcherd on Linux Step 1: Downloading watcherd. Since watcherd is a Github repository you can use wget command
2 min read
How to Make File and Directory Undeletable Even By Root in Linux?
In this article, we are going to see How to Make File and Directory Undeletable, Even By Root in Linux. So in order to make a file or directory undeletable by any user, we need to make the file or directory unmodifiable using the chattr command. This command changes the file attributes in our system. So in order to make it Undeletable Syntax is: No
2 min read
Renaming All Files and Directory Names to Lowercase in Linux
Here we will see how to rename all files and directory names to lowercase in Linux. We can easily do it with some basic commands in Linux. The below command can be used to view the contents of your directory. $ find [directory_name] -depth Here 'priyanshu' directory which has some sub-files/folders, has all files starting from uppercase letters: $
1 min read
Linux Directory Structure
Prerequisite: Linux File Hierarchy Structure In Linux/Unix operating system everything is a file even directories are files, files are files, and devices like mouse, keyboard, printer, etc are also files. Here we are going to see the Directory Structure in Linux. Types of files in the Linux system. General Files - It is also called ordinary files.
5 min read
OpenDoor - OWASP Directory Access Scanner in Kali Linux
Brute-Forcing is an automated process through which Usernames, Passwords can be detected by querying the possible credentials on the target domain login pages. We can also brute-force hidden files and directories on the target server by specifying the possible word-list which contains the phrases that will be tested on the domain. This process save
3 min read
Find and Replace with sed in Directory and Sub Directories in Linux
sed stands for stream editor. It is a basic utility command in Linux that has basic functionality like inserting, deleting, finding, replacing, and searching text strings in a file. But sed is most commonly used for its substitution function i.e find and replace. Syntaxsed OPTIONS... [script] [input_file] e.g. sed "s/abc/ABC/g" abc.txt Consider the
4 min read
How to Get Total Size of a Directory in Linux
Many times during working on Linux we may encounter situations, where we want to look at the total size occupied by a directory. Knowing the file or directory size can help a user perform tasks like storage allocation, size comparison, etc. At that time knowledge of some commands might be helpful to get the size of a directory in Linux. Usually, wh
5 min read
How to Copy a Directory in Linux
There is this famous quote for Linux by Jamie Zawinski that goes like this "Linux is only free if your time has no value." Certainly, that's true as people start using Linux they have to learn more about it, and when encountering it we have to solve it by ourselves, and that way we learn more about Linux. In this article, we are going to learn how
5 min read
How to Fix - Unable to Lock the Administration Directory – Kali Linux
Kali Linux is among the most famous OS for ethical hacking and pentesting. It contains all the necessary tools for pentesting and ethical hacking. Using Kali is not easy and every user has once encountered errors. In this article, we will see how to solve this particular problem using Kali Linux. What is Unable to lock the administration directory
5 min read
How to Remove Directory in Linux
In Linux, a Directory is a container for files and other directories, forming a hierarchical structure. so, before deep diving into the directory, let's learn about the Linux Directory. A directory is a structure that plays an important role in the file system, offering users a systematic way to navigate and structure their data. In the Linux Syste
5 min read
bash: cd: Downloads: No such file or directory in Linux
Encountering errors while navigating through the command line interface can be frustrating, especially for beginners. One common error, "Bash: Cd: Downloads: No Such File Or Directory," often occurs when users attempt to change directories to a location that doesn't exist or isn't accessible. However, fear not! This article serves as a comprehensiv
4 min read
Linux Shell Script to Backup Files and Directory
Backing up our important files and directories is crucial to safeguarding our data. In this article, we’ll create a simple shell script that uses the tar utility to create an archive file for backup purposes that we can use whenever we require. The shell script we’ll build allows us to specify which directories to back up and where to store the bac
5 min read
How to Count Files in Directory Recursively in Linux
When exploring directories on your server, you may have come across folders with a large number of files in them. You might wish to know how many files exist in a certain directory or across many directories at times. To put it another way, you wish to count the number of files saved in a directory on your system. In this article, we are going to s
5 min read
DotDotPwn - Directory Traversal Fuzzer Tool in Linux
Directories in the Web-based application hold various information about the functionalities of the website. Some directories can be common or usual, but some of the directories are attractive or important directories that can contain some important information. Like /etc/passwd can contain the information about the Linux server. Traversing this dir
4 min read
How to Use ln Command to Create Symbolic Links in Linux
A symbolic link (symlink) is like a shortcut that points to a file or folder on Linux and other similar operating systems. Symlinks can be useful for organizing files and folders, or for making it easier to access files from different locations. In this guide, you'll learn how to create symbolic links in Linux using the 'ln' command. Use ln Command
5 min read
How to Create and Use Alias Command in Linux
Imagine you're lost in a maze of complicated Linux commands. You stumble upon a secret doorway marked "Alias," and inside you find shortcuts to all your favorite commands! That's what creating aliases is like. You get to make your own mini-commands for the long ones you use all the time, making things simpler and faster. This article guides you thr
6 min read
How to Create an Empty File in Linux | Touch Command
The touch command is a standard command used in the UNIX/Linux operating system which is used to create, change and modify the timestamps of a file. Basically, there are two different commands to create a file in the Linux system which are as follows: touch command: It is used to create a file without any content. The file created using the touch c
8 min read
How to Create a Text File Using the Command Line in Linux
The Linux command line is a powerful tool that allows you to manage your system with precision and speed. One of the most common tasks is creating a text file, which can be achieved using several commands and methods. Here we will learn some quick ways to create a text file from the Linux Command Line or Terminal. Table of Content Using the 'touch'
6 min read
How to Create a new group in Linux | groupadd command
In the Linux operating system, user management is a crucial aspect of system administration. One of the fundamental tasks is creating and managing user groups. Groups in Linux allow administrators to organize and control user access to various resources and files. The groupadd command is a powerful tool that facilitates the creation of new groups e
7 min read
Shell Script to create a compressed archive of the specified directory
An archive is a simple process of storing a group of files or folders into one file. Sometimes we may need to create a compressed archive out of a single folder so that we can share it with others or for something else. In Linux, to ease the task, we can create a shell script to automate the process of creating an archive by simply providing the fo
3 min read