【Linux】創建目錄: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,查看mkdir命令的幫助文檔(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) 

 

【寫在末尾】

微信公眾號“粒粒的測試筆記


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM