Definition of Create a Folder in Linux
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 grant the 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. 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 ‘mkdir’ command to create a folder. Different options are provided or passed through this command as per our requirement. We can specify number of directories at the same time to create in Linux. The syntax to create a folder is given as 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 error if existing. |
-v, –verbose | It prints messages when each directories are created. |
#NAME? | It sets SELinux security data of every directory created by default . |
–context[=CTX] | It is similar to -Z option, or if CTX is passed in the arguement 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 on 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
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
Example:
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 process 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
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 parent path, it will thrown 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
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 is set to rwx which means the folder is given all the permissions like read, write and execute.
mkdir -m a=rwx folder
ls
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 grant the 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 learnt how to create a folder in Linux Server. The command ‘mkdir’ is one of the most useful command in Linux to create a folder through CLI.
Recommended Articles
This is a guide to Create Folder in Linux. Here we also discuss the definition and how to create a folder in linux along with examples to create a folder in linux. You may also have a look at the following articles to learn more –
3 Online Courses | 3 Hands-on Projects | 21+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses