Linux Create Directory
To create a directory in Linux, use the mkdir command. The command can create a single directory, several directories at once, or an entire directory path when parent directories do not yet exist.
The basic command is mkdir directory_name. The new directory is created relative to your current working directory unless you provide an absolute path.
Linux mkdir Command Syntax for Creating a Directory
The basic syntax of the Linux mkdir command is:
mkdir [options] directory_name
For example, the following command creates a directory named tutorialkart in the current working directory.
mkdir tutorialkart
If the command completes successfully, mkdir normally does not print a message. You can use ls to confirm that the directory exists.
Create a Directory using mkdir in Linux Terminal
Following is a step-by-step process to create a directory in Linux.
Step 1: Open Terminal. On many Ubuntu desktop installations, you can use the keyboard shortcut Ctrl+Alt+T. On other Linux desktop environments, open the terminal application from the application menu.
)
This opens a new terminal window.
Step 2: Navigate to the directory in which you would like to create the new directory. You can use pwd to display your current directory and cd to move to another directory.
)
Step 3: Use the mkdir command and provide the directory name as its argument. In the following example, the directory being created is named tutorialkart.
)
The directory is now created in the current location.
Step 4: Check that the new directory was created by running the ls command.
)
Create a Linux Directory at a Specific Path
You do not have to change into a directory before using mkdir. You can give the command a relative or absolute path.
For example, the following command creates reports inside the current user’s Documents directory:
mkdir ~/Documents/reports
An absolute path starts from the root directory /. For example:
mkdir /home/user/projects
The parent directory must already exist for a normal mkdir command. If one or more parent directories are missing, use the -p option.
Create Parent Directories with mkdir -p
The -p option creates any missing parent directories in the path. This is useful when you need to create a nested directory structure in one command.
mkdir -p projects/linux/examples
If projects or projects/linux does not exist, mkdir -p creates those directories before creating examples.
The -p option is also commonly used when a directory may already exist. If the specified directory already exists, mkdir -p does not report the usual “File exists” error for that directory.
mkdir -p tutorialkart
Create Multiple Directories with One mkdir Command
You can provide more than one directory name to mkdir. Each directory is created in the current working directory unless its argument contains another path.
mkdir images documents backups
This command creates three directories named images, documents, and backups.
Create a Linux Directory Name Containing Spaces
If a directory name contains spaces, quote the name so that the shell treats it as one argument.
mkdir "Project Files"
You can also escape the space with a backslash:
mkdir Project\ Files
Create a Linux Directory with Specific Permissions
On Linux systems that support the standard mkdir -m option, you can specify permission bits when creating a directory. For example:
mkdir -m 750 private_reports
In this example, permission mode 750 gives the owner read, write, and execute permissions; members of the group receive read and execute permissions; and other users receive no permissions.
You can inspect the resulting directory permissions with:
ls -ld private_reports
Check Whether a Linux Directory Was Created
The ls command can list the contents of the current directory:
ls
To inspect one particular directory and its permissions, use ls -ld:
ls -ld tutorialkart
For a command or shell script that only needs to test whether a path is a directory, Bash supports the -d test:
if [ -d "tutorialkart" ]; then
echo "Directory exists"
fi
Common mkdir Errors When Creating Linux Directories
If you try to create a directory that already exists without using -p, mkdir reports an error similar to:
mkdir: cannot create directory 'tutorialkart': File exists
Use mkdir -p tutorialkart when it is acceptable for the directory to already exist.
If a parent directory does not exist, a nested path can produce a “No such file or directory” error. Use mkdir -p to create the missing parent directories.
A “Permission denied” error means the current user does not have permission to create a directory at that location. Choose a location where your account has write permission, or use the appropriate administrative procedure only when the directory genuinely needs to be created in a protected system location.
Create a File Inside a Newly Created Linux Directory
mkdir creates directories, not regular files. If you want to create a directory and then an empty file inside it, you can use mkdir followed by touch.
mkdir notes
touch notes/todo.txt
The first command creates the notes directory, and the second creates an empty file named todo.txt inside that directory if the file does not already exist.
Linux mkdir Command Examples at a Glance
| Task | Command |
|---|---|
| Create one directory | mkdir tutorialkart |
| Create a directory at a path | mkdir ~/Documents/reports |
| Create missing parent directories | mkdir -p projects/linux/examples |
| Create multiple directories | mkdir images documents backups |
| Create a name containing spaces | mkdir "Project Files" |
| Create a directory with permission mode 750 | mkdir -m 750 private_reports |
| Check a directory | ls -ld tutorialkart |
Linux Create Directory Tutorial Summary
In this Linux Tutorial, we learned how to create a directory in Terminal using the mkdir command. We also covered creating directories at specific paths, creating missing parent directories with mkdir -p, creating multiple directories, handling spaces in directory names, setting permissions, checking the created directory, and creating a file inside a new directory.
Linux mkdir Editorial QA Checklist
- Confirm that examples distinguish between
mkdirfor directories andtouchfor creating an empty file. - Confirm that nested-directory examples use
mkdir -pwhen parent directories may not exist. - Confirm that directory names containing spaces are quoted or escaped correctly.
- Confirm that permission examples explain the meaning of the numeric mode used with
mkdir -m. - Confirm that verification examples use commands such as
lsorls -ldand do not imply that successfulmkdirnormally prints a success message. - Confirm that protected system paths are not presented as writable by a normal user without appropriate permissions.
TutorialKart.com