Tuesday, September 19, 2017

How to Create Complex directory Tree Using mkdir -p Command in UNIX

One of the most common tasks in any Linux is creating directories, and most of us spend a lot of time creating complex directory structures in UNIX.  I am sure you know about mkdir command, we have been using this command in almost every operating system e..g DOS, Windows, Linux, OS/2, Solaris, or any other *NIX operating systems. It is one of the basic commands but as important as find, grep or chmod.  mkdir stands for "make directory" and this command is literally used to create directories. Suppose, you need to create a directory tree-like /opt/software/java/app/config, how are you going to create these directories? One by one right? Well, yes you can use mkdir and command to create these directories one by one as shown in below example :

$ cd /opt
$ mkdir software

$ cd software
$ mkdir java

$ cd java
$ mkdir app

$ cd app
$ pwd
/opt/software/java/app/config

This would take almost 8 commands to create above directory structure, unfortunately, you just can not type mkdir /opt/software/java/app/config,  because  parent directories does not exists.





It will simply fail, by the way there is a trick here, you can use mkdir -p command option to create intermediate directories along the way e.g. if /opt exists but /opt/software doesn't you can still use mkdir -p /opt/software/java/app/config to create exact same directory structure as shown below
[# ~]$ mkdir -p software/java/app/config
[# ~]$
[# ~]$ pwd
/home/john
[# ~]$ cd software/java/app/config/
[# ~/software/java/app/config]$

By the way, if you are new to Linux then I also suggest you go through a comprehensive Linux course to learn some basics commands and fundamentals like Linux file system, permissions, and other basic things. 

If you need an online course, I highly recommend Linux Mastery: Master the Linux Command Line in 11.5 Hours on Udemy. It's a very practical and hands-on course to learn Linux fundamentals in a quick time. It's also very affordable and you can buy in just $10 on Udemy flash sales which happen every now and then. 


mkdir command Example in UNIX

Interestingly, mkdir is one of the simplest commands in UNIX, I guess only one which is close to the second simple is pwd. mkdir stands for "make directory" and just has two main command-line options, other than -v which stands for verbose and prints a message for each created directory.

  • -m  stands for mode and used to set the access mode for the new directory.
  • -p   used to create parent directories if not exist

For example, the following command will set initial permission of new directory to 777, so that everyone can access it.
mkdir -m 777 ~/test
On the other hand, our -p option will create test/coding/java directory in one shot.
mkdir -p ~/test/coding/java

You can create even a very complex directory structure like the UNIX file system (as shown in the following diagram) using mkdir -p command.
mkdir -p command example in UNIX



mkdir command: "Permission denied" error in UNIX

By the way, sometimes if you try to create a directory like this(mostly in the directory you don't own) :
$ mkdir dropbox
you may see the following error
mkdir: cannot create directory 'dropbox': Permission denied

As an error message is suggesting, you don't have permission to create this directory in your current working directory. You can use the ls (list) command to figure out what permission you have in your current working directory.

I am sure this simple mkdir command option will save your immense time and effort while working in UNIX. It's a great tool to create or replicate a complex directory structures or tweak them. Ever since I have learned this trick, I don't remember creating directories one by one in UNIX. So remember, mkdir -p command allow you to create an intermediate directory along the way.

Further Learning
Linux Command Line Basics
Linux Command Line Interface (CLI) Fundamentals
Learn Linux in 5 Days and Level Up Your Career

Related UNIX Command Tutorials
  • 10 examples of find command in UNIX (examples)
  • 10 examples of grep command in UNIX (examples)
  • 10 examples of date command in Linux (examples)
  • How to get an IP address from the hostname and vice-versa in Linux (command)
  • 10 examples of xargs command in Linux (examples)
  • 10 examples of tar command in UNIX (examples)
  • 10 examples of Vim in UNIX (examples)
  • How to create, update and delete soft link in UNIX (command)
  • 5 examples of sort command in Linux (examples)
  • 5 examples of kill command in Linux (examples)
  • 10 examples of chmod command in UNIX (examples)
  • 10 tips to work fast in UNIX? (tips)

Thanks for reading this article so far. If you like this article then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.

5 comments :

Andriy said...

Good example of "create even very complex directory structure" is
mkdir /usr/local/src/bash/{old,new,dist,bugs}
from
http://www.gnu.org/software/bash/manual/bashref.html#Brace-Expansion

Anonymous said...

Didn't know aobut this useful options of mkdir command before :-), sometime I just amazed, how much is there to learn even on commands you have used several times.

Anonymous said...

Try the following to create the above example of a very complex directory structure in a subfolder of ~/testdir instead of /

mkdir -p ~/testdir/{bin,sbin,home/{jane,will/{work,play},zeb},tmp,lib,usr/{bin,lib},var}

Anonymous said...

mkdir -p /home/{a,b}

mkdir -p /home/{a/{a1,a2,a3},b/{b1,b2,b3}

Unknown said...

the command is useful but -p also creates the last child directory as parent directory.. is there any way to sort this ???

Post a Comment