mkdir command in Linux with Examples

mkdir command in Linux allows the user to create directories (also referred to as folders in some operating systems ). 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 permissions to create a directory in the parent directory, or he/she may recieve a ‘permission denied’ error.

Syntax:

mkdir [options...] [directories ...]
  • --version: It displays the version number, some information regarding the license and exits.
    Syntax:

    mkdir --version

    Output:
    mkdir --version screenshot

  • --help: It displays the help related information and exits.
    Syntax:

    mkdir --help
    

    Output:
    mkdir --help OUTPUT

  • -v or --verbose: It displays a message for every directory created.
    Syntax:



    mkdir -v [directories]

    Output:
    mkdir -v

  • -p: 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 –
    Error when -p option is not specified

    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.
    Output:
    mkdir -p

  • -m: 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.
    Output:
    mkdir -m



My Personal Notes arrow_drop_up

Full stack web developer | GCI 18 Mentor | JSec at IIITV Coding Club

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.