8 个你不一定全都了解的 rm 命令示例
rm 删除
![](/data/attachment/album/201802/07/203619n9bb3pdwmmpqxwfw.jpg)


删除文件和复制/移动文件一样，都是很基础的操作。在 Linux 中，有一个专门的命令 `rm`，可用于完成所有删除相关的操作。在本文中，我们将用些容易理解的例子来讨论这个命令的基本使用。


但在我们开始前，值得指出的是本文所有示例都在 Ubuntu 16.04 LTS 中测试过。


### Linux rm 命令概述


通俗的讲，我们可以认为 `rm` 命令是用于删除文件和目录的。下面是此命令的语法：



```
rm [选项]... [要删除的文件/目录]...

```

下面是命令使用说明：



> 
> GUN 版本 `rm` 命令的手册文档。`rm` 删除每个指定的文件，默认情况下不删除目录。
> 
> 
> 当删除的文件超过三个或者提供了选项 `-r`、`-R` 或 `--recursive`（LCTT 译注：表示递归删除目录中的文件）时，如果给出 `-I`（LCTT 译注：大写的 I）或 `--interactive=once` 选项（LCTT 译注：表示开启交互一次），则 `rm` 命令会提示用户是否继续整个删除操作，如果用户回应不是确认（LCTT 译注：即没有回复 `y`），则整个命令立刻终止。
> 
> 
> 另外，如果被删除文件是不可写的，标准输入是终端，这时如果没有提供 `-f` 或 `--force` 选项，或者提供了 `-i`（LCTT 译注：小写的 i） 或 `--interactive=always` 选项，`rm` 会提示用户是否要删除此文件，如果用户回应不是确认（LCTT 译注：即没有回复 `y`），则跳过此文件。
> 
> 
> 


下面这些问答式例子会让你更好的理解这个命令的使用。


### Q1. 如何用 rm 命令删除文件？


这是非常简单和直观的。你只需要把文件名（如果文件不是在当前目录中，则还需要添加文件路径）传入给 `rm` 命令即可。


（LCTT 译注：可以用空格隔开传入多个文件名称。）



```
rm 文件1 文件2 ... 

```

如：



```
rm testfile.txt

```

[![How to remove files using rm command](/data/attachment/album/201802/07/203650qdudn2d8efkb2jln.png)](https://www.howtoforge.com/images/command-tutorial/big/rm-basic-usage.png)


### Q2. 如何用 `rm` 命令删除目录?


如果你试图删除一个目录，你需要提供 `-r` 选项。否则 `rm` 会抛出一个错误告诉你正试图删除一个目录。


（LCTT 译注：`-r` 表示递归地删除目录下的所有文件和目录。）



```
rm -r [目录名称]

```

如：



```
rm -r testdir

```

[![How to remove directories using rm command](/data/attachment/album/201802/07/203653tq2kvwwe3kquvabw.png)](https://www.howtoforge.com/images/command-tutorial/big/rm-r.png)


### Q3. 如何让删除操作前有确认提示?


如果你希望在每个删除操作完成前都有确认提示，可以使用 `-i` 选项。



```
rm -i [文件/目录]

```

比如，你想要删除一个目录“testdir”，但需要每个删除操作都有确认提示，你可以这么做：



```
rm -r -i testdir

```

[![How to make rm prompt before every removal](/data/attachment/album/201802/07/203654jb36a85jpd34535a.png)](https://www.howtoforge.com/images/command-tutorial/big/rm-i-option.png)


### Q4. 如何让 rm 忽略不存在的文件或目录?


如果你删除一个不存在的文件或目录时，`rm` 命令会抛出一个错误，如：


[![Linux rm command example](/data/attachment/album/201802/07/203656gjq41qjdx7b1gw44.png)](https://www.howtoforge.com/images/command-tutorial/big/rm-non-ext-error.png)


然而，如果你愿意，你可以使用 `-f` 选项（LCTT 译注：即 “force”）让此次操作强制执行，忽略错误提示。



```
rm -f [文件...]

```

[![How to force rm to ignore nonexistent files](/data/attachment/album/201802/07/203658clziljliiqu5sl5d.png)](https://www.howtoforge.com/images/command-tutorial/big/rm-f-option.png)


### Q5. 如何让 rm 仅在某些场景下确认删除？


选项 `-I`，可保证在删除超过 3 个文件时或递归删除时（LCTT 译注： 如删除目录）仅提示一次确认。


比如，下面的截图展示了 `-I` 选项的作用——当两个文件被删除时没有提示，当超过 3 个文件时会有提示。


[![How to make rm prompt only in some scenarios](/data/attachment/album/201802/07/203701yk3ace3i8f3wki3w.png)](https://www.howtoforge.com/images/command-tutorial/big/rm-I-option.png)


### Q6. 当删除根目录是 rm 是如何工作的?


当然，删除根目录（`/`）是 Linux 用户最不想要的操作。这也就是为什么默认 `rm` 命令不支持在根目录上执行递归删除操作。（LCTT 译注：早期的 `rm` 命令并无此预防行为。）


[![How rm works when dealing with root directory](/data/attachment/album/201802/07/203703r88y4ws00yoddf8k.png)](https://www.howtoforge.com/images/command-tutorial/big/rm-root-default.png)


然而，如果你非得完成这个操作，你需要使用 `--no-preserve-root` 选项。当提供此选项，`rm` 就不会特殊处理根目录（`/`）了。


假如你想知道在哪些场景下 Linux 用户会删除他们的根目录，点击[这里](https://superuser.com/questions/742334/is-there-a-scenario-where-rm-rf-no-preserve-root-is-needed)。


### Q7. 如何让 rm 仅删除空目录？


假如你需要 `rm` 在删除目录时仅删除空目录，你可以使用 `-d` 选项。



```
rm -d [目录]

```

下面的截图展示 `-d` 选项的用途——仅空目录被删除了。


[![How to make rm only remove empty directories](/data/attachment/album/201802/07/203705afvq3ffmxjx5navb.png)](https://www.howtoforge.com/images/command-tutorial/big/rm-d-option.png)


### Q8. 如何让 rm 显示当前删除操作的详情?


如果你想 rm 显示当前操作完成时的详细情况，使用 `-v` 选项可以做到。



```
rm -v [文件/目录]

```

如：


[![How to force rm to emit details of operation it is performing](/data/attachment/album/201802/07/203707p3aspiahi11iivq3.png)](https://www.howtoforge.com/images/command-tutorial/big/rm-v-option.png)


### 结论


考虑到 `rm` 命令提供的功能，可以说其是 Linux 中使用频率最高的命令之一了（就像 [cp](https://www.howtoforge.com/linux-cp-command/) 和 `mv` 一样）。在本文中，我们涉及到了其提供的几乎所有主要选项。`rm` 命令有些学习曲线，因此在你日常工作中开始使用此命令之前 你将需要花费些时间去练习它的选项。更多的信息，请点击此命令的 [man 手册页](https://linux.die.net/man/1/rm)。




---


via: <https://www.howtoforge.com/linux-rm-command/>


作者：[Himanshu Arora](https://www.howtoforge.com) 译者：[yizhuoyan](https://github.com/yizhuoyan) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
# Linux rm Command Explained for Beginners (8 Examples)

### On this page

[Linux rm command](#linux-rm-command)[Q1. How to remove files using rm command?](#q-how-to-remove-files-using-rm-command)[Q2. How to remove directories using rm command?](#q-how-to-remove-directories-using-rm-command)[Q3. How to make rm prompt before every removal?](#q-how-to-make-rm-prompt-before-every-removal)[Q4. How to force rm to ignore nonexistent files?](#q-how-to-force-rm-to-ignore-nonexistent-files)[Q5. How to make rm prompt only in some scenarios?](#q-how-to-make-rm-prompt-only-in-some-scenarios)[Q6. How rm works when dealing with root directory?](#q-how-rm-works-when-dealing-with-root-directory)[Q7. How to make rm only remove empty directories?](#q-how-to-make-rm-only-remove-empty-directories)[Q8. How to force rm to emit details of operation it is performing?](#q-how-to-force-rm-to-emit-details-of-operation-it-is-performing)[Conclusion](#conclusion)

Deleting files is a fundamental operation, just like copying files or renaming/moving them. In Linux, there's a dedicated command - dubbed **rm** - that lets you perform all deletion-related operations. In this tutorial, we will discuss the basics of this tool along with some easy to understand examples.

But before we do that, it's worth mentioning that all examples mentioned in the article have been tested on Ubuntu 16.04 LTS.

## Linux rm command

So in layman's terms, we can simply say the rm command is used for removing/deleting files and directories. Following is the syntax of the command:

rm [OPTION]... [FILE]...

And here's how the tool's man page describes it:

This manual page documents the GNU version of rm. rm removes each specified file. By default, it

does not remove directories.

If the -I or --interactive=once option is given, and there are more than three files or the -r,

-R, or --recursive are given, then rm prompts the user for whether to proceed with the entire

operation. If the response is not affirmative, the entire command is aborted.

Otherwise, if a file is unwritable, standard input is a terminal, and the -f or --force option is

not given, or the -i or --interactive=always option is given, rm prompts the user for whether to

remove the file. If the response is not affirmative, the file is skipped.

The following Q&A-styled examples will give you a better idea on how the tool works.

## Q1. How to remove files using rm command?

That's pretty easy and straightforward. All you have to do is to pass the name of the files (along with paths if they are not in the current working directory) as input to the rm command.

rm [filename]

For example:

rm testfile.txt

## Q2. How to remove directories using rm command?

If you are trying to remove a directory, then you need to use the **-r** command line option. Otherwise, rm will throw an error saying what you are trying to delete is a directory.

rm -r [dir name]

For example:

rm -r testdir

## Q3. How to make rm prompt before every removal?

If you want rm to prompt before each delete action it performs, then use the **-i** command line option.

rm -i [file or dir]

For example, suppose you want to delete a directory 'testdir' and all its contents, but want rm to prompt before every deletion, then here's how you can do that:

rm -r -i testdir

## Q4. How to force rm to ignore nonexistent files?

The rm command lets you know through an error message if you try deleting a non-existent file or directory.

However, if you want, you can make rm suppress such error/notifications - all you have to do is to use the **-f** command line option.

rm -f [filename]

## Q5. How to make rm prompt only in some scenarios?

There exists a command line option **-I**, which when used, makes sure the command only prompts once before removing more than three files, or when removing recursively.

For example, the following screenshot shows this option in action - there was no prompt when two files were deleted, but the command prompted when more than three files were deleted.

## Q6. How rm works when dealing with root directory?

Of course, deleting root directory is the last thing a Linux user would want. That's why, the rm command doesn't let you perform a recursive delete operation on this directory by default.

However, if you want to go ahead with this operation for whatever reason, then you need to tell this to rm by using the * --no-preserve-root* option. When this option is enabled, rm doesn't treat the root directory (/) specially.

*In case you want to know the scenarios in which a user might want to delete the root directory of their system, head here.*

## Q7. How to make rm only remove empty directories?

In case you want to restrict rm's directory deletion ability to only empty directories, then you can use the -d command line option.

rm -d [dir]

The following screenshot shows the -d command line option in action - only empty directory got deleted.

## Q8. How to force rm to emit details of operation it is performing?

If you want rm to display detailed information of the operation being performed, then this can be done by using the **-v** command line option.

rm -v [file or directory name]

For example:

## Conclusion

Given the kind of functionality it offers, rm is one of the most frequently used commands in Linux (like [cp](https://www.howtoforge.com/linux-cp-command/) and mv). Here, in this tutorial, we have covered almost all major command line options this tool provides. rm has a bit of learning curve associated with, so you'll have to spent some time practicing its options before you start using the tool in your day to day work. For more information, head to the command's [man page](https://linux.die.net/man/1/rm).