每天一个Linux命令(3)mkdir命令
mkdir命令用来创建目录。
(1)用法:
用法: mkdir [选项]... 目录...
(2)功能:
功能: 若指定目录不存在则创建目录
该命令创建由dirname命名的目录。如果在目录名的前面没有加任何路径名,则在当前目录下创建由dirname指定的目录;如果给出了一个已经存在的路径,将会在该目录下创建一个指定的目录。在创建目录时,应保证新建的目录与它所在目录下的文件没有重名。
(3)选项参数
1)-Z: 设置安全上下文,当使用SELinux时有效
2) -m:<目标属性>或-mode<目标属性> 建立目录的同时设置目录的权限
3) -p或--parents 若所要建立目录的上层目录目前尚未建立,则会一并建立上层目录
4) -version 显示版本信息
(4)实例
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]$