SoFunction
Updated on 2025-03-03

Interpretation of mkdir command syntax and instance in LINUX

In the dynamic world of Linux operating systems known for their absolute functionality and flexibility, there are a large collection of commands that will give you complete control of the system.

A deep understanding and mastery of the use of "mkdir commands" in Linux is an integral part of the Linux journey.

Syntax of MKDIR command

The mkdir command is the abbreviation of "make directory" and is an important part of the Unix/Linux command line interface. It enables users to create new directories or folders in their file system and is an indispensable tool for novice users and experienced Linux administrators.

In its simplest form, the mkdir command follows the following syntax:

mkdir [OPTION]... DIRECTORY...
  • mkdiris a command that instructs the system to create a new directory.
  • [OPTION]is an optional parameter to modify the behavior of a command.
  • DIRECTORYis the name of one or more directories to be created.

Options available to the mkdir command

There are several options to usemkdirUse the commands together to customize their functions:

  • -m, --mode=MODE: Sets the file mode (permissions) of the new directory, usually expressed as a numeric string, such as "755" or "644".
  • -p, --parents: This option ensures that no errors are reported if the specified directory already exists. It also allows the creation of parent directories as needed.
  • -v, --verbose: Provide detailed output, explanationmkdirWhat the command is doing.
  • ---help: Show the help message and exit.
  • ---version: Output version information and exit.

Learn more about MKDIR command examples

Create a simple directory with mkdir

The easiest and most common usage of the mkdir command is to create a new directory:

mkdir new_directory

In this command, new_directory is the name of the directory to be created. After executing this command, a new directory named new_directory is created at the current location.

Create multiple directories with mkdir

The mkdir command can also create multiple directories at the same time

Here is how to operate:

mkdir dir1 dir2 dir3

In this command, dir1 and dir3 are the names of the directory to be created.

dir2 This command will create these three directories at the current location.

Use mkdir to create a directory with specific permissions

Use the --m or --mode option to specify permissions for directories when creating them:

mkdir -m 755 new_directory

Here, the permissions of new_directory are set to "755", which means that the owner can read, write and execute, while groups and others can only read and execute.

Create nested directories with mkdir

The -p or ---parents option allows the creation of parent directories as needed.

For example:

mkdir -p dir1/dir2/dir3

This command checks whether dir1 and dir2 exist, and if they do not exist, they are created. It then creates dir3 in dir2 which internal dir2dir1.

Use detailed output with mkdir

The -v or ---verbose option makes mkdir print a message for each directory it creates, which is useful for troubleshooting or confirmation:

mkdir -v new_directory

This command creates new_directory and then prints a message indicating its creation.

Create a directory with spaces in the name

Sometimes, we want to create a directory with spaces in our name.

We can do this by enclosing the directory name in quotes:

mkdir "new directory"

Using this command, a new directory named new directory is created (there is a space between "new directory" and "directory").

Check the version of mkdir

You can check the version of the mkdir command currently installed on the system.

This may be helpful when compatibility issues occur or troubleshooting is occurring.

To check the version, you can use:

mkdir --version

This command displays the version information of the mkdir command currently used on the system.

Show mkdir help

IfIf you need help with the mkdir command or if you want to quickly check its options, use the ---help option:

mkdir --help

This command will output a help message.Show usage and available options of the mkdir command

Create a directory from a text file

Suppose there is a text file containing a list of directory names to be created.

CanUse the xargs command and mkdir to executeThis operation:

xargs mkdir < dir_list.txt

In this command,dir_list.txt is aA text file containing a list of directory names. This command will read this file and create a directory with these names.

Create a directory with a full path

You can specify the full path to create a directory in it.

This is especially useful when you want to create a directory outside of the current working directory:

mkdir /path/to/new_directory

HereIn the new_directory is in the specified path /path/to/Created.

Create a hidden directory

In Linux, any point(. The file or directory name starting with is considered hidden.

To create a hidden directory,use:

mkdir .hidden_directory

This command will create aA hidden directory named .hidden_directory. You can use ls -a to view this itemrecord.

Prevent existing directories from overwriting

The -p or --parents option can also be used to prevent the mkdir command from overwriting existing directories:

mkdir -p existing_directory

Use this command,If existing_directory already exists, it will not be overwritten, thus preventing any potential data loss.

Create a directory and set a group ID

g option allows setting new directoryGroup ID:

mkdir -m g=group_name new_directory

This commandCreate new_directory and set its group ID to group_name.

Create a directory and make it a temporary directory

The t option allows theThe directory is set as a temporary directory, and Linux processes this directory slightly differently than the standard directory:

mkdir -m t new_directory

This command willCreate new_directory as a temporary directory.

Files created in temporary directories are usuallyIt is deleted when it is inaccessible for a period of time.

Create multi-level nested directories

The -p option can be usedCreate multi-level nested directories with a single command:

mkdir -p Level1/Level2/Level3/Level4

This command will create four levels of directories.

Level1 is the top-level directory, Level4 is the underlying directory, and Level2 and Level1 are the intermediate directories. Level3

Create directories with different permissions for users, groups, and others

-m option allowsMay specify different permissions for users, groups and others:

mkdir -m u=rwx,g=rx,o= new_directory

Here, new_directory has user'sRead, write, and execute permissions (u), group read and execute permissions (g), and others (o).

Create directory in verbose mode and ignore errors

The -v option can be used in conjunction with -p to ignore errors and provideDetailed output:

mkdir -pv existing_directory

In this command, ifThe existing_directory already exists, then the mkdir command will not return an error, instead provide detailed output.

Create a directory without detailed mode

If you want to create a directory silently without any verbose output, just makeUse the mkdir command without using the -v option:

mkdir quiet_directory

This command creates a name called quiet_directory directory without displaying any messages.

Use braces extensions with mkdir commands

Braces extension is a powerful feature in Linux that canTogether with the mkdir commandUse to create a series of directories:

mkdir dir{1..5}

This command will create five directories named dir1 and dir2, dir3dir4 and dir5 dir1dir2.

Create directories and set sticky bits

The sticky bit is the permission bit that protects files in the directory.

If set, the file can only be deleted or renamed by the owner or root user.

Here is how to set sticky bits when creating a directory:

mkdir -m +t new_directory

Using this command, the sticky bit set will be created usingCreate new_directory

Create a directory and set the Setuid and setgid bits

The setuid and setgid bits can be set when creating a directory, which affects ownership of files and directories created there.

The -m option allows the following settingsBit:

mkdir -m u+s,g+s new_directory

This command will useThe setuid and setgid bits create new_directory.

Create directories with different permissions using octal mode

Although symbolic modes for setting permissions have been discussed,But the mkdir command is also supportedHold octal mode:

mkdir -m 700 private_directory

existHere, private_directoryPermission is set to "700" (user reads, writes, and executes, no permissions for group and others).

Create a directory and make it immutable

Making the directory immutable prevents it from being deleted, even for root users.

First create a directory.Then use the chattr command to make itImmutable:

mkdir new_directory
sudo chattr +i new_directory

This will createCreate new_directory,Then make it immutable.

Create a directory and set the default ACL

If the file system supports access control lists (ACL), you can set the default ACL when creating a directory:

mkdir new_directory
setfacl -d -m g::rwx new_directory

Among these commands, firstCreate a new_directory, and the setfacl command sets the default ACL of the directory to rwx of the group.

Create a directory and set up a SELinux context

If running on a SELinux-enabled system, you can set the SELinux context when creating a directory:

mkdir new_directory
chcon -t httpd_sys_content_t new_directory

Here, firstCreate new_directory. The chcon command then changes the SELinux context of new_directory to httpd_sys_content_t.

in conclusion

In LinuxAn extensive exploration of mkdir commands illustrates its power and versatility.

Whether it is creating a single directory, nesting multiple directories, or tuning permissions and properties, the mkdir command remains an indispensable tool in every Linux user toolkit.

Knowing these examples not only improves proficiency in managing directories, but also increases overall command-line flexibility. Continue to use the mkdir command to simplifyand enhance the Linux experience.

The above is personal experience. I hope you can give you a reference and I hope you can support me more.