What is the mkdir Command in Linux?
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 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.
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 -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:
mkdir –m777 DirM
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 {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 |
Note: Learn to fully manage directories by learning to move a directory in a system running a Linux distribution.
Conclusion
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.
原创文章,作者:745907710,如若转载,请注明出处:https://blog.ytso.com/223788.html