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[alert color=yellow ]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.[/alert]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.