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

How to understand the mkdir command of Linux system

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

The content of this article mainly focuses on how to understand the mkdir command of the Linux system. The content of the article is clear and clear. It is very suitable for beginners to learn and is worth reading. Interested friends can follow the editor to read together. I hope you can get something through this article!

The mkdir command in the Linux system creates the specified directory name, requiring the user who created the directory to have write permission in the current directory, and the specified directory name cannot be an existing directory in the current directory.

The syntax mkdir [- p] dirName parameter states:-p ensures that the directory name exists, and build one if it doesn't exist.

Case demonstration:-p option if the test directory does not exist, it will be created, otherwise it will be prompted to fail, as follows

~ $mkdir test mkdir: unable to create directory "test": file already exists 12 failed to create at the command prompt, but there is no effect, but there is an impact in the shell script. The exit status of this command is 1, indicating that the execution failed, which will affect the logic behind the script. So is there any way to execute the mkdir command without reporting an error when the directory already exists? Use the-p option

~ $ls-d test test ~ $mkdir-p test ~ $echo $? 12345 you can see that when the test directory exists, if you create a test directory with the-p option, the exit status is 0, which means the command was executed successfully, which is very useful in shell scripts.

The-p option actually has another function, which is to create multi-level directories.

~ $ls test/ hello.c world.c ~ $mkdir test/dir1/dir2 mkdir: unable to create directory "test/dir1/dir2": without that file or directory ~ $mkdir-p test/dir1/dir2 ~ $tree test test ├── dir1 │ └── dir2 ├── hello.c └── world.c 2 directories, 2 files 12345678910111213 you can see that if you do not use the-p option, you cannot create a multi-level directory.

Specify permissions to create directories. The-m option allows you to specify permissions to create directories, such as

~ $mkdir-m 744 hello ~ $ls-ld hello drwxr--r-- 2 david david 4096 September 29 15:08 hello 123 We can find that the permissions of the hello directory correspond to 744 and are not affected by the umask value.

This is just creating a first-level directory, what if you create a multi-level directory?

~ $umask 0022 ~ $mkdir-p-m 744 hello/world/david ~ $$ls-ld hello/world/ drwxr-xr-x 3 umi umi 4096 September 29 15:16 hello/world/ ~ $ls-ld hello/world/david/ drwxr--r-- 2 umi umi 4096 September 29 15:16 hello/world/david/ 12345678910 creates two directories world and david through mkdir-p-m 744 hello/world/david, but only the permissions of david directory are specified, while world directory is 755 This is because the world directory is affected by umaks.

So if a permission is specified when creating a multi-level directory, this permission is assigned to the deepest level of the directory, while the permission of the parent directory is in the way of the system (affected by umask).

Thank you for your reading. I believe you have some understanding of "how to understand mkdir commands in Linux system". Go to practice quickly. If you want to know more about it, you can follow the website! The editor will continue to bring you better 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

Development

Wechat

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

12
Report