- mkdir command: Create New Directories in Linux
- mkdir command examples
- Create new directories
- Create multiple directories
- Create nested directories
- Create directory with specific permissions
- How to Create Directories in Linux (mkdir Command)
- Linux mkdir Command Syntax #
- How to Create a New Directory #
- How to Create Parent Directories #
- How to Set Permissions when Creating a Directory #
- How to Create Multiple Directories #
- Conclusion #
- How to Use mkdir Command to Make or Create a Linux Directory
- mkdir Command Syntax in Linux
- How to Make a New Directory In Linux
- How to Create Multiple Directories with mkdir
- How to Make Parent Directories
- How to Set Permissions When Making a Directory
- How to Verify Directories
- mkdir Command Options and Syntax Summary
mkdir command: Create New Directories in Linux
mkdir is one of the essential Linux commands that every Linux user should know. You can create new directories using mkdir.
One of the essential Linux commands is mkdir. The mkdir allows you to make new directories (folders in common term) in Linux.
In this beginner series, you’ll learn to use the mkdir command.
mkdir command examples
The mkdir command is one of the rare few Linux commands that doesn’t have tons of options. And that makes it really simple to use.
mkdir [option] directory_name_or_path
Create new directories
To make a new directory, use mkdir command without any option:
This will create a new directory named new_dir in the present directory. You can check it using the ls command.
[email protected]:~/tuts$ ls [email protected]:~/tuts$ mkdir new_dir [email protected]:~/tuts$ ls -l total 4 drwxrwxr-x 2 abhishek abhishek 4096 May 14 16:15 new_dir
You may also specify the path to where you want to create the new directory.
[email protected]:~/tuts$ ls new_dir [email protected]:~/tuts$ mkdir new_dir/another_new_dir [email protected]:~/tuts$ tree . └── new_dir └── another_new_dir 2 directories, 0 files
Create multiple directories
You may also create several new directories with a single command:
mkdir new_dir_1 new_dir_2 new_dir_3
All the new directories are created at the same level. You may also create nested directories which is described in the next section.
Create nested directories
You can use the option -p to create a nested directory structure. If the parent directory doesn’t exist, it will create it for you.
This is particularly helpful when you want to create a directory structure or if you want to make sure that the directory path exists.
This is what the above command created:
[email protected]:~/tuts$ mkdir -p dir1/dir2/dir3/dir4 [email protected]:~/tuts$ tree . ├── dir1 │ └── dir2 │ └── dir3 │ └── dir4 └── new_dir └── another_new_dir 6 directories, 0 files
You may also use the -p option with a single directory. It won’t create a new directory that already exists, but it won’t throw any errors as well:
[email protected]:~/linuxhandbook$ mkdir new_dir mkdir: cannot create directory ‘new_dir’: File exists [email protected]:~/linuxhandbook$ mkdir -p new_dir [email protected]:~/linuxhandbook$ ls -l total 8 drwxrwxr-x 3 abhishek abhishek 4096 May 14 16:39 dir1 drwxrwxr-x 3 abhishek abhishek 4096 May 14 16:16 new_dir
Create directory with specific permissions
By default, your shell’s umask controls the permission on the newly created directories. If you want different file permissions on the directory, instead of creating the directory first and then changing the permission with the chmod command, you can use the -m option.
Suppose you want permission 766 on the directory you are going to create. You can use:
mkdir -m 766 new_directory
That’s pretty much what you need to know about the mkdir command. Now that you know how to create directories, maybe you would want to learn about the rmdir command and deleting directories in Linux command line.
How to Create Directories in Linux (mkdir Command)
In Linux systems, you can create new directories either from the command line or with the help of your desktop’s file manager. The command that allows you to create directories (also known as folders) is mkdir .
This tutorial covers the basics of using the mkdir command, including everyday examples.
Linux mkdir Command Syntax #
The syntax for the mkdir command is as follows:
The command takes one or more directory names as its arguments.
How to Create a New Directory #
To create a directory in Linux, pass the directory’s name as the argument to the mkdir command. For example, to create a new directory newdir , you would run the following command:
You can verify that the directory was created by listing the contents using the ls command :
drwxrwxr-x 2 username username 4096 Jan 20 03:39 newdir
When providing only the directory name, without the full path, it is created in the current working directory.
The current working directory is the directory from which you are running the commands. To change the current working directory, use the cd 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 /tmp directory you would type:
If you try to create a directory in a parent directory where the user does not have sufficient permissions, you will receive Permission denied error:
mkdir: cannot create directory '/root/newdir': Permission denied
The -v ( —verbose ) option tells mkdir 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 -p option.
Let’s say you want to create a directory /home/linuxize/Music/Rock/Gothic :
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 command with the -p option:
mkdir -p /home/linuxize/Music/Rock/Gothic
When the -p 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 -p option is not provided, mkdir will print File exists error:
mkdir: cannot create directory 'newdir': File exists
How to Set Permissions when Creating a Directory #
To create a directory with specific permissions, invoke the mkdir commanf with the -m ( -mode ) option. The syntax for assigning permissions is the same as with the chmod command.
In the following example, we’re creating a new directory with 700 permissions, which means that only the user who created the directory will be able to access it:
When the -m option is not used, the newly created directories usually have either 775 or 755 permissions, depending on the umask value.
How to Create Multiple Directories #
To create multiple directories, specify the directories’ names as the command arguments, separated by space:
The mkdir command also allows you to create a complex directory tree with one command:
mkdir -p Music/,Classical/Baroque/Early>
The command above creates the following directory tree :
Music/ |-- Classical | `-- Baroque | `-- Early |-- Disco |-- Folk |-- Jazz | `-- Blues `-- Rock |-- Gothic |-- Progressive `-- Punk
Conclusion #
The mkdir command in Linux is used to create new directories.
For more information about mkdir , visit the mkdir man page .
If you have questions, feel free to leave a comment below.
How to Use mkdir Command to Make or Create a Linux 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.
- 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:
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.
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
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.
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 -p 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 ls -R to show the recursive directory tree.
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, 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:
By getting the feedback from the process, you do not have to run the ls command to verify the directory was created.
mkdir Command Options and Syntax Summary
Option / Syntax | Description |
---|---|
mkdir directory_name | Creates a directory in the current location |
mkdir | 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 |
Note: Learn to fully manage directories by learning to move a directory in a system running a Linux distribution.
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.