• Home
  • Store
  • Blog
  • Contact
  • Home
  • Store
  • Blog
  • Contact
  • #linux
  • |
  • #commandline
  • |
  • #softwareengineering
  • |
  • #embeddedsystems
  • |
  • #compilers
  • ...
  • View All >>

Intro To 'mkdir' Command In Linux

2023-04-24 - By Robert Elder

     I use the 'mkdir' command to create new directories:

ls -l
total 4
-rw-rw-r-- 1 robert robert 4 Apr 25 17:08 README.txt
mkdir my-new-folder
ls -l
total 8
drwxrwxr-x 2 robert robert 4096 Apr 25 17:10 my-new-folder
-rw-rw-r-- 1 robert robert    4 Apr 25 17:08 README.txt

Create Multiple Directories At Once

     I can even use it to create multiple directories at once by specifying more than one directory name:

mkdir first-folder second-folder third-folder fourth-folder
ls -l
total 24
drwxrwxr-x 2 robert robert 4096 Apr 25 17:12 first-folder
drwxrwxr-x 2 robert robert 4096 Apr 25 17:12 fourth-folder
drwxrwxr-x 2 robert robert 4096 Apr 25 17:10 my-new-folder
-rw-rw-r-- 1 robert robert    4 Apr 25 17:08 README.txt
drwxrwxr-x 2 robert robert 4096 Apr 25 17:12 second-folder
drwxrwxr-x 2 robert robert 4096 Apr 25 17:12 third-folder

Create Multiple Nested Directories At Once Using '-p'

     Sometimes, you may want to create multiple nested parent directories like this:

mkdir backups/docs/pictures/cats

     However, the above command will only work if the directories in the path 'backups/docs/pictures' already exist, so you'll likely see this error message:

mkdir: cannot create directory ‘backups/docs/pictures/cats’: No such file or directory

     To create all of the required directories at once, you can use the '-p' flag:

mkdir -p backups/docs/pictures/cats

     And now, you can see that all of these directories were created:

find backups
backups/
backups/docs
backups/docs/pictures
backups/docs/pictures/cats

     As you can see from above, using the 'mkdir' command with the '-p' flag like this:

mkdir -p backups/docs/pictures/cats

     is effectively the same as running multiple 'mkdir' commands without the '-p' flag:

mkdir backups
mkdir backups/docs
mkdir backups/docs/pictures
mkdir backups/docs/pictures/cats

     It's also worth noting that the '-p' flag has another purpose, which is to suppress errors when a directory already exists:

mkdir example-folder
mkdir example-folder
mkdir: cannot create directory ‘example-folder’: File exists
mkdir -p example-folder

Create Directories Using Relative Or Absolute Paths

     I can create a new directory within the current working directory, or in a completely different directory using relative or absolute paths:

mkdir first-folder/sub-folder
ls -l first-folder
total 4
drwxrwxr-x 2 robert robert 4096 Apr 25 17:16 sub-folder
ls -l /home/robert/important
total 24
drwxrwxr-x 3 robert robert 4096 Apr 25 17:16 first-folder
drwxrwxr-x 2 robert robert 4096 Apr 25 17:12 fourth-folder
drwxrwxr-x 2 robert robert 4096 Apr 25 17:10 my-new-folder
-rw-rw-r-- 1 robert robert    4 Apr 25 17:08 README.txt
drwxrwxr-x 2 robert robert 4096 Apr 25 17:12 second-folder
drwxrwxr-x 2 robert robert 4096 Apr 25 17:12 third-folder
mkdir /home/robert/important/new-folder
ls -l /home/robert/important
total 28
drwxrwxr-x 3 robert robert 4096 Apr 25 17:16 first-folder
drwxrwxr-x 2 robert robert 4096 Apr 25 17:12 fourth-folder
drwxrwxr-x 2 robert robert 4096 Apr 25 17:10 my-new-folder
drwxrwxr-x 2 robert robert 4096 Apr 25 17:25 new-folder
-rw-rw-r-- 1 robert robert    4 Apr 25 17:08 README.txt
drwxrwxr-x 2 robert robert 4096 Apr 25 17:12 second-folder
drwxrwxr-x 2 robert robert 4096 Apr 25 17:12 third-folder

     This use of the 'mkdir' command will create a new directory called 'test1' that's one directory up relative to the current working directory:

ls -l ../
total 4
drwxrwxr-x 2 robert robert 4096 Apr 25 17:16 some-folder
mkdir ../test1
ls -l ../
total 8
drwxrwxr-x 2 robert robert 4096 Apr 25 17:16 some-folder
drwxrwxr-x 2 robert robert 4096 Apr 25 17:30 test1

     This use of the 'mkdir' command will create a new directory called 'test2' in the absolute directory '/home/robert/important':

ls -l /home/robert/important
total 28
drwxrwxr-x 4 robert robert 4096 Apr 25 17:30 first-folder
drwxrwxr-x 2 robert robert 4096 Apr 25 17:12 fourth-folder
drwxrwxr-x 2 robert robert 4096 Apr 25 17:10 my-new-folder
drwxrwxr-x 2 robert robert 4096 Apr 25 17:25 new-folder
-rw-rw-r-- 1 robert robert    4 Apr 25 17:08 README.txt
drwxrwxr-x 2 robert robert 4096 Apr 25 17:12 second-folder
drwxrwxr-x 2 robert robert 4096 Apr 25 17:12 third-folder
mkdir /home/robert/important/test2
ls -l /home/robert/important
total 32
drwxrwxr-x 4 robert robert 4096 Apr 25 17:30 first-folder
drwxrwxr-x 2 robert robert 4096 Apr 25 17:12 fourth-folder
drwxrwxr-x 2 robert robert 4096 Apr 25 17:10 my-new-folder
drwxrwxr-x 2 robert robert 4096 Apr 25 17:25 new-folder
-rw-rw-r-- 1 robert robert    4 Apr 25 17:08 README.txt
drwxrwxr-x 2 robert robert 4096 Apr 25 17:12 second-folder
drwxrwxr-x 2 robert robert 4096 Apr 25 17:31 test2
drwxrwxr-x 2 robert robert 4096 Apr 25 17:12 third-folder

     And that's why the 'mkdir' command is my favorite Linux command.

Intro To 'stty' Command In Linux
Published 2023-10-04
$1.00 CAD
Terminal Block Mining Simulation Game
Intro To 'nproc' Command In Linux
Published 2023-07-15
Intro To 'comm' Command In Linux
Published 2023-09-06
How To Force The 'true' Command To Return 'false'
Published 2023-07-09
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01
Intro To 'chroot' Command In Linux
Published 2023-06-23
Join My Mailing List

Privacy Policy
Why Bother Subscribing?
  • Free Software/Engineering Content. I publish all of my educational content publicly for free so everybody can make use of it.  Why bother signing up for a paid 'course', when you can just sign up for this email list?
  • Read about cool new products that I'm building. How do I make money? Glad you asked!  You'll get some emails with examples of things that I sell.  You might even get some business ideas of your own :)
  • People actually like this email list. I know that sounds crazy, because who actually subscribes to email lists these days, right?  Well, some do, and if you end up not liking it, I give you permission to unsubscribe and mark it as spam.
© 2025 Robert Elder Software Inc.
Privacy Policy      Store Policies      Terms of Use