1. Open the terminal window.
2. Type the command “mkdir” followed by the name of the directory you want to create. For example, to create a directory called “mydir”, type “mkdir mydir”.
3. Press Enter. The directory will be created in the current working directory.
4. To create multiple directories at once, type “mkdir dir1 dir2 dir3” and press Enter. This will create three directories called “dir1”, “dir2” and “dir3” in the current working directory.
5. To create a directory in a different location, type “mkDIR /path/to/directory” and press Enter. This will create the directory in the specified location.
6. To create a directory and all its parent directories, type “MKDIR -p /path/to/directory” and press Enter. This will create the directory and all its parent directories if they don’t already exist.
Create directory in linux. In this tutorial, you will learn how to create directories in linux uisng The mkdir
command with command prompt or terminal.
Note that, The mkdir command allows users to create or make new directories in linux system.
This tutorial guide will help you to create directories in linux system using mkdir command and you can also set permissions, create multiple directories (folders) at once, and much more.
Prerequisites
- Linux or UNIX-like system.
- Access to a terminal/command line.
- A user with permissions to create and change directory settings.
Syntax of mkdir Command in Linux
First of all, you need to know the basic command for creating directories in Linux consists of the mkdir
command and the name of the directory. as shown below:
How to Make a New Directory In Linux
To create a directory using the mkdir command in linux with terminal.
See the following example of how to create directory in linux:
If the operation is successful, the terminal returns an empty line.
To verify, use ls
.
Note: To create a hidden directory, follow our guide on how to show and create hidden files in Linux.
How to Create Multiple Directories with mkdir
If you want to create multiple directory at once command. So, you can use a single mkdir command to create multiple directories at once.
To do so, use the curly brackets {} with mkdir and state the directory names, separated by a comma.
mkdir {test1,test2,test3}
Note that, Do not add any spaces in the curly brackets for the directory names.
How to Make Parent Directories
Building a structure with multiple subdirectories using mkdir
requires adding the -p
option. This makes sure that mkdir
adds any missing parent directories in the process.
For example, if you want to create “mydirtest2” in “mydirtest1” inside the Linux directory (i.e., Linux/mydirtest1/mydirtest2), run the command:
mkdir –p Linux/mydirtest1/mydirtest2
Note that, you can use ls -R
to display the recursive directory tree in linux. And Without the -p
option, the terminal returns an error if one of the directories in the string does not exist.
How to Set Permissions When Making a Directory
The mkdir
command by default gives rwx permissions for the current user only.
To add read, write, and execute permission for all users, add the -m
option with the user 777 when creating a directory.
To create a directory DirM with rwx permissions:
To list all directories and show the permissions sets: -l
The directory with rwx permissions for all users is highlighted. As you can see on the image above, two other directories by default have rwx permission for the owner, xr for the group and x for other users.
How to Verify Directories
When executing mkdir
commands for creating a directory in linux, there is no feedback for successful operations. To see the details of the mkdir process, append the -v
option to the terminal command.
Let’s create a Details directory inside Dir1 and print the operation status:
mkdir Command Options and Syntax Summary
Option / Syntax | Description |
---|---|
mkdir directory_name |
Creates a directory in the current location |
mkdir {dir1,dir2,dir3,dir4} |
Creates multiple directories in the current location. Do not use spaces inside {} |
mkdir –p directory/path/newdir |
Creates a directory structure with the missing parent directories (if any) |
mkdir –m777 directory_name |
Creates a directory and sets full read, write, execute permissions for all users |
mkdir –v directory_name(s) |
Creates a directory in the current location |
Conclusion
This guide covered all commands you have learned how to create directories in Linux and how to set permission for directory.