- Linux: Create directory or folder using mkdir command
- mkdir command – Introduction
- Frequently Asked:
- Create a single directory in Linux with mkdir command
- Create multiple directories in Linux with mkdir command
- Linux: Create directory if not exists
- Linux: Create directory and subdirectories in one command – Recursively
- Linux: Create directory with space in name
- Escape the spaces in name and pass to mkdir command
- Escape the spaces in name and pass to mkdir command
- Linux: Create directory with permissions
- Linux: Create folder with date
- Create a directory with today’s date
- Create a directory with today’s date and time
- Summary
- Related posts:
- Share your love
- 1 thought on “Linux: Create directory or folder using mkdir command”
- Leave a Comment Cancel Reply
- Terms of Use
- Disclaimer
Linux: Create directory or folder using mkdir command
In this article we will discuss how to create a single or multiple directories in Linux using mkdir command. Then we will see various examples like, creating folders with space in name or creating folders with different permissions or creating nested directories recursively in single command.
Table of Contents
mkdir command – Introduction
Linux provides a command mkdir to create single or multiple directories,
- -m or –mode: Set the mode / permissions like chmod.
- -p or –parents: Make parent directories if required and show no error in case directory exists.
- -v or –verbose: Print a message for each of the created directory.
mkdir command creates the folder if it does not exists. If the folder already exists then based on the option provided, it will decide whether to show error or not. Let’s see some examples,
Frequently Asked:
Create a single directory in Linux with mkdir command
To create a single directory in Linux, just provide the folder name along with mkdir command. For example,
It created a directory with name ‘project’. Let’s verify this,
Create multiple directories in Linux with mkdir command
To create multiple directories in a single command provide all the names of directories to mkdir command, separated by space. For example,
[email protected]:~/study/project$ mkdir src cfg lib
It created three directories ‘src’, ‘cfg’ and ‘lib’. Let’s verify this,
[email protected]:~/study/project$ ls cfg lib src
We can also use the verbose option i.e. -v while creating multiple directories. It will display the message after creating each directory. For example,
[email protected]:~/study$ mkdir -v test1 test2 test3 mkdir: created directory 'test1' mkdir: created directory 'test2' mkdir: created directory 'test3' [email protected]:~/study$
What if we try to create a directory that already exist ? Will it show error or overwrite it ? Let’s know about this.
Linux: Create directory if not exists
If we use the mkdir command to create a directory that already exist, then by default mkdir will give error. For example,
[email protected]:~/study/project$ ls cfg lib src [email protected]:~/study/project$ mkdir src mkdir: cannot create directory ‘src’: File exists
As ‘src’ directory already exists, therefore when we tried to create a new directory with same name using mkdir command (without any option) then it raised the error.
If you want a behavior that it should create a directory if it does not exist and in case directory already exists then just skip, without any creation.
Then we can do that using -p option. For example,
[email protected]:~/study/project$ ls cfg lib src [email protected]:~/study/project$ mkdir -p src [email protected]:~/study/project$
As ‘src’ directory already exists, therefore mkdir will not create new directory with same name. But as -p option was provided, therefore it didn’t gave an error. It just skipped the folder creation. So, when we use -p option flag with mkdir command then it will only create the directory if does not exist.
Linux: Create directory and subdirectories in one command – Recursively
If you create a folder and subfolders inside it in a single command then you need to provide -p option. Where -p stands for parents, it means if a complete folder path is given and any parent folder in the path is missing, then it will create that too. Let’s understand with some examples,
Let’s try to create a folder ‘source’ and inside the folder ‘source’ create a folder ‘code’ and inside that an another folder ‘test’. Nested folder structure should be like, ‘source/code/test’. If we try to create this nested folder structure with mkdir command without any option then it will give error,
[email protected]:~/study$ mkdir source/code/test mkdir: cannot create directory ‘source/code/test’: No such file or directory [email protected]:~/study$
By default mkdir command, can not create missing parent folders. To automatically create missing parent folders, use the -p option with mkdir. For example,
[email protected]:~/study$ mkdir -p source/code/test
It created the completed nested folder structure recursively. Let’s verify this,
[email protected]:~/study$ ls -R .: source ./source: code ./source/code: test ./source/code/test: [email protected]:~/study$
So, to create folder and subfolders in a single command in linux, use -p option with mkdir command.
Linux: Create directory with space in name
In Linux, creating a directory with space in name is little tricky, because if we directly provide a name with space to the mkdir command then it will create two directories instead of one. Let’s understand with an example,
Suppose we want to create a directory “test dir” using mkdir command. Let’s pass this name with space to mkdir command directly,
It will create two folders ‘dir’ & ‘test’. Let’s verify this,
[email protected]:~/study$ ls dir test [email protected]:~/study$
This is not what we wanted. So, to create a folder with space in name we have two techniques,
Escape the spaces in name and pass to mkdir command
Escape the spaces in the name while passing it to the mkdir command. For example,
It created a folder with space in name. Let’s verify this,
Escape the spaces in name and pass to mkdir command
Encapsulate the folder name with spaces in quotes and pass it to mkdir command,
It created a folder with space in name. Let’s verify this,
[email protected]:~/study$ ls 'course work' 'test dir' [email protected]:~/study$
Linux: Create directory with permissions
To create a directory with permissions we can use the -m option or -mode option. The syntax of permissions that we need to provide with -m option is similar to chmod. For example,
[email protected]:~/study$ mkdir -m 777 test_dir
It will create a directory test_dir with permissions “rwxrwxrwx”. Let’s verify this,
[email protected]:~/study$ ll total 12 drwxrwxr-x 3 ubuntu ubuntu 4096 Dec 15 16:13 ./ drwxr-xr-x 6 ubuntu ubuntu 4096 Dec 15 03:39 ../ drwxrwxrwx 2 ubuntu ubuntu 4096 Dec 15 16:13 test_dir/
Linux: Create folder with date
To create a directory with mkdir command we need to use the command substitutions. For example,
Create a directory with today’s date
It created a folder with today’s date. Let’s verify this,
Here the output of command $(date +%Y-%m-%d) is today’s date in YYYY-MM-DD format and we inserted that string to mkdir command, which in turns creates the folder with today’s date in format YYY-MM-DD.
Create a directory with today’s date and time
[email protected]:~/study$ mkdir $(date +%Y-%m-%d--%H-%M-%S) [email protected]:~/study$ ls 2020-12-15--16-22-15
Here the output of date command was passed to mkdir command and it in turn created a folder with today’s timestamp.
Summary
We learned how to create directories in Linux using mkdir command and we also explored all the options of mkdir command like created nested directories & setting permissions etc.
Related posts:
Share your love
1 thought on “Linux: Create directory or folder using mkdir command”
Hi Good and very useful information about mkdir command. Every option is explained well and easy to understand.
Leave a Comment Cancel Reply
This site uses Akismet to reduce spam. Learn how your comment data is processed.
Terms of Use
Disclaimer
Copyright © 2023 thisPointer
To provide the best experiences, we and our partners use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us and our partners to process personal data such as browsing behavior or unique IDs on this site and show (non-) personalized ads. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Click below to consent to the above or make granular choices. Your choices will be applied to this site only. You can change your settings at any time, including withdrawing your consent, by using the toggles on the Cookie Policy, or by clicking on the manage consent button at the bottom of the screen.
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.