Managing files and directories is a fundamental skill for Linux users. Whether you’re a seasoned sysadmin or a new Linux enthusiast, understanding how to create and manage directories is crucial. In this article, we will explore how to create a folder in Linux using the mkdir
command, covering various options and scenarios.
Definition of Creating a Folder in Linux
To create a folder in Linux, the mkdir
(make directory) command is used. This command allows users to create single or multiple directories simultaneously and set various permissions as required. It’s essential for users to have the necessary permissions in the parent directory; otherwise, they may encounter a “permission denied” error. In this guide, we’ll walk you through how to use the mkdir
command and set permissions effectively.
Syntax
The basic syntax for the mkdir
command is:
mkdir [options...] [directories...]
Options for mkdir
The mkdir
command supports several options that enhance its functionality:
- -m: Set the folder’s permission level. By default, if no permission is specified, it inherits permissions from its parent directory.
- -p: Create parent directories as needed. It will not throw an error if directories already exist.
- -v: Print a message for each directory that is created.
- -Z: Set SELinux security context. By default, it applies the default SELinux security.
- –context[=CTX]: Similar to
-Z
, but allows specifying a custom SELinux or SMACK security context ifCTX
is provided. - –help: Display a help message with available options and exit.
- –version: Display the version information of
mkdir
and exit.
How to Create a Folder in Linux
Basic Usage
To create a simple directory, use:
mkdir foldername
This command will create a directory named foldername
in the current working directory.
Using Options
- Option
-v
(Verbose)This option prints a message for each directory created. It’s useful for tracking progress when creating multiple directories.
bashmkdir -v folder1 folder2 folder3
Output:
arduinomkdir: created directory 'folder1'
mkdir: created directory 'folder2'
mkdir: created directory 'folder3'
- Option
-p
(Parents)This option allows you to create nested directories, and it won’t throw an error if some directories already exist. It’s useful for creating a full path structure in one command.
bashmkdir -p -v parent/child/grandchild
Output:
arduinomkdir: created directory 'parent'
mkdir: created directory 'parent/child'
mkdir: created directory 'parent/child/grandchild'
- Option
-m
(Mode)Use this option to set specific permissions for the newly created directory. It works similarly to the
chmod
command.bashmkdir -m 700 securefolder
This sets the permissions of
securefolder
to read, write, and execute for the owner only. - Option
-Z
(SELinux Context)This option sets the SELinux security context for the directory. It is useful in environments where SELinux is enforced.
bashmkdir -Z contextfolder
Note: The default SELinux context is applied if you do not specify
-Z
or--context
. - Option
--help
Display a help message with a list of available options for
mkdir
.bashmkdir --help
- Option
--version
Show the version information of the
mkdir
command.bashmkdir --version
Output:
vbnetmkdir (GNU coreutils) 8.32
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Creating a Folder in Ubuntu
Ubuntu users can create directories using both the terminal and the graphical user interface (GUI). In the terminal, you can use the same mkdir
command as described. To create a folder via the GUI, right-click in the desired location, select “New Folder,” and enter the folder name.
Conclusion
The mkdir
command is a powerful tool for managing directories in Linux. By understanding and using its various options, you can efficiently create directories, set permissions, and manage file structures. Whether you are working on a Linux server or a local machine, mastering mkdir
will enhance your file management capabilities.