×

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

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.

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.

Linux cd

Linux cd: As in GUI operating systems like Windows 10, we use the mouse to travel to various folders back and forth; similarly, we have the “cd” command in Linux...

4 minutes read.

tail Command in Linux/Unix with Examples

The "tail" command in Linux (and other Unix-like operating systems) is used to display the last few lines of a file. By default, the tail command will display the last...

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

man Command in Linux/Unix with Examples

The man command in Linux is used to display the manual pages for a given command or system call. These manual pages provide detailed information on the command's usage, options,...

3 minutes read.

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.

telnet Command in Linux/Unix with Examples

The telnet command in Linux is a command-line utility that allows you to connect to a remote host using the Telnet protocol. Telnet is a network protocol that provides a...

3 minutes read.

sed Command in Linux/Unix with Examples

The 'sed' command in Linux is a powerful text processing tool that allows you to perform various operations on text files, such as searching, replacing, and manipulating text. The command...

3 minutes read.

curl Command in Linux/Unix with Examples

The 'curl' command in Linux is a command-line tool that allows you to transfer data from or to a server using various protocols, including HTTP, HTTPS, FTP, and many more....

3 minutes read.

rename Command in Linux/Unix with Examples

The rename command in Linux is used to rename multiple files at once, using a specified pattern. The command is also known as rename, ren, and mv. This command is...

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.

awk Command in Linux/Unix with Examples

The 'awk' command in Linux is a powerful text processing tool that allows you to perform various operations on text files, such as searching, replacing, and manipulating text. The command...

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.

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.

df Command in Linux/Unix with Examples

The 'df' command in Linux is used to display the amount of disk space available on the file system. The command reports the amount of disk space used and available...

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

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.

mv Command in Linux/Unix with Examples

The 'mv' command in Linux is used to move or rename files and directories. The command stands for "move" and it allows you to move files and directories from one...

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