【Linux】1 创建目录:mkdir

时间:2020-05-09
本文章向大家介绍【Linux】1 创建目录:mkdir,主要包括【Linux】1 创建目录:mkdir使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

mkdir命令用于创建目录,如同一路径下创建单个或多个目录、递归创建目录,但同路径下不能创建同名目录,且目录名区分大小写。

【命令】

mkdir

【用途】

  创建目录(单个目录/多个目录)

【语法】

mkdir [选项]...目录名...

【示例】

  切换到当前目录:/usr/local/linuxStudy,所有示例在此路径下操作。

[root@testserver linuxStudy]# pwd
/usr/local/linuxStudy

例1.创建单个目录dir1

[root@testserver linuxStudy]# mkdir dir1
[root@testserver linuxStudy]# ls
dir1

例2.一次创建多个目录:dir2,dir3

[root@testserver linuxStudy]# mkdir dir2 dir3
[root@testserver linuxStudy]# ls
dir1  dir2  dir3

例3.同路径下创建同名目录:dir1-->创建失败,同路径下不能创建同名目录

[root@testserver linuxStudy]# mkdir dir1
mkdir: cannot create directory `dir1': File exists

例4.-p参数,创建多层目录dir4/dir5(dir4目录不存在时,同时创建dir4、dir5目录;dir4目录存在时,则只创建dir5目录)

[root@testserver linuxStudy]# mkdir dir4/dir5                           #未加-p参数,上层目录不存在时,创建目录失败
mkdir: cannot create directory `dir4/dir5': No such file or directory
[root@testserver linuxStudy]# mkdir -p dir4/dir5                        #-p:上层目录不存在时,同步创建
[root@testserver linuxStudy]# ls -R                                     #-R:递归列出当前目录下所有的目录、文件
.:
dir1  dir2  dir3  dir4

./dir1:

./dir2:

./dir3:

./dir4:
dir5

./dir4/dir5:
[root@testserver linuxStudy]# 

例5:-v参数,对于每个创建的目录,打印一条信息

[root@testserver linuxStudy]# mkdir -v dir6
mkdir: created directory `dir6'
[root@testserver linuxStudy]# mkdir -v dir7 dir8
mkdir: created directory `dir7'
mkdir: created directory `dir8'

例6.-m参数,创建目录的同时设置文件权限(同chmod命令)

[root@testserver linuxStudy]# mkdir -m o-rw dir10  #创建dir10目录,other用户去掉rw权限
[root@testserver linuxStudy]# ll
total 36
drwxr-xr-x 2 root root 4096 May  9 14:47 dir1
drwxrwx--x 2 root root 4096 May  9 15:08 dir10
drwxr-xr-x 2 root root 4096 May  9 14:47 dir2
drwxr-xr-x 2 root root 4096 May  9 14:47 dir3
drwxr-xr-x 3 root root 4096 May  9 14:52 dir4
drwxr-xr-x 2 root root 4096 May  9 15:02 dir6
drwxr-xr-x 2 root root 4096 May  9 15:02 dir7
drwxr-xr-x 2 root root 4096 May  9 15:02 dir8
drwxrwxrwx 2 root root 4096 May  9 15:07 dir9
[root@testserver linuxStudy]# mkdir -m 511 dir11  #创建dir11目录,设置user、group、other用户权限分别为5、1、1(读权限4,写权限2,执行权限1,用户具备多种权限时值相加)
[root@testserver linuxStudy]# ll
total 40
drwxr-xr-x 2 root root 4096 May  9 14:47 dir1
drwxrwx--x 2 root root 4096 May  9 15:08 dir10
dr-x--x--x 2 root root 4096 May  9 15:09 dir11
drwxr-xr-x 2 root root 4096 May  9 14:47 dir2
drwxr-xr-x 2 root root 4096 May  9 14:47 dir3
drwxr-xr-x 3 root root 4096 May  9 14:52 dir4
drwxr-xr-x 2 root root 4096 May  9 15:02 dir6
drwxr-xr-x 2 root root 4096 May  9 15:02 dir7
drwxr-xr-x 2 root root 4096 May  9 15:02 dir8
drwxrwxrwx 2 root root 4096 May  9 15:07 dir9

【帮助文档】

Linux环境下输入 man mkdir,查看find命令的帮助文档(ps:英文渣渣咬咬牙啃一啃帮助文档。不要偷懒,多看官方文档。注释部分为个人添加。)

[root@testserver local]# man mkdir
MKDIR(1)                         User Commands                        MKDIR(1)

NAME
       mkdir - make directories   #创建目录

SYNOPSIS
       mkdir [OPTION]... DIRECTORY...

DESCRIPTION
       Create the DIRECTORY(ies), if they do not already exist. #目录已存在时,创建目录失败

       Mandatory arguments to long options are mandatory for short options too.

       -m, --mode=MODE
              set file mode (as in chmod), not a=rwx - umask #设置文件权限,而不是默认权限a=rwx

       -p, --parents
              no error if existing, make parent directories as needed  #父目录不存在时,创建所需的父目录

       -v, --verbose
              print a message for each created directory #对每一个创建的目录打印1条信息

       -Z, --context=CTX
              set the SELinux security context of each created directory to CTX

       --help display this help and exit

       --version
              output version information and exit

AUTHOR
       Written by David MacKenzie.

REPORTING BUGS
       Report mkdir bugs to bug-coreutils@gnu.org
       GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
       General help using GNU software: <http://www.gnu.org/gethelp/>
       Report mkdir translation bugs to <http://translationproject.org/team/>

COPYRIGHT
       Copyright   ©   2010   Free   Software   Foundation,   Inc.   License  GPLv3+:  GNU  GPL  version  3  or  later
       <http://gnu.org/licenses/gpl.html>.
       This is free software: you are free to change and redistribute it.  There is NO WARRANTY, to the extent permit-
       ted by law.

SEE ALSO
       mkdir(2)

       The  full  documentation for mkdir is maintained as a Texinfo manual.  If the info and mkdir programs are prop-
       erly installed at your site, the command

              info coreutils 'mkdir invocation'

       should give you access to the complete manual.

GNU coreutils 8.4                November 2013                        MKDIR(1)
(END) 

【写在末尾】

文章如有错误之处,欢迎评论指正。

有兴趣可关注同名微信公众号“粒粒的测试笔记”,号内会分享各种测试相关知识,感谢关注。

原文地址:https://www.cnblogs.com/yllil/p/12857385.html