mkdir
The mkdir
command in Linux is a commonly used command in the command-line interface, and its primary function is to create new directories. The name mkdir
stands for "make directory". Understanding the usage of mkdir
is essential for file system organization and management in Linux. Here are the key aspects and functionalities of the mkdir
command:
Basic Directory Creation: To create a new directory, you simply use
mkdir
followed by the name of the directory you want to create. For example,mkdir new_directory
will create a directory namednew_directory
.Creating Multiple Directories:
mkdir
can create multiple directories at once. For instance,mkdir dir1 dir2 dir3
will create three new directories nameddir1
,dir2
, anddir3
.Creating Nested Directories: The
-p
(parents) option allows you to create a directory along with any necessary parent directories. For example,mkdir -p dir1/dir2/dir3
will createdir3
and also createdir1
anddir2
if they don't already exist. This is useful for setting up a nested directory structure in one command.Setting Permissions: When creating a directory, you can set its permissions using the
-m
option followed by the permission set. For example,mkdir -m 755 new_directory
creates a directory with read, write, and execute permissions for the owner, and read and execute permissions for the group and others.Checking for Errors: If
mkdir
encounters an error (like trying to create a directory that already exists), it will display an error message. This behavior can be suppressed using the-p
option, which will not show an error if the directory already exists.Verbose Mode: Using
mkdir
with the-v
(verbose) option will make it display a message for each directory it creates. This can be useful for debugging or confirming the actions of a script.Use in Scripts:
mkdir
is frequently used in shell scripts to ensure the required directory structure is in place for the script's operations.
Last updated