To create a directory in Linux, use the command mkdir
– this command is an acronym for “make directory”. mkdir command is pretty simple to use and this article is for beginners to become familiar with Linux.
mkdir’s syntax
Table of Contents
Basic command syntax is as follows:
mkdir directory...
A note on notation: When three periods follow an argument in the description of a command (as above), it means that the argument can be repeated, thus:
mkdir dir1
would create a single directory named dir1
, while
mkdir dir1 dir2 dir3
would create three directories named dir1
, dir2
, and dir3
.
Options of the mkdir command
To display the full options of the mkdir
command, use the following command:
mkdir --help
Create folder with permissions
Here, the article only gives you a brief overview of permissions in Linux. Normally, we will use the chmod
command to set permissions.
There are three user levels:
- Owner
- Group
- Other
There are three possible settings:
- Read – has a value of 4
- Write – has a value of 2
- Execute – has a value 1
As the image above, you see the permissions set to have a value of 755
. What does this mean? This means that the user is the owner who has permissions including read, write, execute (4 + 2 + 1
). Group members have the permissions to read and execute (4 + 1
) and other users have the permission to read and execute.
The mkdir
command supports the -m
parameter, so you can set permissions right at the time of creating that directory.
-m, --mode=MODE
: set file mode (as in chmod), not a=rwx – umask
You can see above that I set the permissions for directory name dir1
is 755
, and permissions for directory name dir2
is 700
.
Create directory without parent directory
Mkdir support a parameter that is useful -p
, this parameter allows you to create subfolders without concern that the parent directory exists or not. Because it will create both the parent directory and the subdirectory at the same time.
Recommended Reading: Copy files and directories with “cp” command
As you can see, when I run mkdir
command withou -p
, it return error.
Conclusion
Although the mkdir
command is very simple and with the examples above we can create folders with more options. That is the option that probably few people use but very useful.
(This is an article from my old blog that has been inactive for a long time, I don’t want to throw it away so I will keep it and hope it helps someone).