×

mkdir Command in Linux/Unix with Examples

The "mkdir" command in Linux (and other Unix-like operating systems) is used to create new directories. The "mkdir" command stands for "make directory" and it is used to create a new directory with the specified name. The basic syntax of the "mkdir" command is:

mkdir [options] directory_name

When executed without any options, the "mkdir" command will create a new directory with the specified name in the current working directory. For example, the following command will create a new directory named "example":

mkdir example

The "mkdir" command also has several options that can be used to control how the new directory is created. The most commonly used options are:

-p : creates any necessary parent directories along the path specified. For example, the following command will create a new directory named "example" and any necessary parent directories along the path:

mkdir -p /path/to/example

-m : sets the file mode (permissions) for the newly created directory. For example, the following command will create a new directory named "example" with permissions set to 755:

mkdir -m 755 example

-v : displays verbose output, showing each directory as it is created. For example, the following command will create a new directory named "example" and display verbose output:

mkdir -v example

-Z : sets the SELinux context for the newly created directory. This option is used on systems with SELinux enabled. For example, the following command will create a new directory named "example" and set the SELinux context to "user_u:object_r:var_t":

mkdir -Z user_u:object_r:var_t example

The "mkdir" command can also be used in shell scripts and automation tasks to create new directories as needed. For example, a script may use the "mkdir" command to create a new directory to store backups, or to create a new directory for a new user account.

The "mkdir" command is a fundamental command for creating directories in Linux and other Unix-like operating systems. Here are some additional features and uses of the "mkdir" command:

  • The "mkdir" command can also be used to create multiple directories at once. You can specify multiple directory names as arguments to the command and it will create all of them. For example, the following command will create the directories "example1" and "example2" in the current working directory:
    • mkdir example1 example2
  • The "mkdir" command can also be used in combination with other commands to create directories dynamically. For example, the following command will create a new directory with the name "backup-<current-date>" in the current working directory, where <current-date> is the current date in the format YYYY-MM-DD:
    • mkdir backup-$(date +%F)
  • The "mkdir" command can also be used in shell scripts and automation tasks to create directories with specific naming conventions. For example, a script may use the "mkdir" command to create new directories with names in the format "backup-<current-date>" to store backups, or to create new directories with names in the format "user-<username>" to store files for specific users.
  • The "mkdir" command also provides the ability to set the ownership and group of the new directory by using the option -o, and -g. This can be useful when running scripts as a non-root user and creating directories with specific permissions.

In summary, the "mkdir" command is a powerful command that can be used to create directories in Linux and other Unix-like operating systems. It can be used to create multiple directories at once, create directories dynamically, and create directories with specific naming conventions. It can also be used in combination with other commands and in shell scripts and automation tasks. It also provides the ability to set ownership and group of the directory, which can be useful when running scripts as a non-root user and creating directories with specific permissions.


Related Topics

ping Command in Linux/Unix with Examples

The "ping" command in Linux (and other Unix-like operating systems) is used to test the connectivity between two networked devices. The "ping" command sends a small packet of data, called...

4 minutes read.

gzip Command in Linux/Unix with Examples

The gzip command in Linux is used for compressing and decompressing files. It uses the Lempel-Ziv coding (LZ77) algorithm to compress files, which is known for its high compression ratio...

6 minutes read.

unzip Command in Linux/Unix with Examples

The 'unzip' command in Linux is used to extract files from a ZIP archive file. The command allows you to decompress and extract the files and directories stored in a...

3 minutes read.

Linux Path Completion

Every operating system has some features which make them special. The Linux operating system primarily works on the command line interface, and we do have many features for this CLI....

1 minute read.

ls Command in Linux/Unix with Examples

The ls command in Linux is a command-line utility that is used to list the files and directories in a directory. It is one of the most basic and commonly...

4 minutes read.

Linux Commands with Examples

Linux Commands with Examples All the Linux commands are executed on the terminal provided by the Linux system. This terminal is a command-line interface to implement and display command outputs. Linux...

10 minutes read.

Linux Absolute and Relative Path

The concept of the absolute and relative path is very simple. The names “relative path” and “absolute path” give us little understanding of the concept we are learning here. The...

1 minute read.

scp Command in Linux/Unix with Examples

The scp (secure copy) command in Linux is a command-line utility that is used to securely transfer files between two hosts over a network. It is similar to the cp...

4 minutes read.

cat Command in Linux/Unix with Examples

The cat command in Linux is a command-line utility that is used to display the contents of a text file. It stands for "concatenate" and it is used to view...

4 minutes read.

cut Command in Linux/Unix with Examples

The "cut" command in Linux (and other Unix-like operating systems) is used to remove sections from each line of a file or stream of data. The cut command is often...

3 minutes read.

apt-get Command in Linux with Examples

The apt-get command is a command-line tool that is used to handle packages in Linux. This command handles the processes like retrieving the packages and information from authentic sources for...

5 minutes read.

adduser Command in Linux with Examples

What is the adduser command? The adduser command creates a new user, and this command may be used to add apre-existing user to a group that already exists. How to use the...

4 minutes read.

Difference Between Linux and Windows

Linux vs. Windows Here in this tutorial, we are going to discuss several features of Linux and Windows. These features or characteristics help us to come to a conclusion about which...

5 minutes read.

Linux Distributions

Linux Distributions Linux OS is not like other operating systems. Linux can be modified according to the user’s requirements, and it is very flexible to any change. Linux distributions are often...

4 minutes read.

sudo Command in Linux/Unix with Examples

The sudo command in Linux is used to run a command or set of commands with the privileges of another user, typically the root user. This allows users to perform...

3 minutes read.

rm Command in Linux/Unix with Examples

The "rm" command in Linux (and other Unix-like operating systems) is used to remove files and directories. The "rm" command stands for "remove" and it is used to delete files...

4 minutes read.

chown Command in Linux/Unix with Examples

The 'chown' command in Linux is used to change the ownership of a file or directory. The command stands for "change owner" and it allows you to change the user...

3 minutes read.

chmod Command in Linux/Unix with Examples

The chmod command in Linux is a command-line utility that is used to change the permissions of files and directories. It stands for "change mode" and it is used to...

4 minutes read.

Linux rmdir command

The default use case of the rmdir command in the Linux system is to delete an empty directory. The below screenshot shows how to delete the empty directory. First, we...

1 minute read.

Linux Bash

Linux Bash In this tutorial, we are going to learn what the shell is? And how many various shells we have, we have also covered most of the bash shell features....

4 minutes read.