Updated May 22, 2023
Definition of Create a Folder in Linux
To create a folder in Linux, we can use the ‘mkdir’ command line in Linux Servers. This command also helps in creating multiple directories at the same time and even grants permissions for the folders. Any user who creates a folder should have enough permissions in the parent directory. Else they might receive a ‘permission denied’ error. In this article, we will learn how to create a folder in Linux Server. We will also see how to set permissions for a folder with examples and screenshots for better understanding.
Syntax:
Syntax to create a folder in Linux:
We use the ‘mkdir’ command to create a folder. Different options are provided or passed through this command as per our requirement. We can specify the number of directories at the same time to create in Linux. The syntax to create a folder is given below:
mkdir [options..] [directories..]
Options Include:
- -m: To set the folder’s permission level. Be default, if no permission is specified, it will take the same permission of its parent.
- -p: It will create the directories structures from parent path.
- -v: It will print the directories created explicitly.
- -Z: By default, it will set the SELinux security.
How to Create a Folder in Linux?
To create a folder in Linux, we can use different options as below. The description of each option is mentioned for better understanding.
Options | Descriptions |
-m, –mode=MODE | It will help in setting the file mode like chmod command. |
-p, –parents | It will create folder structures from parent directories; it will not throw an error if existing. |
-v, –verbose | It prints messages when each directory is created. |
#NAME? | It sets SELinux security data of every directory created by default. |
–context[=CTX] | It is similar to the -Z option, or if CTX is passed in the argument, then it will set the SELinux or SMACK security data to CTX. |
–help | It prints the list of options and exits. |
–version | It will print the output version info and exits. |
Examples to Create a Folder in Linux
Below are the options with examples of how to create a folder in Linux.
1. No Option
When no options are passed through mkdir command in Linux, it will simply create the directories. We can create one directory or multiple directories at a time, as shown below.
Syntax:
mkdir [directories]
Example:
mkdir folder1 folder2 folder3
ls
Output:
2. Option –version
This option will help to display the version of ‘mkdir’ command in Linux. It also provides other information regarding the license and authors. Once all the information is displayed, it will exit.
Syntax:
mkdir --version
Output:
3. Option –help
The option ‘–help’ will display all the options that are available for ‘mkdir’ command with its description, and it will exit.
Syntax:
mkdir --help
Example:
We can see that –help command displays the options available for mkdir command.
4. Option -v, –verbose
It will display all the processes explicitly. For ‘mkdir’ command, it will display all the directories created when we pass this option.
Syntax:
mkdir -v [directories..]
Example:
In the below example, we can see the information on how the folders are been created one by one.
mkdir -v fold1 fold2 fold3
Output:
5. Option -p
The option -p, will help to create directories from the parent path. If the directories are present, then no error is thrown. If we do not specify ‘-p’ and we try creating directories from the parent path, it will throw an error stating that ‘no such file or directory found’.
Syntax:
mkdir -p [directories]
Example:
From the below example, it is clear that the directories are been created from the parent path by using -p option.
mkdir -p -v fold4/first/second/third
Output:
6. Option -m
The option -m helps to set the file permissions. It is similar to chmod command in Linux. We can specify the permissions to the folder as per our requirement.
Syntax:
mkdir -m a=rwx [directories]
Example:
Here the permissions to folder are set to rwx which means the folder is given all the permissions like read, write and execute.
mkdir -m a=rwx folder
ls
Output:
Conclusion
To create a folder in Linux, we can use ‘mkdir’ command line in Linux Servers. This command also helps in creating multiple directories at the same time and even grants permissions for the folders. Any user who creates a folder should have enough permissions in the parent directory. Else they might receive ‘permission denied’ error. In this article, we will learn how to create a folder in Linux Server. The command ‘mkdir’ is one of the most useful commands in Linux to create a folder through CLI.
Recommended Articles
We hope that this EDUCBA information on “Create Folder in Linux” was beneficial to you. You can view EDUCBA’s recommended articles for more information.