How to Mkdir Command Works in Linux
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.
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.
Making Multiple Directories Within Each Other With Mkdir -P
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.
Verify a Folder Is Created With Mkdir
If we want to see a message about if
mkdir was successful or not, we can use
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.
Setting the Chmod or Mode of a Directory With Mkdir
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:
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
Credit: Source link