How to Create Directory in Linux | mkdir Command - GeeksforGeeks (2024)

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.

Table of Content

  • Syntax of `mkdir` Command in Linux
  • Options and their Practical Implementation in mkdir
  • Examples on How to Create Directory in Linux by Using`mkdir` command
  • How to Create Directory in Linux – FAQs

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

How to Create Directory in Linux | mkdir Command - GeeksforGeeks (1)

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

How to Create Directory in Linux | mkdir Command - GeeksforGeeks (2)

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]

How to Create Directory in Linux | mkdir Command - GeeksforGeeks (3)

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 –

How to Create Directory in Linux | mkdir Command - GeeksforGeeks (4)

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.

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.

How to Create Directory in Linux | mkdir Command - GeeksforGeeks (6)

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.

How to Create Directory in Linux | mkdir Command - GeeksforGeeks (7)

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

How to Create Directory in Linux | mkdir Command - GeeksforGeeks (8)

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.

How to Create Directory in Linux | mkdir Command - GeeksforGeeks (9)

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

How to Create Directory in Linux | mkdir Command - GeeksforGeeks (10)

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.



rossoskull

How to Create Directory in Linux | mkdir Command - GeeksforGeeks (12)

Improve

Previous Article

How to Display Current Working Directory in Linux | pwd Command

Next Article

rm command in Linux with examples

Please Login to comment...

How to Create Directory in Linux | mkdir Command - GeeksforGeeks (2024)

FAQs

How to create a new directory in Linux? ›

To create new directory use "mkdir" command. For example, to create directory TMP in the current directory issue either "mkdir TMP" or "mkdir ./TMP".

How to use mkdir command in Linux? ›

Create a directory

The basic syntax for using this command is mkdir {dir} (replace {dir} with the desired name of your directory). Before creating any directory or file, remember that most Linux filesystems are case-sensitive.

How to set directory in Linux? ›

Changing to another directory (cd command)
  1. To change to your home directory, type the following: cd.
  2. To change to the /usr/include directory, type the following: cd /usr/include.
  3. To go down one level of the directory tree to the sys directory, type the following: cd sys.

Which command is used to make a new directory? ›

The mkdir (make directory) command in the Unix, DOS, DR FlexOS, IBM OS/2, Microsoft Windows, and ReactOS operating systems is used to make a new directory.

How to create and delete directories in Linux? ›

To create a directory in Linux, just follow these simple steps:
  1. From the terminal window, you can create a new directory using the mkdir command, followed by the name of the directory you'd like to create. ...
  2. Using the command above would create a new, empty directory, called sampledirectory.

How to create a path in Linux? ›

Linux: Add to PATH Permanently
  1. Open the . bashrc file using a text editor. ...
  2. Go to the end of the file.
  3. Paste the export syntax at the end of the file. export PATH="/Directory1:$PATH"
  4. Save and exit.
  5. Execute the script or reboot the system to make the changes live. To verify the changes, run echo :
Sep 22, 2022

How to create a root directory in Linux? ›

The mkdir command in Linux/Unix is a command-line utility that allows users to create new directories. mkdir stands for "make directory." With mkdir , you can also set permissions, create multiple directories at once, and much more.

What is the command for Linux directory? ›

Basic Linux Commands
  • pwd (Print Working Directory) The pwd command is your GPS in the Linux file system. ...
  • ls (List Files and Directories) ...
  • cd (Change Directory) ...
  • mkdir (Make Directory) ...
  • touch (Create Empty File) ...
  • rm (Remove Files and Directories)
Sep 15, 2023

How do you write into a directory in Linux? ›

After creating a file using the "cat" command, the Terminal enters write mode allowing you to add text to the file. Type the text you want the file to contain on the next line. Press Ctrl + d . This saves the file with the text and exits write mode in the Terminal.

How do I set a directory? ›

Open the Command Prompt (CMD) and type "cd" with a space, followed by the name of the directory, or drag and drop the directory into CMD from File Explorer. Press "Enter." Type "cd.." and press "Enter" to go back one directory. Type "cd\" and press "Enter" to go to the root of the drive.

How to create a file in Linux? ›

How to Create a File in Linux Using Command Line
  1. touch Command. Access a terminal window and use the touch command to create a new text file called test.txt: touch test.txt. ...
  2. cat Command. ...
  3. echo Command. ...
  4. printf Command. ...
  5. Redirect Operator.
Jul 11, 2024

How to install directory in Linux? ›

  1. Log In to the HSM.
  2. Confirm the HSM Initial State.
  3. Update the HSM Configuration Files. Set the HSM Hostname. Set HSM IP Address. Enable Name Resolution. Update the Active IP Tables. Update the Inactive IP Tables. Restart Networking After Changes.
Mar 6, 2024

How do I create a directory? ›

To create a directory in MS-DOS or the Windows Command Prompt (cmd), use the md or mkdir MS-DOS command. For example, below, we are creating a new directory called "hope" in the current directory. You can also create multiple new directories in the current one with the md command.

How to create a folder in Linux? ›

To create a new folder, use the mkdir command followed by the desired folder name. Replace “YourFolderName” with whatever you'd like to name your folder.

How to create multiple directory in Linux? ›

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] .......

How do you create a new home directory in Linux? ›

By default, the useradd command does not create a home directory. Instead, you will need to specify the -m option to create a home directory for a user. This will create a home directory with username at /home/ and copy the . bash_logout, .

How do I create a new directory in repository? ›

To create a new folder in Github, you can navigate to the desired repository, then: Click on the "Add file" button followed by the "Create new file" link in the dropdown menu.

What is the CD command in Linux? ›

Linux cd command is used to change the current working directory ( i.e., in which the current user is working). The "cd" stands for 'change directory. ' It is one of the most frequently used commands in the Linux terminal.

References

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Gov. Deandrea McKenzie

Last Updated:

Views: 6077

Rating: 4.6 / 5 (46 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Gov. Deandrea McKenzie

Birthday: 2001-01-17

Address: Suite 769 2454 Marsha Coves, Debbieton, MS 95002

Phone: +813077629322

Job: Real-Estate Executive

Hobby: Archery, Metal detecting, Kitesurfing, Genealogy, Kitesurfing, Calligraphy, Roller skating

Introduction: My name is Gov. Deandrea McKenzie, I am a spotless, clean, glamorous, sparkling, adventurous, nice, brainy person who loves writing and wants to share my knowledge and understanding with you.