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 Loop Through a List of Strings

A list of strings or array or sequence of elements can be iterated by using for loop in bash. How you can iterate the list...
29/12/2020

Bash exit on error

An exit status code is returned when any Linux command is executed from the terminal, either the command is successful...
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
Bài Viết

Bài Viết Mới Cập Nhật

Mua proxy v4 chạy socks5 để chơi game an toàn, tốc độ cao ở đâu?
18/05/2024

Thuê mua proxy Telegram trọn gói, tốc độ cao, giá siêu hời
18/05/2024

Thuê mua proxy Viettel ở đâu uy tín, chất lượng và giá tốt? 
14/05/2024

Dịch vụ thuê mua proxy US UK uy tín, chất lượng số #1
13/05/2024

Thuê mua proxy Việt Nam: Báo giá & các thông tin MỚI NHẤT
13/05/2024