mkdir命令用来创建目录。该命令创建由dirname命名的目录。如果在目录名的前面没有加任何路径名,则在当前目录下创建由dirname指定的目录;如果给出了一个已经存在的路径,将会在该目录下创建一个指定的目录。 在创建目录时,应保证新建的目录与它所在目录下的文件没有重名。
(1)用法:
mkdir [选项]... 目录...
(2)功能:
若指定目录不存在则创建目录
(3)选项参数
1)-Z: 设置安全上下文,当使用SELinux时有效
2) -m:<目标属性>或-mode<目标属性> 建立目录的同时设置目录的权限
3) -p或--parents 若所要建立目录的上层目录目前尚未建立,则会一并建立上层目录
4) -version 显示版本信息
(4)实例
1)[[email protected] Documents]$ mkdir mainDir 在当前文件夹下创建一个新文件
[[email protected] Documents]$ ll //列出当前目录下的文件及文件夹 total 0 [[email protected] Documents]$ mkdir mainDir //新建一个文件 [[email protected] Documents]$ ll //查看 total 0 drwxrwxr-x. 2 sunjimeng sunjimeng 6 May 1 02:49 mainDir
2)[[email protected] Documents]$ mkdir -v secondDir 在当期那文件夹下下创建一个新文件,并输出提示信息
[[email protected] Documents]$ mkdir -v secondDir mkdir: created directory ‘secondDir’ [[email protected] Documents]$
3)[[email protected] Documents]$ mkdir -p thirdDir/{test1,test2,test3} 在当前文件夹下创建一个新文夹,而且包含多个子文件夹
[[email protected] Documents]$ mkdir -p thirdDir/{test1,test2,test3} //新建包含多个子文件夹的文件夹 [[email protected] 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 [[email protected] Documents]$ cd thirdDir //进入目录 [[email protected] 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 [[email protected] thirdDir]$
[[email protected] 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 [[email protected] Documents]$ mkdir thirdDir/{test4,test5,test6}//虽然已经存在了thirdDir文件夹,但丝毫不影响这个操作 [[email protected] 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 [[email protected] Documents]$ cd thirdDir [[email protected] 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)[[email protected] Documents]$ mkdir -m 700 /home/sunjimeng/Document 在指定路径下创建文件夹,并且只有文件主有读、写和执行权限,其他人无权访问。
[[email protected] Documents]$ mkdir -m 700 /home/sunjimeng/Document [[email protected] Documents]$ cd /home/sunjimeng [[email protected] ~]$ 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)[[email protected] Document]$ mkdir -pm 750 bin/os_1 在当前目录中建立bin和bin下的os_1目录,权限设置为文件主可读、写、执行,同组用户可读和执行,其他用户无权访问
[[email protected] Document]$ mkdir -pm 750 bin/os_1 [[email protected] Document]$ ll total 0 drwxrwxr-x. 3 sunjimeng sunjimeng 17 May 1 03:13 bin [[email protected] Document]$ cd bin [[email protected] bin]$ ll total 0 drwxr-x---. 2 sunjimeng sunjimeng 6 May 1 03:13 os_1 [[email protected] bin]$
6)[[email protected] Document]$ mkdir --version 显示mkdir的版本信息
[[email protected] 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)[[email protected] Document]$ mkdir --parents Father/Child 与 mkdir -p Father/Child的效果是一样的 同理 -m等同于--mood
[[email protected] Document]$ mkdir --parents Father/Child [[email protected] 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 [[email protected] Document]$ cd Father [[email protected] Father]$ ll total 0 drwxrwxr-x. 2 sunjimeng sunjimeng 6 May 1 03:21 Child [[email protected] Father]$