Merge pull request #7787 from cyleft/master

translated done
This commit is contained in:
Xingyu.Wang 2018-02-16 09:56:38 +08:00 committed by GitHub
commit aa7d2a5515
2 changed files with 80 additions and 81 deletions

View File

@ -1,81 +0,0 @@
translated by cyleft
Linux rmdir Command for Beginners (with Examples)
======
So we've already discussed [the rm command][1] that's primarily used for deleting files and directories from the Linux command line. However, there's another, related command line utility that is specifically aimed at removing directories. The tool in question is **rmdir** , and in this tutorial, we will discuss the basics of it using some easy to understand examples.
#### Linux rmdir command
As the name suggests, the rmdir command is focused at removing directories, although empty-ones only. Following is its syntax:
```
rmdir [OPTION]... DIRECTORY...
```
And here's how the man page explains it:
```
Remove the DIRECTORY(ies), if they are empty.
```
The following Q&A-styled examples should give you a good idea on how this utility works.
#### Q1. How rmdir works?
That's pretty straight forward - just pass the directory name as input to the command. For example:
```
rmdir test-dir
```
[![How rmdir works][2]][3]
#### Q2. How to make rmdir ignore non-empty directories.
BY default, the rmdir command throws an error if you try deleting a non-empty directory. However, if you want, you can suppress this behavior of rmdir using the --ignore-fail-on-non-empty option.
For example:
[![How to make rmdir ignore non-empty directories][4]][5]
#### Q3. How to make rmdir remove parent directories as well?
Just like in the case of [mkdir][6], you can also ask rmdir to perform its operation on parent directories. What that means is, you can also delete parent directories of a directory in one go. This feature is accessible through the -p command line option.
For example, the following command will delete both 'test' and 'test-dir' directories.
```
rmdir -p test/test-dir/
```
**Note** : For this operation to work, all parent directories should not contain anything other than the empty-directory being deleted.
#### Q4. What is the difference between rmdir and rm -r ?
If you remember, you can also delete directories using the rm command by enabling the -r option it provides. So what's the difference between that and rmdir? Well, the answer is rmdir only works in the case of empty directories - there's no way whatsoever you can use to make rmdir delete non-empty directories.
So rmdir is a useful in tool in those situations where you otherwise need to check if a directory is empty before deleting it.
#### Conclusion
As you'll agree, rmdir isn't a complex command to understand and use. Plus, it offers only a handful command line options. We've discussed almost all of them here, so practice the examples mentioned in this article, and you should be good to go. Just in case you need, [here's the man page][7] for rmdir.
--------------------------------------------------------------------------------
via: https://www.howtoforge.com/linux-rmdir-command/
作者:[Himanshu Arora][a]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:https://www.howtoforge.com
[1]:https://www.howtoforge.com/linux-rm-command/
[2]:https://www.howtoforge.com/images/command-tutorial/rm-basic-usage1.png
[3]:https://www.howtoforge.com/images/command-tutorial/big/rm-basic-usage1.png
[4]:https://www.howtoforge.com/images/command-tutorial/rmdir-ignore-nonempty.png
[5]:https://www.howtoforge.com/images/command-tutorial/big/rmdir-ignore-nonempty.png
[6]:https://www.howtoforge.com/linux-mkdir-command/
[7]:https://linux.die.net/man/1/rmdir

View File

@ -0,0 +1,80 @@
为初学者准备的 Linux rmdir 命令(案例教程)
======
在 Linux 命令行上删除文件和目录,我们已经讨论过 [rm 命令][1] 的使用。然而,这里有另一个相关的命令行工具,专门针对目录的删除。这是所指的工具就是 **rmdir**,本教程中,我们将根据一些简单的实例来讨论它。
#### Linux rmdir 命令
见名知意rmdirremoveDirectory 命令专门针对删除目录,不过通常目录内空空如也。如下是它的语法:
```
rmdir [OPTION]... DIRECTORY...
```
下面是 man 文档的说明:
```
删除目录(索引),如其目录为空。
```
在理解此程序如何工作方面,下面的问答风格的案例可能会给您带来更好的灵感。
#### Q1. rmdir 命令如何工作?
这非常直接,只需将目录名作为输入参数传递给它。
比如:
```
rmdir test-dir
```
[![rmdir 命令如何工作][2]][3]
#### Q2. 如何让 rmdir 忽视非空目录?
通常情况下rmdir 命令会在您删除非空目录时抛出一个错误。然而,如果需要的话,您可以在非空目录下使用 --ignore-fail-on-non-empty 选项,忽略它。
比如说:
[![如何让 rmdir 忽视非空目录][4]][5]
#### Q3. 如何让 rmdir 命令移除父目录?
和 [mkdir][6] 一样,您可以通过要求 rmdir 命令在其父目录执行操作。这意味着,您可以一次性删除目录和目录的上级目录。这个功能可以通过 -p 命令选项实现。
比如,下面的命令将会删除 'test' 和 'test-dir' 目录。
```
rmdir -p test/test-dir/
```
**注意** 欲此操作生效,所有被删除的父目录都不应该包含任何空目录之外的目录。
#### Q4. rmdir 和 rm -r 有何区别?
如果还有印象,您还可以通过提供 -r 选项的 rm 命令删除目录。那 rmdir 和 rm -r 区别何在rmdir 仅在空目录下生效 - 您没有办法使用 rmdir 删除非空目录。
因此rmdir 在删除需要检测目录是否为空的情况下,是一个有效的工具。
#### 总结
相信您会认同rmdir 并不是一个难以理解和使用的复杂命令。除此之外,它仅提供少量命令行选项。在此,我们几乎讨论了所有的选项,如果您想掌握的更好,练习在本教程提到的案例即可。关于 rmdir[这里另有 man 文档][7],备您不时之需。
--------------------------------------------------------------------------------
via: https://www.howtoforge.com/linux-rmdir-command/
作者:[Himanshu Arora][a]
译者:[CYLeft](https://github.com/CYLeft)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:https://www.howtoforge.com
[1]:https://www.howtoforge.com/linux-rm-command/
[2]:https://www.howtoforge.com/images/command-tutorial/rm-basic-usage1.png
[3]:https://www.howtoforge.com/images/command-tutorial/big/rm-basic-usage1.png
[4]:https://www.howtoforge.com/images/command-tutorial/rmdir-ignore-nonempty.png
[5]:https://www.howtoforge.com/images/command-tutorial/big/rmdir-ignore-nonempty.png
[6]:https://www.howtoforge.com/linux-mkdir-command/
[7]:https://linux.die.net/man/1/rmdir