The current working directory is the directory from which you are running the commands. To change the current working directory, use the
ls -l
2 command.To create a new directory in another location, you’ll need to provide the parent directory’s absolute or relative file path. For example, to create a new directory in the
ls -l
3 directory you would type:mkdir /tmp/newdir
If you try to create a directory in a parent directory where the user does not have sufficient permissions, you will receive
ls -l
4 error:mkdir /root/newdir
mkdir: cannot create directory '/root/newdir': Permission denied
The
ls -l
5 [ls -l
6] option tells mkdir newdir
6 to print a message for each created directory.How to Create Parent Directories #
A parent directory is a directory that is above another directory in the directory tree. To create parent directories, use the
ls -l
8 option.Let’s say you want to create a directory
ls -l
9:mkdir /home/linuxize/Music/Rock/Gothic
If any of the parent directories don’t exist, you will get an error as shown below:
mkdir: cannot create directory '/home/linuxize/Music/Rock/Gothic': No such file or directory
Instead of creating the missing parent directories one by one, invoke the
mkdir newdir
6 command with the ls -l
8 option:mkdir -p /home/linuxize/Music/Rock/Gothic
When the
ls -l
8 option is used, the command creates the directory only if it doesn’t exist.If you try to create a directory that already exists and the
ls -l
8 option is not provided, mkdir newdir
6 will print drwxrwxr-x 2 username username 4096 Jan 20 03:39 newdir
5 error:mkdir newdir
mkdir newdir
1How to Set Permissions when Creating a Directory #
To create a directory with specific permissions, invoke the
mkdir newdir
6 commanf with the drwxrwxr-x 2 username username 4096 Jan 20 03:39 newdir
7 [drwxrwxr-x 2 username username 4096 Jan 20 03:39 newdir
8] option. The syntax for assigning permissions is the same as with the drwxrwxr-x 2 username username 4096 Jan 20 03:39 newdir
9 command.In the following example, we’re creating a new directory with
mkdir /tmp/newdir
0 permissions, which means that only the user who created the directory will be able to access it:mkdir newdir
2When the
drwxrwxr-x 2 username username 4096 Jan 20 03:39 newdir
7 option is not used, the newly created directories usually have either mkdir /tmp/newdir
2 or mkdir /tmp/newdir
3 permissions, depending on the mkdir /tmp/newdir
4 value. The mkdir
command in Linux/Unix allows users to create or make new directories. mkdir stands for “make directory.”
With mkdir
, you can also set permissions, create multiple directories [folders] at once, and much more.
This tutorial will show you how to use the mkdir command in Linux.
Prerequisites
- Linux or UNIX-like system.
- Access to a terminal/command line.
- A user with permissions to create and change directory settings.
mkdir Command Syntax in Linux
The basic command for creating directories in Linux consists of the mkdir
command and the name of the directory. As you can add options to this command, the syntax looks like this:
mkdir [option] dir_name
To understand better how to use mkdir
, refer to the examples we provide in the rest of the guide.
Tip: Use cd
to navigate to the directory where you want to create a sub-directory. You can also use the direct path. Use ls to list the directories in the current location.
How to Make a New Directory In Linux
To create a directory using the terminal, pass the desired name to the mkdir command.
In this example, we created a directory Linux on the desktop. Remember commands in Linux and options are case sensitive.
mkdir Linux
If the operation is successful, the terminal returns an empty line.
To verify, use
mkdir Linux
0.How to Create Multiple Directories with mkdir
You can create directories one by one with mkdir, but this can be time-consuming. To avoid that, you can run 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}
Do not add any spaces in the curly brackets for the directory names. If you do, the names in question will include the extra characters:
How to Make Parent Directories
Building a structure with multiple subdirectories using mkdir
requires adding the
mkdir Linux
2 option. This makes sure that mkdir
adds any missing parent directories in the process.For example, if you want to create “dirtest2” in “dirtest1” inside the Linux directory [i.e., Linux/dirtest1/dirtest2], run the command:
mkdir –p Linux/dirtest1/dirtest2
Use
mkdir Linux
4 to show the recursive directory tree.Without the
mkdir Linux
2 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
mkdir Linux
7 option with the user 777 when creating a directory.To create a directory DirM with rwx permissions:
mkdir –m777 DirM
To list all directories and show the permissions sets:
mkdir Linux
8The 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, there is no feedback for successful operations. To see the details of the mkdir process, append the
mkdir {test1,test2,test3}
0 option to the terminal command.Let’s create a Details directory inside Dir1 and print the operation status:
By getting the feedback from the process, you do not have to run the
mkdir Linux
0 command to verify the directory was created.mkdir Command Options and Syntax Summary
Option / SyntaxDescriptionmkdir {test1,test2,test3}
2Creates a directory in the current locationmkdir {test1,test2,test3}
3Creates multiple directories in the current location. Do not use spaces inside {}mkdir {test1,test2,test3}
4Creates a directory structure with the missing parent directories [if any]mkdir {test1,test2,test3}
5Creates a directory and sets full read, write, execute permissions for all usersmkdir {test1,test2,test3}
6Creates a directory in the current locationConclusion
This guide covered all commands you need to create directories in Linux.
Now you understand how to use the Linux mkdir
command. It’s straightforward and simple to use.
If you have the necessary permissions, there should be no error messages when you follow the instructions in this article.