Bash ‘mkdir’ not existent path

29/12/2020
mkdir’ is the basic built-in shell command of Linux to create a new directory or folder from the terminal. You can create a new directory by giving new directory name with ‘mkdir’ command. But if the directory name already exist before executing the command then it will display an error message.  When you want to create a directory in a path that does not exist then an error message also display to inform the user. If you want to create the directory in any non-exist path or omit the default error message then you have to use ‘-p’ option with ‘mkdir’ command. How you can use ‘mkdir’ directory to create directory or folder in non-exist path and with permissions are shown in this tutorials.

Create simple directory or folder

Suppose, you want to create a directory in /home folder named ‘mydir’. Run the following command to create the directory. If no directory exists with the name ‘mydir’ before then the command will be executed without any error. Run ‘ls’ command to check the directory is created or not.

$ mkdir mydir
$ ls

Create multiple directories

Run the following command to create multiple directories using ‘mkdir’ command. Three directories, temp1, temp2 and temp3 will be created after executing the command.

$ mkdir temp1 temp2 temp3
$ ls

Create directory when the directory path not exist

Suppose, you want to create a directory in a path, /picture/newdir/test. In the current system, ‘mydir’ directory has no directory or files in it. So, the path is invalid. Run the ‘mkdir’ command with the above path. An error message will appear after running the command.

$ mkdir /picture/newdir/test

If you want to create non-exist path forcefully by creating all non-exist directories mentioned in the path from terminal then run ‘mkdir’ command with ‘-p’ option.

$ mkdir -p /picture/newdir/test

Now, check the directories are created or not by running the following commands.

$ cd picture
$ ls -R

Create directory with permission

When you create a new directory then a default permission is set for the newly created directory.

Create a new directory and check the default permission by executing following commands. ‘stat’ command is used to check current permission of any existing directory. The default directory permission is ‘rwxr-xr-x’. This indicates directory owner has all permissions, and group users and others users have no write permission.

$ mkdir newdir1
$ stat newdir1/

‘-m’ option is used to set the directory permission at the time of directory creation. Run the following commands to create a directory with all permissions and check the permission using ‘stat’ command. The output shows all types of users have all permissions.

$ mkdir -m 777 newdir2
$ stat newdir2/

Create directory using script

You can test any directory is exist or not by using bash script. Create a bash file and add the following code to create the new directory after testing the directory is exist or not by using ‘-d’ option. If the directory exists then it will show the message, “Directory already exists”, otherwise new directory will be created.

#!/bin/bash

echo -n "Enter the directory name:"
read newdirname
if [ -d "$newdirname" ]; then
echo "Directory already exists" ;
else
`mkdir -p $newdirname`;
echo "$newdirname directory is created"
fi

Run the script and check the directory is created or not.

$ bash create_dir.sh
$ ls

Hope, you will be able to use ‘mkdir’ command with various options more effectively after reading this tutorial. Thank you.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Bash Split String Examples

We need to split the string data for various purposes in the programming. Many programming languages have a built-in function...
29/12/2020

Bash wait for keypress

`read` command is used to take user input in a bash script. We can take input in bash script by using various types of...
29/12/2020

How to use arrays in Bash

When you want to use multiple data using single variable in any programming language then you have to use array variables....
28/12/2020