mkdir How to Create a Directory in Linux

For those of you wanting to know how to create a directory in Linux using the command line interface or Linux shell, you will want to read this Linux beginner tutorial on how to use the Linux mkdir command. The Linux command mkdir is used to make directories in Linux. There aren’t many options I use with this Linux command either. There is only 1 option I use with the mkdir command on a regular basis.

The usage for mkdir is very simple mkdir [options] [directory].

Just remember when running mkdir that you are creating directories from the current working directory, unless you specify another path. So if I am in /home/max/ and I want to make a directory called images I would run mkdir images which will create the directory images in /home/max/ so now my complete path for images is /home/max/images/. Now if I was in /home/max/files/ and still wanted to create /home/max/images/ I could run mkdir ../images which ../ is your parent directory. I could also type in the full path on the command line by running mkdir /home/max/images to get the same results.

The option I use on a regular basis for the mkdir command is mkdir -p which will allow you to create parent directories if they don’t already exist. For example if /home/max/images/ didn’t already exist and I wanted to make a directory /home/max/images/family I could either make each directory individually by running mkdir /home/max/images then run mkdir /home/max/images/family. I could also script this slightly all in one command line, yet 2 separate commands by running mkdir /home/max/images && /home/max/images/family. So either way still having to type out many commands. Now instead of typing all the extras, i could simply run 1 mkdir command to create the parent directories as well using the mkdir -p option. Which I would then run mkdir -p /home/max/images/family and this will create /home/max/images and also /home/max/images/family as well!

I’ve also seen a lot of people asking how to make directories a through z with Linux? Well my first thought was to try mkdir [a-z], but unfortunately this will just create a directory named [a-z]. So in Linux to make directories a through z you have to make a small script type command. Here is the command you can run to create directories a through z, if you are wanting to organize mp3s for example: perl -e 'for (a..z) {system("mkdir $_")};'

SHELL NOTE: For anyone wondering in the example command line where I used mkdir /home/max/images && /home/max/images/family what the && does, the && on a command line separates shell commands and only runs the next shell command if the first Linux shell command ran successfully without any errors. So if for some reason mkdir /home/max/images failed, the shell would not run the next shell command mkdir /home/max/images/family.

As always thank you for reading this information on the Linux mkdir command for creating new folders and directories in Linux via the command line interface. If you have any friends you would like to teach how to run Linux, send them here to the beginner linux tutorial, where we will hopefully turn them into advanced Linux users or at least Linux users so they can get rid of that evil Microsoft product they call an operating system!

Comment Linux tutorial or Leave a Trackback

8 Comments

  1. josh
    Posted February 22, 2010 at 4:24 pm | Linux tutorial comment

    thanks you made me understand these stuff because my teacher told me these but i really didn’t get it you know :o) thank you

  2. Posted February 26, 2010 at 5:25 pm | Linux tutorial comment

    glad to hear my Linux tutorials have helped you.. Thanks for reading!

  3. Posted May 2, 2011 at 7:29 am | Linux tutorial comment

    Thanks for the detailed explanation about -p option

  4. Shivaraj
    Posted December 9, 2011 at 12:42 am | Linux tutorial comment

    How to give php command to create directory in linux?

  5. Posted December 10, 2011 at 3:11 am | Linux tutorial comment

    Shivaraj,
    You can use the mkdir command builtin to php.
    mkdir("/path/to/new/dir", 0755);
    Replace the 0755 with the permissions you’d like to use.

  6. RoVa
    Posted January 24, 2012 at 5:37 pm | Linux tutorial comment

    When you wan to create a directory from a till z you can also use the command mkdir {a..z}

  7. Kathy
    Posted July 8, 2014 at 10:30 am | Linux tutorial comment

    I’m taking the Intro to Linux and my instructor has given us an assignment and the question is “Make a directory inside of your home director called dir99.

  8. Posted July 25, 2014 at 11:26 pm | Linux tutorial comment

    Kathy,
    mkdir ~/dir99

One Trackback

  1. […] mkdir command – used to make directories on the filesystem. […]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*