How to Create a Directory In Linux

Creating directories or folders is a fundamental task for all Linux users. Linux enthusiasts can easily create directories from the distro’s desktop environment or the command-line using mkdir (also known as the “make directory” command).

Note: We’re using Ubuntu 20.4, a Debian-based distro, for this guide. All of the Linux commands featured in this article are distro-independent, meaning they will work on any Linux distro.

How to Create Directories via the Desktop Environment’s Graphical User Interface (GUI)

  1. To create a directory in Linux via the GUI, open the directory you want to create the subdirectory in.Initial GUI directory structure.
  2. Left-click anywhere in the folder, as long as it’s not on a file or folder.
  3. Select New Folder from the dropdown menu.

    Create a directory in the desktop environment.
    Create a directory in the desktop environment.

  4. Lastly, rename the directory. That’s it! The folder has been created.
Final directory structure after subdirectory has been created and renamed.
Final directory structure after subdirectory has been created and renamed.

mkdir Command Options/Syntax

The syntax for the mkdir command is as follows:

# mkdir directoryname
# mkdir directoryname1 directoryname2
# mkdir [option] directoryname
# mkdir -p directoryname1/directoryname2/directoryname3

The table below lists all the options available with the mkdir command.

Options Description
-m, –mode=MODE set file mode (as in chmod), not a=rwx – umask
-p, –parents no error if existing, make parent directories as needed
-v, –verbose print a message for each created directory
-Z set SELinux security context of each created directory to the default type
–context[=CTX] like -Z, or if CTX is specified then set the SELinux or SMACK security context to CTX
–help display this help and exit (same as man mkdir)
–version output version information and exit

How to display the version of the mkdir command

Throughout this guide, we will be using the mkdir command extensively. It’s always good to see what version of the mkdir command you’re using. To do this, type the following command in the terminal:

# mkdir --version
Show the version of the mkdir command, mkdir --version.
Show the version of the mkdir command, mkdir –version.

If mkdir is not the latest version, you can update it using the apt upgrade command.

# sudo apt upgrade.

The sudo command in front of the apt upgrade command ensures that the command runs as root. You will have to enter the root password for the command to work.

How to Create a Single Directory

Note that we will be using the -v parameter when creating directories. This verifies that the directories were created. Also, note that you must have sufficient privileges to run the command.

# mkdir -v directory1
Make a directory, mkdir -v directory1.
mkdir -v directory1 – create the directory1 directory.

This command will result in creating the directory in the current working directory. If you don’t want to use the mkdir -v command, you can verify the directory was created using the ls -l command.

# ls -l
List the updated directory, ls-l.
List the updated directory, ls-l.

How to Create Multiple Directories

If you want multiple directories, you can create them one-by-one using the mkdir directory command multiple times. However, this is time-consuming. To save time, use the mkdir command using commas.

# mkdir -v directory1,directory2,directory3
Creaate multiple directories with a single command, mkdir -v directory1,directory2,directory3.
Creaate multiple directories with a single command, mkdir -v directory1,directory2,directory3.

Note: don’t add a space between the directory names, or the directory names will each have an extra character (space).

How to Create Parent Directories

If you want to create multiple directories, use the mkdir -p command separating the directories with slash symbol (/) between each directory. Remember not to add a space between the /directory/. In this example, directory1 is the parent directory, directory2 is a subdirectory of directory1, and directory3 is a subdirectory of directory2.

# mkdir -p -v directory1/directory2/directory3
Create a directory tree, mkdir -p -v directory1/directory2/directory3.
Create a directory tree, mkdir -p -v directory1/directory2/directory3.

How to Set Permissions When Creating Directory/Directories

To create directories with preset permissions, it’s necessary to use the -m or –mode option. In the example below, we create a directory with the permission “777“. This means the directory is readable, writable, and executable by all users. However, using “777” is not recommended as it poses a security risk.

# mkdir -m "777" -v directory1
Create a directory with specific permissions, mkdir -m "777" -v directory1.
Create a directory with specific permissions, mkdir -m “777” -v directory1.

Alternatively, you can use the  –mode option.

#mkdir --mode "777" directory1
Create a directory with specific permissions, mkdir -p "777" -v directory1.
Create a directory with specific permissions, mkdir -p “777” -v directory1.

Commands Used In This Article

  • mkdir – make directories.
  • ls – list directory contents.
  • sudo – allows a permitted user to execute a command as the superuser or another user, as specified by the security policy.
  • apt upgrade – Debian/Ubuntu specific command to upgrade application(s) on the system.

Final words

Creating new directories is an essential skill set for all Linux users. There are also several directory-related Linux commands such as ls, cdrmdirpwd, and ls that you should familiarize yourself using the man command.

Leave a Comment