• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

How to Create Directories in Linux (mkdir Command)

Written by Admin, Updated On December 29, 2019
mkdir, terminal
How to Create Directories in Linux (mkdir Command)

You can create new directory on your Linux systems using command line or using UI of file manager. The mkdir command allows you to create a new directories (folders) on your Linux systems. This guide explains use of mkdir command with examples.

Linux mkdir Command Syntax#

The basic syntax for the mkdir command is as below:

mkdir [OPTION] [DIRECTORY]

You can pass more then one directory names as its arguments.

Create a New Directory#

In Linux systems, you can pass directory names as arguments along with mkdir command. For instance, to create a new directory test_dir run below command:

mkdir test_dir

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

ls -l
drwxrwxr-x 2 tecnstuff tecnstuff 4096 Dec 29 01:12 test_dir

When you are in current working directory and run ls command it will just show directory name without full path. To change current working directory to test_dir, use cd command.

You can create directory to other location using absolute or relative path of parent directory.

mkdir /var/www/test_dir

If user will not have sufficient permission then it will throw Permission denied error message:

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

Create Parent Directories#

You can also create parent directory for any other directory. To create parent directories, use mkdir command with the -p option.

For instance, you want to create directory /home/tecnstuff/Documents/Tec/Stuff

mkdir /home/tecnstuff/Documents/Tec/Stuff

You will get an error as shown below if any of the parent directories don’t exist:

mkdir: cannot create directory '/home/tecnstuff/Documents/Tec/Stuff': No such file or directory

You can create missing parent directories by invoking mkdir command with -p option:

mkdir -p /home/tecnstuff/Documents/Tec/Stuff

The -p option doesn’t effect if directory exists, it will create the directory only if it doesn’t exist.

If you will try to create directory which is exists without -p option then it will show File exists error message:

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

How to Create Multiple Directories#

If you want multiple directories within a single command, just pass directories’ names as arguments by space separated:

mkdir test1 test2 test3

Set Permissions while Creating a Directory#

You can set specific directory permissions while creating directory using mkdir command. Use -m (-mode) option along with mkdir command and syntax will be same as chmod command:

For example, we are creating a directory with 700 permission so directory will be accessible only by creator of directory.

mkdir -m 700 test_dir

If the mkdir used without -m option then directories will have 775 or 755 permissions.

Conclusion#

The mkdir command in Linux is basic command and used to create new directories. You also can create multiple directories in a single command. To get more details about mkdir, visit the mkdir man page.

If you have questions or feedback, please leave a comment below.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article How to Install Java on Debian 10 (Buster)
Next Article   How to use Grep Command in Linux

Related Posts

  • How to Install SSH Keys on Ubuntu 22.04

    How to Set up SSH Keys on Ubuntu 22.04

    January 7, 2023
  • How to Install Fail2ban on Ubuntu 22.04

    How to Install and Configure Fail2ban on Ubuntu 22.04

    December 5, 2022
  • How to Enable SSH on Ubuntu 22.04

    How to Enable SSH on Ubuntu 22.04

    December 1, 2022

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install SSH Keys on Ubuntu 22.04
    How to Set up SSH Keys on Ubuntu 22.04 January 7, 2023
  • How to Install Mongodb on Debian 11
    How to Install MongoDB on Debian 11 Linux January 11, 2023
  • How to Install Puppet Agent on Ubuntu 22.04
    How to Install Puppet Agent on Ubuntu 22.04 January 22, 2023
  • How to Install Python 3.11 on Debian 11
    How to Install Python on Debian 11 January 25, 2023
  • How to Change-Hostname Ubuntu 22.04
    How to Change Hostname on Ubuntu 22.04 January 19, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook