[email protected]Support 24/7 · Billing 09:00 – 00:00LinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
Getting startedJune 21, 2023

How to Create Directories with mkdir command.

How To Create Directories With Mkdir Command.

This tutorial explain how to use the mkdir command.

 

mkdir Command Syntax:

The syntax for the mkdir command is as follows:

mkdir [OPTION] [DIRECTORY]

The command takes one or more directory names as its arguments.

 

How to Create a New Directory

To create a directory in Linux, pass the directory’s name as the argument to the mkdir command. For example, to create a new directory newdir, you would run the following command:

mkdir newdir

You can verify that the directory was created by listing the contents using the ls command :

ls -l
drwxr-xr-x  2 root root  4096 Jun 21 13:08 newdirWhen providing only the directory name, without the full path, it is created in the current working directory.

The current working directory is the directory from which you are running the commands. To change the current working directory, use the cd command.

To create a new directory in another location, you’ll need to provide the parent directory’s absolute or relative file path. For example, to create a new directory in the /tmp directory you would type:

mkdir /tmp/newdir

If you try to create a directory in a parent directory where the user does not have sufficient permissions, you will receive Permission denied error:

mkdir /root/newdir
mkdir: cannot create directory '/root/newdir': Permission denied

The -v (–verbose) option tells mkdir to print a message for each created directory.

 

How to Create Parent Directories

A parent directory is a directory that is above another directory in the directory tree. To create parent directories, use the -p option.

Let’s say you want to create a directory /home/nexonhost/Music/Rock/Gothic:

mkdir /home/nexonhost/Music/Rock/Gothic

If any of the parent directories don’t exist, you will get an error as shown below:

mkdir: cannot create directory '/home/nexonhost/Music/Rock/Gothic': No such file or directory

Instead of creating the missing parent directories one by one, invoke the mkdir command with the -p option:

mkdir -p /home/nexonhost/Music/Rock/Gothic

When the -p option is used, the command creates the directory only if it doesn’t exist.

If you try to create a directory that already exists and the -p option is not provided, mkdir will print File exists error:

mkdir newdir
mkdir: cannot create directory 'newdir': File exists

 

How to Set Permissions when Creating a Directory

To create a directory with specific permissions, invoke the mkdir commanf with the -m (-mode) option. The syntax for assigning permissions is the same as with the chmod command.

In the following example, we’re creating a new directory with 700 permissions, which means that only the user who created the directory will be able to access it:

mkdir -m 700 newdir

When the -m option is not used, the newly created directories usually have either 775 or 755 permissions, depending on the umask value.

 

How to Create Multiple Directories

To create multiple directories, specify the directories’ names as the command arguments, separated by space:

mkdir dir1 dir2 dir3

The mkdir command also allows you to create a complex directory tree with one command:

mkdir -p Music/{Jazz/Blues,Folk,Disco,Rock/{Gothic,Punk,Progressive},Classical/Baroque/Early}

The command above creates the following directory tree :

Music/
|-- Classical
|   `-- Baroque
|       `-- Early
|-- Disco
|-- Folk
|-- Jazz
|   `-- Blues
`-- Rock
    |-- Gothic
    |-- Progressive
    `-- Punk

 

Conclusion

The mkdir command in Linux is used to create new directories.

Keep reading
How to use ulimit command in Linux.
October 6, 2023

How to use ulimit command in Linux.

How To Use Ulimit Command In Linux. ulimit is a built-in Linux shell command that allows viewing or limiting system resource amounts that individual users consume. Limiting resource usage is valuable in environments with multiple users and system performance issues. What Is limits.conf? The…

Read article
How to use tput command in Linux.
October 6, 2023

How to use tput command in Linux.

How To Use Tput Command In Linux. The tput command uses the terminfo database to make terminal-dependent information available to the shell. The tput command outputs a string if the attribute CapabilityName is of type string. The output string is an integer if the attribute is of type integer. If…

Read article
How to Extract and Create RAR Files.
October 6, 2023

How to Extract and Create RAR Files.

How To Extract And Create RAR Files. Usually, you’ll come across the .zip or .gz compressed files. However, every now and then, you’ll also encounter RAR files in Linux. Dealing with RAR files in the Linux command line is not that complicated. You extract RAR files with the unrar command: unrar x…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, cloud hosting, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Support 24/7 · Billing 09:00 – 00:00