How to Mkdir Command Works in Linux
Let's look at the mkdir command on linux, and how you can use it via Terminal.
Join the DZone community and get the full member experience.Join For Free
On Linux and UNIX-like systems, like Mac, we use
mkdir to make a new directory from the terminal. To do this, open up a new terminal, and make sure you're in the right directory using the cd command.
The syntax for
mkdir looks like this, where dir_name is the name of your new directory, and
[OPTIONS] are optional settings.
mkdir [OPTIONS] dir_name
For example, to create a new directory called "
hello_world", we would type:
If you want to make multiple directories, just put them in curly brackets, and separate each directory by a comma. For example, the below code makes two directories called "hello" and "world":
Options for Mkdir
mkdir has 3 options that we can add to our command:
- -p - this allows us to make multiple directories within each other.
- -v - this outputs information on the directory or directories created.
- -m - this lets us set the
chmod/modevalue for our directory, i.e. 777.
Let's say we wanted to create a folder structure, where we have a project folder within a parent folder, within a master folder. If we use just
mkdir, we would have to make each individually. Instead, we can use
mkdir -p to make them all at once.
mkdir -p master/parent/project
This will make three directories, each within the other.
If we want to see a message about if
mkdir was successful or not, we can use
mkdir -v master
The above will output the following message:
mkdir: created directory 'master'
-v will not work together - so you have to use one or the other.
If we want to set the mode of a directory, we can set it directly with
mkdir. For example, to create a directory with a
chmod value of
777, we would write the following:
mkdir -m777 master
Creating multiple directories with a certain
chmod or mode using
We can combine
mkdir into one command, if we want. For example, the below code will create the folder structure master/parent/project, and set each directory to a mode of 777:
mkdir -p -v -m777 master/parent/project
Published at DZone with permission of Johnny Simpson, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.