- 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 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
- Terminal Basics #2: Making Directories in Linux Terminal
- Making a new directory in Linux
- Creating multiple new directories
- Making multiple nested subdirectories
- Test your knowledge
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 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.
Terminal Basics #2: Making Directories in Linux Terminal
Learn to make new folders in the Linux command line in this part of the Terminal Basics tutorial series.
In the previous chapter of the Terminal Basics series, you learned about changing folders in the Linux command line. I gave an exercise at the end that briefly mentioned making directories. In this part of the series, I’ll discuss how you can make new folders in the Linux command line using the mkdir command.
Making a new directory in Linux
You should be familiar with the concept of absolute and relative paths in Linux by now. If not, please refer to this tutorial. Open the terminal on your system if it is not already opened. Normally, you start with your home directory (/home/username). But for the sake of this tutorial and to recall a couple of things, I presume you are not in your home directory. So, change to your home directory first.
Yes. If you simply enter cd without any options and arguments, it takes you to your home directory. You could also use cd ~ among other methods. Here, make a new directory called practice.
Great! Now you have a dedicated folder where you’ll practice the Linux command line tutorials in this series.
Creating multiple new directories
You just created a new directory. What if you have to create more than one? Let’s say three of them. You may use the mkdir command three times in a row for each of them. It will work. However, it is not really needed. You can save time and effort by creating multiple directories at the same time like this:
Go on and do that please. You can list the contents of the practice directory to see all the newly created directories. More on the ls command later.
Making multiple nested subdirectories
So, you now know about creating multiple directories at once. But what if you have to create a nested directory structure? Let’s say that you have to create a directory subdir2 inside subdir1 inside dir1.
The problem here is that subdir1 does not exist. So if you try `mkdir dir1/subdir1/subdir32, you’ll get an error:
[email protected]:~/practice$ mkdir dir1/subdir1/subdir2 mkdir: cannot create directory ‘dir1/subdir1/subdir2’: No such file or directory
If you didn’t know better, you would go for mkdir dir1/subdir1 and then run mkdir dir1/subdir2 . That will work. However, there is a much better way. You use the -p option, which makes parent directories if needed. If you run the command below:
mkdir -p dir1/subdir1/subdir2
There is no naming convention, but it is better to avoid spaces in file and directory names. Use underscore or dash instead because handling spaces in file/directory names requires special effort.
Test your knowledge
- Without entering the dir2 directory, create two new subdirectories in it.
- Without entering the dir3 directory, create two-level nested subdirectories (subdir1/subdir2)
- Change to the dir2 directory. From here, create a directory named temp_stuff in your home directory. Don’t worry; we will delete it later in this tutorial series.
- Go back to the parent practice directory and try to create a directory named dir3 . You see an error. Can you make it go away with the -p option?
In the next chapter of the Terminal Basics series, you’ll learn about listing the contents of a directory with the ls command.
Do let me know if you have questions or suggestions.