/**/ How to create directories using mkdir command in Linux? - Dextutor Linux

How to create directories using mkdir command in Linux?

mkdir command in Linux is used to create new directories. What you call Folders in Windows are called Directories in Linux. Creating directories is important as they help to organize the files. So, lets learn how to use mkdir command in Linux.

Syntax

mkdir [OPTION]...DIRECTORY...

creates directories if they do not already exist.

Example

how to use mkdir command in linux

the first ls command shows there are three files f1, f2 and f3. Next, we use mkdir to create a directory Dir1 which is verified by listing the contents of the directory again by using ls command.

ls -l option can also be used to verify that Dir1 is a directory. You will see in the output that the directory entry starts with ‘d’ while a file’s entry starts with a ‘-‘.

mkdir command can create multiple directories at the same time. For example:

$mkdir Dir2 Music DD1

will create two directories Dir2, Music and DD1. Remember, to give space between directory names.

Options used with mkdir command

1. To given separate permissions

-m: option allows you to assign separate permission other than the default permissions as per umask. So, if you don’t want a particular directory to not have default set of permissions that use -m option to assign new permissions while you create the directory.

$mkdir -m 742 Dir

assigns all permissions to the user, only read to group and only write to others. Check the permissions using

$ls -ld Dir 

2. To create hierarchy of directories

-p: option create a hierarchy of directories.
Suppose you want to create three directories A, B and C such that C is inside B and B is inside A and non of the directories already exist. Normally first we will create A, then inside it B and then inside B we create C.
But using the -p option one can create all the three directories in the specified manner in one go as shown below

$mkdir -p A/B/C
using -p option with mkdir in linux

Here, first a hierarchy of three directories is created using -p option. This can be verified by using the tree command.

Practice Questions on How to use mkdir command in Linux

Q1. Create two directories D1 and D2 by using the mkdir command only once.
Q2. Create a directory DD having the following permissions: read and write for user, read and execute for group and only read for others.

PPT on mkdir command

Video on mkdir command

Relevant Commands

touch
rm
rmdir
chmod

Leave a Comment