Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of using mkdir command to create directory in linux

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

On Linux systems, users can create new directories from the command line or from the desktop file manager. The command to create a directory (also known as a folder) in Linux is the mkdir command. The following article will introduce the basics of the mkdir command, through examples to let you understand the use of the mkdir command, I hope it will be helpful.

Linux mkdir command syntax

The syntax of the Linux mkdir command is:

Mkdir [OPTION] [DIRECTORY]

This command can take one or more directory names as its parameters.

Note: to create a directory using this mkdir command, you need to have write permission to the parent directory. Otherwise, you will receive a Permission denied error.

How does linux create a new directory using the mkdir command?

To create a directory using the mkdir command in Linux, simply pass the directory name as an argument to the mkdir command:

Mkdir newdir

We can use the ls command to verify that the directory has been created by listing the contents:

Ls-l

Output

Drwxrwxr-x 2 username username 4096 Jan 20 03:39 newdir

If you want the mkdir command to print messages for the directory you created, you can pass the-v (--verbose) option.

When only the directory name is provided, it is created in the current working directory (the current working directory is the directory where the command is run). You can use the pwd command to display the location of the current working directory. To change the current working directory, use the cd (change directory) command.

To create a directory in another location, the user needs to provide an absolute or relative file path to the parent directory. For example, to create a new directory in the / tmp directory, type:

Mkdir / tmp/newdir

If a user attempts to create a directory in the parent directory where the user does not have sufficient permissions, he or she will receive a Permission denied error:

Mkdir / root/newdir

Output:

Mkdir: cannot create directory'/ root/newdir': Permission denied

How does linux create a parent directory using the mkdir command?

The parent directory is the directory above another directory in the directory tree. To create a parent directory, you need to use the-p option.

Suppose you want to create a / home/linuxize/Music/Rock/Gothic directory. If any of these parent directories do not exist, you will receive an error as follows:

Mkdir / home/linuxize/Music/Rock/Gothic

Output:

Mkdir: cannot create directory'/ home/linuxize/Music/Rock/Gothic': No such file or directory

At this point we can use the-p option to create a parent directory instead of creating all the missing parent directories one by one:

Mkdir-p / home/linuxize/Music/Rock/Gothic

When using the-p option, the mkdir command creates a directory only if it does not exist.

How does linux use the mkdir command to set permissions when creating a directory?

To set permissions when creating a directory, you need to use the-m (- mode) option. The syntax for passing permissions is the same as for the chmod command.

In the following example, only the user who created it can access the new directory:

Mkdir-m 700 newdir

If the-m option is not passed to mdkir, the newly created directory will have 775 or 755 permissions, depending on the umask value.

How does linux create multiple directories using the mkdir command?

To create more than one directory at a time, you need to specify the name of the directory as a parameter, separated by spaces:

Mkdir dir1 dir2 dir3

The mkdir command also allows users to create complex directory trees with a single command:

Mkdir-p Music/ {Jazz/Blues,Folk,Disco,Rock/ {Gothic,Punk,Progressive}, Classical/Baroque/Early}

The above command creates the following directory tree:

Music/ |-- Classical | `--Baroque |`-- Early |-- Disco |-- Folk |-- Jazz | `--Rock |-- Gothic |-- Progressive`-- Punk

These are the details of how to create a directory using the mkdir command in linux. For more information, please follow other related articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report