Linux mkdir command tutorial

Directory is a folder to keep sub-directories or related files together. This is important because keeping all files in the proper directory will make Linux system tidy and easier to manage. As you can see in the default Linux system, all directories can be found in / and each directory has related files, for examples the /bin directory contains all system user's binary files. This tutorial will guide Linux new user how to create directory using mkdir command.

Linux mkdir command

Linux mkdir command is similar to Windows/dos mkdir command. It creates directories with the specified names supplied after it. The mkdir command format is mkdir <directory1 directory2 directory3 ...>.

The mkdir command is one of the mostly use command by Linux users, though it just creates new directory. If we look at the mkdir manual page example below, there is not many options available with mkdir command.

NAME
       mkdir - make directories

SYNOPSIS
       mkdir [OPTION] DIRECTORY...

DESCRIPTION
       Create the DIRECTORY(ies), if they do not already exist.

       Mandatory arguments to long options are mandatory for short options too.

       -m, --mode=MODE
              set file mode (as in chmod), not a=rwx - umask

       -p, --parents
              no error if existing, make parent directories as needed

       -v, --verbose
              print a message for each created directory

       --help display this help and exit

       --version
              output version information and exit

This is an example of Linux mkdir command used with no option supplied:

    bill@slackware:~$ mkdir programs
    bill@slackware:~$ ls
    programs/ 
    
    bill@slackware:~$
    

As you can see from the example above, when we view the result with ls command, a new directory named programs has been created.

The Linux mkdir command also allows you to create multiple directory. Here is an example on how to create multiple directories at once:

    bill@slackware:~$ mkdir linux tutorial guide example
    bill@slackware:~$ ls
    example/ guide/ 
    linux/ programs/ 
    tutorial/
    bill@slackware:~$
    

Let's try using the options available with mkdir command. The first mkdir option example is the '--mode=MODE':

    kkcjlab@ubuntu-server:~$ mkdir --mode=550 linux-tutorial
    kkcjlab@ubuntu-server:~$ ls -l
    total 566176
    drwxr-xr-x  2 kkcjlab kkcjlab     20480 2009-10-27 15:58 final kem spl 1
    dr-xr-x---  2 kkcjlab kkcjlab      4096 2009-11-02 10:24 linux-tutorial
    

The mkdir '--mode=MODE' option can be used to sets the permission mode as in chmod. You can read more about chmod in Linux file and directory permissions tutorial.

Here is another mkdir usage example. We are going to use '-p, --parents' which makes all necessary directories even if they don’t currently exist.

    root@ubuntu-server:/home/kkcjlab# mkdir --parents linux-tutorial/basic-command/mkdir
    root@ubuntu-server:/home/kkcjlab# ls -R linux-tutorial/
    linux-tutorial/:
    basic-command

    linux-tutorial/basic-command:
    mkdir

    linux-tutorial/basic-command/mkdir:
    root@ubuntu-server:/home/kkcjlab#
    

Here is an explanation of the mkdir command above. We use mkdir command to create a new directory in the /linux-tutorial/basic-command directory. The 'linux-tutorial' directory is already exist but we don't have the 'basic-command' directory. This is an example situation when we can use the mkdir '--parents' option to create parent directory we specified even if they don’t currently exist.

That's it. Creating new directory in Linux is a very easy task even a new user can do the job without any problem. With the basic mkdir examples given above, you can create as many directory as you want. Practice until you understand how to use the mkdir options. Don't worry, you can remove them later because in the next tutorial, you'll learn how to Remove or delete directory with Linux rmdir command.

Back to Linux basic commands main page.