×

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

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.

How to change the ApacheHTTP port in Linux

What is an Apache HTTP server in Linux? When we talk about the most commonly used web servers, one of the web servers that cannot be missed is Apache. With the...

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.

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.

du Command in Linux/Unix with Examples

The "du" command in Linux (and other Unix-like operating systems) is used to estimate the space used by a file or directory. The du command stands for "disk usage," and...

4 minutes read.

pwd Command in Linux/Unix with Examples

The "pwd" command in Linux (and other Unix-like operating systems) stands for "print working directory." When executed, it simply displays the current directory (or folder) that the user is currently...

3 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.

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.

Linux Admin Tutorial

In publishing and graphic design, lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document without relying on meaningful content....

1 minute read.

cp Command in Linux/Unix with Examples

The cp (copy) command in Linux is a command-line utility that is used to copy files and directories. It is one of the most basic and commonly used commands in...

5 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.

Difference Between Unix and Linux

Unix vs Linux Unix and Linux both have been of great use in changing the world of operating systems. Unix was among the very few operating systems which were programmed for...

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.

Linux mkdir | Linux Create Directory

The Linux mkdir command is very much similar to Windows’s "new folder" feature. This command is used in Linux os to create new directories. The command mkdir stands for "make...

1 minute read.

Linux apt-get command

Apt-get is a command-line that helps to handle the packages in Linux. Its major task is to assist in retrieving data and packages from trusted sources for purposes of installation,...

6 minutes read.

addgroup Command in Linux with Examples

What is the addgroup command? It is a command in Linux to create a new group using specified command lined and the system’s default values. How to add groups Adding a user group:...

4 minutes read.

ssh Command in Linux/Unix with Examples

The ssh (Secure Shell) command in Linux is used to remotely access and control another computer or device over a network. It provides a secure and encrypted connection between the...

6 minutes read.

addr2line Command in Linux with Examples

Use of addr2line Command The addr2line command is used to convert addresses to line numbers and file names. This command uses debugging info to check which line number and file name...

4 minutes read.

top Command in Linux/Unix with Examples

The 'top' command in Linux is a real-time system monitoring tool that displays information about the running processes and system resource usage. The command provides a dynamic, scrolling view of...

4 minutes read.

grep Command in Linux/Unix with Examples

The grep command in Linux is a command-line utility that searches for patterns in text files. It stands for "global regular expression print" and it is used to search for...

5 minutes read.