Using mkdir command to make a directory in Linux
 

Using mkdir command to make a directory in Linux

 

There are different ways to create a directory (folder) in your Linux system. If you are using a desktop environment then you can use the file manager to create or delete a directory.

Another way is to use mkdir commands provided by your operating system. By using mkdir you can create single or multiple directories using a single command. In this article, we will see how you can create a directory using a terminal in Linux.

How to create a directory using terminal

For creating a directory in Linux/ Unix mkdir command is used. The syntax of mkdir command is given below –

mkdir [option] [directory_name]

For example –

To create a directory dir1 inside your current working directory use the following command –

mkdir dir1

You can view the created directory by using ls command –

ls

If dir1 is already there it will display the message ” mkdir: can’t create directory ‘dir1’: File exists “.

Creating a directory to a specific location in the filesystem you need to use the full path from your current location in the terminal. For instance, to create a directory dir1 inside the /usr directory we will use –

mkdir /usr/dir1

Please note that while creating a directory at some places such as inside the /root directory in the filesystem you may need to have the root privileges. So use the sudo at the beginning of the command.

Creating multiple directories using a single command

Let’s say you want to create multiple directories i.e. dir1, dir2, dir3 inside the current working directory then use the following command –

mkdir dir1 dir2 dir3

Now if you run the ls command you can see the created directories inside your current working directory.

ls

How to create a hidden directory

To create a new hidden directory dir1 inside your current working directory you need to use the following command –

mkdir .dir1

To display hidden directories in your terminal use –

ls -a

To make dir1 visible use –

mv .dir1 dir1

How to create nested directories using mkdir command

Nesting of directories means creating one directory inside another. You need to use the option -p with mkdir command to create multiple nested directories. Now use the following command to create nested directories –

mkdir-p dir1/dir2/dir3/dir4

You can replace the directory’s names with yours.

Now you can see the nested directory structure using the tree command as it is given below –

tree dir1

How to create a directory and set permissions on it

You can set permissions on directories while creating them. You need to use -m option with mkdir command. To set permission read, write, and execute for all on dir1 you can use the following command in your terminal –

mkdir -m 777 dir1

For more options to use with mkdir you can see its manual page by using –

man mkdir

Conclusion

Now you know how to use mkdir command if you have a query feel free to leave it in the comments below.

Previous how to fix permission for ubuntu server installed docker
Next LPS plugins all over the place
 

About author

You might also like

Commands 0 Comments

How to rename files & directories in Linux?

There are various ways in which you can rename the files or directories on a Linux system. If you are using a system with a graphical user interface then you

Commands 0 Comments

How to format a disk using mkfs command in Linux

The data on a computer can be stored on different storage devices such as hard disk, USB drive, etc. This data need to be positioned in a systematic way so

Linux Terminal 0 Comments

How to Compile and Run C Program on a Linux Terminal

This post was last updated on April 17th, 2021 at 11:08 pmLinux is the best platform to learn C programming because it is an open source platform and you will

0 Comments

No Comments Yet!

You can be first to comment this post!

Leave a Reply