linux创建和删除目录-mkdir和rmdir命令详解
1.mkdir命令建立新目录
回到顶部[root@initroot ~]# mkdir [-mp] 目录名称选项与参数:
-m :设置目录的权限!-m选项设置的目录权限不受权限掩码umask的影响;将当前工作目录切换到/tmp目录下,然后建立一个新目录testdir:
-p :递归建立多层目录
peter@initroot:~$ cd /tmp/ peter@initroot:/tmp$ mkdir testdir peter@initroot:/tmp$ ls -ld testdir drwxr-xr-x 2 peter peter 4096 Jan 7 19:17 testdir
创建多级目录需要加上-p选项:
peter@initroot:/tmp$ mkdir -p test1/test2/test3/test4如果不加-p选项是无法创建的,因为多级目录需要一层一层手动建立,只有先创建了父目录才能创建子目录, 加上-p选项就可以自动地创建多级目录了。不过这里并不建议使用-p选项,因为目录参数一旦输入错误,就会造成混乱。 如果要建立多级目录,最好是手动一层一层建立,这样还能用tab自动补全帮助检查拼写错误。
创建一个新目录dir1,并将新目录的权限设置为rwx--x--x:
peter@initroot:/tmp$ mkdir -m 711 dir1 peter@initroot:/tmp$ ls -ld dir1 drwx--x--x 2 peter peter 4096 Jan 7 19:35 dir1如果你理解了linux文件权限的知识,应该不难理解上面命令中的711是什么意思。如果看不懂,那就回到我们的上一篇 linux文件属性与权限 章节查看吧!
-m权限可以指定新建目录的权限,新指定的权限不受umask的影响。如果不加-m选项,系统会创建默认权限的新目录。 关于默认权限和umask的内容我们也已经在上一篇 linux文件属性与权限 章节讲解过了,这里不在赘述。
mkdir用于创建一个新目录:
mkdir initroot
上述命令在当前工作目录下创建initroot目录
Usage:
mkdir [OPTION]... DIRECTORY...
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
-p, --parents no error if existing, make parent directories as needed
-v, --verbose print a message for each created directory
-Z set SELinux security context of each created directory to the default type
--context[=CTX] like -Z, or if CTX is specified then set the SELinux or SMACK security context to CTX
--help display this help and exit
--version output version information and exit
GNU coreutils online help: http://www.gnu.org/software/coreutils/ Full documentation at:
http://www.gnu.org/software/coreutils/mkdir or available locally via:
info '(coreutils) mkdir invocation'
2.rmdir命令删除空目录
回到顶部[root@initroot ~]# rmdir [-p] 目录名称-p选项类似mkdir命令的-p选项,可以删除多层级空目录。
删除上面建立的testdir和dir1目录:
peter@initroot:/tmp$ rmdir testdir peter@initroot:/tmp$ rmdir dir1 peter@initroot:/tmp$ ls -al total 204 drwxr-xr-x 2 peter peter 4096 Jan 7 19:17 . drwxrwxrwt 18 root root 200704 Jan 7 19:51 .. drwxrwxrwt 18 root root 200704 Jan 7 19:51 test1通过ls -al命令可看出testdir和dir1目录已成功删除。 注意rmdir命令只能用于删除空目录,即目录需要手动一层一层删除。如果目录不为空,那么rmdir命令会失败报错:
peter@initroot:/tmp$ rmdir test1 rmdir: failed to remove ‘test1’: Directory not empty需要先将目录中的文件删除或者通过rm -r test1命令强制删除目录。 当然我们也可以通过-p选项删除:
peter@initroot:/tmp$ rmdir -p test1/test2/test3/test4
rmdir是remove directory的缩写,该命令用于删除空目录,再用rmdir命令删除目录之前,需要确保目录为空目录
删除当前工作目录下的testdir目录
rmdir testdir
Usage:
rmdir [OPTION]... DIRECTORY...
Remove the DIRECTORY(ies), if they are empty.
--ignore-fail-on-non-empty ignore each failure that is solely because a directory is non-empty
-p, --parents remove DIRECTORY and its ancestors; e.g., 'rmdir -p a/b/c' is similar to 'rmdir a/b/c a/b a'
-v, --verbose output a diagnostic for every directory processed
--help display this help and exit
--version output version information and exit
GNU coreutils online help: http://www.gnu.org/software/coreutils/ Full documentation at:
http://www.gnu.org/software/coreutils/rmdir or available locally via:
info '(coreutils) rmdir invocation'
initroot编辑整理,转载请注明www.initroot.com
100次点赞
100次阅读