How To Create Directories/Folders In Linux?

Directories or Folders are used to store files and folders. Directories can be created from GUI by using a desktop environment and application or command-line interface commands. In this tutorial, we will learn how to create directories in Linux. This tutorial can be used for all major Linux distributions like Ubuntu, Debian, Mint, Kali, CentOS, Fedora, RHEL, SUSE, etc.

mkdir Command Syntax

The mkdir command the standard command used to create directories. The mkdir command has the following syntax.

mkdir OPTIONS DIRECTORY
  • OPTIONS is used to set different attributes about the directory creation. This options is optional.
  • DIRECTORY is single or multiple directories which will be created. This option is required.

List Existing Directories

Before starting to create new directories and folders we can list existing directories and folders to get their names. Also after creating the directories and folders created directories and folders can be listed too. The ls command is used to list directories and folders.

ls

Create Diretory

Even the mkdir provides different features we will start with a simple example where we will create a single directory with the specified name. The new directory name will be “data” for the following example.

mkdir data

We can list the created directory with the ls command like below.

ls

The absolute path or full path can be used to create a directory that is a more reliable and flexible way. By default, the given directory will be created in the current working path but by using the relative path directories can be created in other paths. In the following example, we will create a directory named data in the “/var/log“.

mkdir /var/log/data

Relative paths can be also used to create directories. Relative path is set according to the current working directory or given path specifier. In the following example we will create the directory named “data” in the parent directory.

mkdir ../data

Create Multiple Directories

The mkdir command can be also used to create multiple directories in a single command. Just provides the directory names by separating them with spaces.

mkdir data test backup

Also, the mkdir command can be used to create multiple directories with their absolute or full path. This is a more reliable and flexible way to create directories.

mkdir /etc/test /var/log/data

Create Non-Existin Multi-Level Directories

Multi-level directories can be created with the mkdir command even some intermediate directories do not exist. These non-existing directories and child directories will be also created by using the -p option. Just provide the child and parent directories you want to create. In the following example, the directory year and 2020 do not exist but will be created too.

mkdir -p ./year/2020/December

Set Permissions During Directory Creation

By default when a directory is created the umask value will be set as permission. Also, the chmod command can be used to change given single or multiple directories permissions but the mkdir command also supports settings permission during creation. The -m option can be used to set different than default permissions by providing the numerical representation of the permissions. In the following example, we will create a directory named “newdir” which has 777 permission.

mkdir -m 777 newdir

Alternatively multiple created directories permissions can be set with the -m option like below.

mkdir -m 777 newdir data backup

“cannot create directory: File exists” Error

The “cannot create directory: File exists” error occurs when the given directory is already exist. So the new directory can not be created and overwritten.

“cannot create directory: No such a file or directory” Error

The “cannot create directory: File exists” error can occur in different situations where any of the parent directories in the given path do not exist and so the given directory can not be created because of the parent directory. This error can be solved by creating parent directories too by using the -p option which is described previosuly.

mkdir -p /home/ismail/Music/Rammstein/2000

Create Directory with GUI

The GUI tools like File Manager etc. can be used to create directories. But it has limited functionalities according to the mkdir command. For example multiple directories can not be created with GUI. Below we will create a new directory by right-clicking and selecting the “New Folder“.

Create New Folder/Directory

In this step, we will specify the directory name we want to create in the current path. The last step is clicking on the “Create” button.

Leave a Comment