How to Create Directories in Linux (mkdir Command)
Introduction
Before we begin talking about how to create directories in Linux, let's briefly understand - What is Linux?
Linux is a free and open-source operating system kernel that serves as the foundation for numerous operating systems, known as Linux distributions. Creating directories in Linux is a fundamental task that allows users to organize and structure their file system.
You may create new folders in Linux systems using either the command line or the file manager on your desktop. mkdir
is a command that lets you create directories (also known as folders).
This tutorial will walk you through the process of creating directories in Linux, providing you with the necessary knowledge to efficiently manage your files and directories. We will also address a few FAQs on mkdir command in Linux.
Linux mkdir Command Syntax
Below is the syntax of mkdir
command:
mkdir [OPTION] [DIRECTORY]
As parameters, the command accepts one or more directory names.
How to Create a New Directory
To create a directory in Linux, use the mkdir
command with the directory's name as an input. To create a new directory named vega
, for example, run the following command:
mkdir vega
You can confirm that the directory was created by listing its contents using the ls
command:
ls -l
Output
drwxrwxr-x 2 username username 4096 March 3 03:39 vega
It is produced in the current working directory when only the directory name is provided without the full path.
The current working directory is the location where the commands are being executed. The cd
command is used to change the current working directory.
To create a new directory in a different location, you'll need to enter the absolute or relative file path of the parent directory. To create a new directory in the /tmp
directory, for example, type:
mkdir /tmp/vega
You'll get a Permission denied
error if you try to create a directory in a parent directory where the user doesn't have enough permissions.
mkdir /root/vega
Output
mkdir: cannot create directory '/root/vega': Permission denied
The -v
(--verbose
) flag tells mkdir
to print a message for each directory it creates.
How to Create Parent Directories
A parent directory is a directory in the directory tree that is above another directory. Use the -p
option to create parent directories.
Let's imagine you wish to make a /home/vega/Music/Rock/Gothic
directory:
mkdir /home/vega/Music/Rock/Gothic
If any of the parent directories are missing, you will receive the following error:
Output
mkdir: cannot create directory '/home/vega/Music/Rock/Gothic': No such file or directory
Instead of manually constructing the missing parent directories, use the mkdir
command with the -p
option to create them all at once:
mkdir -p /home/vega/Music/Rock/Gothic
When you use the -p
option, the command only creates the directory if it doesn't already exist.
If you don't use the -p
option and try to create a directory that already exists, mkdir
will produce File exists
error:
mkdir vega
mkdir: cannot create directory 'vega': File exists
Set Permissions when Creating a Directory
Invoke the mkdir
command with the -m
(-mode
) option to create a directory with certain permissions. Permissions are assigned using the same syntax as the chmod
command.
We'll create a new directory with 700
rights in the following example, which indicates that only the user who created the directory will be able to view it:
mkdir -m 700 newdir
When the -m
option is not used, the umask
value determines whether the newly created folders have 775
or 755
permissions.
How to Create Multiple Directories
To create several directories, use the command parameters to specify the directories' names, separated by a space:
mkdir dir1 dir2 dir3
You may also use the mkdir
command to create a complicated directory tree with only one command:
mkdir -p Music/{Jazz/Blues,Folk,Disco,Rock/{Gothic,Punk,Progressive},Classical/Baroque/Early}
The following directory tree is created by the command above:
Output
Music/
|-- Classical
| `-- Baroque
| `-- Early
|-- Disco
|-- Folk
|-- Jazz
| `-- Blues
`-- Rock
|-- Gothic
|-- Progressive
`-- Punk
Frequently Asked Questions - Creating Directories in Linux
Can I create multiple directories at once?
Yes, you can create multiple directories simultaneously by providing multiple directory names as arguments to the mkdir
command.
How do I create directories with specific permissions?
You can specify directory permissions during the creation process using the mkdir
command with appropriate permission options, such as chmod
.
How do I create a hidden directory in Linux?
To create a hidden directory in Linux, prefix the directory name with a dot (.), such as .hidden_directory
.
Can I create multiple levels of nested directories at once?
Yes, you can create multiple levels of nested directories at once by specifying the complete directory path using forward slashes (/) to separate each level.
How do I create a directory with read-only permissions?
To create a directory with read-only permissions, you can set the appropriate permissions using the chmod
command after creating the directory.
Is it possible to create directories with a specific size?
No, directories do not have an explicit size in Linux. Their size is negligible and depends on the number and size of files and subdirectories they contain.
How do I create a temporary directory in Linux?
To create a temporary directory in Linux, you can use the mktemp
command, which automatically generates a unique directory name.
Conclusion
We hope this comprehensive explanation has helped you understand how to create folders in Linux. In Linux, creating directories is a critical activity for organizing and organising the file system. Users can simply create directories and nested directories by following the easy method of using the mkdir command.
This enables efficient file management and categorization, making it easier to find and access data within the Linux operating system. If you have any queries, please leave a comment below and we’ll be happy to respond to them.
If you have any queries, please leave a comment below and we’ll be happy to respond to them.