歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> 每天一個Linux命令(3)mkdir命令

每天一個Linux命令(3)mkdir命令

日期:2017/3/3 14:04:56   编辑:Linux技術

[b] mkdir命令用來創建目錄。 [/b] [b] (1)用法:[/b]

[b]用法: mkdir [選項]... 目錄...[/b] [b] (2)功能:[/b]

[b] 功能: 若指定目錄不存在則創建目錄[/b]該命令創建由dirname命名的目錄。如果在目錄名的前面沒有加任何路徑名,則在當前目錄下創建由dirname指定的目錄;如果給出了一個已經存在的路徑,將會在該目錄下創建一個指定的目錄。在創建目錄時,應保證新建的目錄與它所在目錄下的文件沒有重名。

[b] (3)選項參數[/b] 1)-Z: 設置安全上下文,當使用SELinux時有效

2) -m:<目標屬性>或-mode<目標屬性> 建立目錄的同時設置目錄的權限 3) -p或--parents 若所要建立目錄的上層目錄目前尚未建立,則會一並建立上層目錄

4) -version 顯示版本信息 [b] (4)實例[/b]

1)[sunjimeng@localhost Documents]$ mkdir mainDir 在當前文件夾下創建一個新文件

[sunjimeng@localhost Documents]$ ll            //列出當前目錄下的文件及文件夾
total 0
[sunjimeng@localhost Documents]$ mkdir mainDir //新建一個文件
[sunjimeng@localhost Documents]$ ll            //查看
total 0
drwxrwxr-x. 2 sunjimeng sunjimeng 6 May  1 02:49 mainDir

2)[sunjimeng@localhost Documents]$ mkdir -v secondDir 在當期那文件夾下下創建一個新文件,並輸出提示信息

[sunjimeng@localhost Documents]$ mkdir -v secondDir
mkdir: created directory ‘secondDir’
[sunjimeng@localhost Documents]$

3)[sunjimeng@localhost Documents]$ mkdir -p thirdDir/{test1,test2,test3} 在當前文件夾下創建一個新文夾,而且包含多個子文件夾

[sunjimeng@localhost Documents]$ mkdir -p thirdDir/{test1,test2,test3}  //新建包含多個子文件夾的文件夾
[sunjimeng@localhost Documents]$ ll   //查看當前工作目錄下有的文件及文件夾,以詳細信息輸出
total 0
drwxrwxr-x. 2 sunjimeng sunjimeng  6 May  1 02:49 mainDir
drwxrwxr-x. 2 sunjimeng sunjimeng  6 May  1 02:52 secondDir
drwxrwxr-x. 5 sunjimeng sunjimeng 42 May  1 02:57 thirdDir
[sunjimeng@localhost Documents]$ cd thirdDir  //進入目錄
[sunjimeng@localhost thirdDir]$ ll    //查看目錄下的子目錄
total 0
drwxrwxr-x. 2 sunjimeng sunjimeng 6 May  1 02:57 test1
drwxrwxr-x. 2 sunjimeng sunjimeng 6 May  1 02:57 test2
drwxrwxr-x. 2 sunjimeng sunjimeng 6 May  1 02:57 test3
[sunjimeng@localhost thirdDir]$

[sunjimeng@localhost Documents]$ ll                                 //查看Document文件夾下的文件及文件夾
total 0
drwxrwxr-x. 2 sunjimeng sunjimeng  6 May  1 02:49 mainDir
drwxrwxr-x. 2 sunjimeng sunjimeng  6 May  1 02:52 secondDir
drwxrwxr-x. 5 sunjimeng sunjimeng 42 May  1 03:01 thirdDir
[sunjimeng@localhost Documents]$ mkdir thirdDir/{test4,test5,test6}//雖然已經存在了thirdDir文件夾,但絲毫不影響這個操作
[sunjimeng@localhost Documents]$ ll                      
total 0
drwxrwxr-x. 2 sunjimeng sunjimeng  6 May  1 02:49 mainDir
drwxrwxr-x. 2 sunjimeng sunjimeng  6 May  1 02:52 secondDir
drwxrwxr-x. 8 sunjimeng sunjimeng 78 May  1 03:01 thirdDir
[sunjimeng@localhost Documents]$ cd thirdDir
[sunjimeng@localhost thirdDir]$ ll                                //因為新建的重名文件夾下的子文件夾集將可以添加到已有重名文件夾下
total 0
drwxrwxr-x. 2 sunjimeng sunjimeng 6 May  1 02:57 test1
drwxrwxr-x. 2 sunjimeng sunjimeng 6 May  1 02:57 test2
drwxrwxr-x. 2 sunjimeng sunjimeng 6 May  1 02:57 test3
drwxrwxr-x. 2 sunjimeng sunjimeng 6 May  1 03:01 test4
drwxrwxr-x. 2 sunjimeng sunjimeng 6 May  1 03:01 test5
drwxrwxr-x. 2 sunjimeng sunjimeng 6 May  1 03:01 test6
4)[sunjimeng@localhost Documents]$ mkdir -m 700 /home/sunjimeng/Document 在指定路徑下創建文件夾,並且只有文件主有讀、寫和執行權限,其他人無權訪問。

[sunjimeng@localhost Documents]$ mkdir -m 700 /home/sunjimeng/Document
[sunjimeng@localhost Documents]$ cd /home/sunjimeng
[sunjimeng@localhost ~]$ ll
total 0
drwxr-xr-x. 2 sunjimeng sunjimeng  6 May  1 01:23 Desktop
drwx------. 2 sunjimeng sunjimeng  6 May  1 03:07 Document     //這一項即為所新建的文件夾
drwxr-xr-x. 5 sunjimeng sunjimeng 51 May  1 02:57 Documents
drwxr-xr-x. 2 sunjimeng sunjimeng  6 May  1 01:23 Downloads
drwxr-xr-x. 2 sunjimeng sunjimeng  6 May  1 01:23 Music
drwxr-xr-x. 2 sunjimeng sunjimeng  6 May  1 01:23 Pictures
drwxr-xr-x. 2 sunjimeng sunjimeng  6 May  1 01:23 Public
drwxr-xr-x. 2 sunjimeng sunjimeng  6 May  1 01:23 Templates
drwxr-xr-x. 2 sunjimeng sunjimeng  6 May  1 01:23 Videos
5)[sunjimeng@localhost Document]$ mkdir -pm 750 bin/os_1 在當前目錄中建立bin和bin下的os_1目錄,權限設置為文件主可讀、寫、執行,同組用戶可讀和執行,其他用戶無權訪問

[sunjimeng@localhost Document]$ mkdir -pm 750 bin/os_1
[sunjimeng@localhost Document]$ ll
total 0
drwxrwxr-x. 3 sunjimeng sunjimeng 17 May  1 03:13 bin
[sunjimeng@localhost Document]$ cd bin
[sunjimeng@localhost bin]$ ll
total 0
drwxr-x---. 2 sunjimeng sunjimeng 6 May  1 03:13 os_1
[sunjimeng@localhost bin]$
6)[sunjimeng@localhost Document]$ mkdir --version 顯示mkdir的版本信息

[sunjimeng@localhost Document]$ mkdir --version
mkdir (GNU coreutils) 8.22
Copyright (C) 2013 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 permitted by law.

Written by David MacKenzie.
7)[sunjimeng@localhost Document]$ mkdir --parents Father/Child 與 mkdir -p Father/Child的效果是一樣的 同理 -m等同於--mood

[sunjimeng@localhost Document]$ mkdir --parents Father/Child
[sunjimeng@localhost Document]$ ll
total 0
drwxrwxr-x. 3 sunjimeng sunjimeng 17 May  1 03:13 bin
drwxrwxr-x. 3 sunjimeng sunjimeng 18 May  1 03:21 Father
[sunjimeng@localhost Document]$ cd Father
[sunjimeng@localhost Father]$ ll
total 0
drwxrwxr-x. 2 sunjimeng sunjimeng 6 May  1 03:21 Child
[sunjimeng@localhost Father]$

Copyright © Linux教程網 All Rights Reserved