linux删除目录命令_如何使用一个Linux命令删除多个子目录

linux删除目录命令

linux删除目录命令

00_lead_image_removing_multiple_subdirectories

If you want to remove several subdirectories within another directory using the command line in Linux, generally you have to use the rm command several times. However, there is a faster way to do this.

如果要在Linux中使用命令行删除另一个目录中的多个子目录,通常必须多次使用rm命令。 但是,有一种更快的方法可以做到这一点。

Let’s say we have a directory called htg with five subdirectories within it and we want to delete three of them. In a normal situation, we’d use the rm command three times.

假设我们有一个名为htg的目录,其中包含五个子目录,我们想删除其中的三个。 在正常情况下,我们将使用rm命令3次。

02_removing_subdirectories_separately

However, we can make this process even shorter by combining the three rm commands into one. Here’s how.

但是,通过将三个rm命令组合为一个,我们可以使此过程变得更短。 这是如何做。

To remove the three subdirectories you only need to type the following command at the prompt and press Enter (obviously, change the directory names to what you want to remove).

要删除这三个子目录,只需在提示符下键入以下命令,然后按Enter键(显然,将目录名更改为要删除的目录名)。

rm -r ~/Documents/htg/{done,ideas,notes}

The words in the brackets are part of the “brace expansion list”. Each of the items in the brace expansion list is appended separately to the preceding path (~/Documents/htg/). For example, the above command is expanded into ~/Documents/htg/done, ~/Documents/htg/ideas, and ~/Documents/htg/notes, the three subdirectories under the htg directory that we want to remove. As you can see in the screenshot below, those three subdirectories were removed.

方括号中的词是“括号扩展列表”的一部分。 大括号扩展列表中的每个项目都分别附加到前面的路径(〜/ Documents / htg /)。 例如,以上命令被扩展为〜/ Documents / htg / done,〜/ Documents / htg / ideas和〜/ Documents / htg / notes,这是我们要删除的htg目录下的三个子目录。 如下面的屏幕快照所示,这三个子目录已被删除。

03_removing_three_subdirectries_at_once

The -r flag is required when using the rm command to remove a directory rather than a file. If you leave the -r flag out of the above command, you will get an error saying that the directories cannot be removed.

使用rm命令除去目录而不是文件时,需要-r标志。 如果将-r标志保留在上述命令之外,则会收到一条错误消息,指出无法删除目录。

04_cannot_remove_subdirectory

If all of the subdirectories you want to remove are empty, you can use the rmdir command, as shown below.

如果要删除的所有子目录都为空,则可以使用rmdir命令,如下所示。

rmdir ~/Documents/htg/{done,ideas,notes}

If it turns out that any of the subdirectories are not empty, an error will display saying that the removal failed and the subdirectory in question and its subdirectories are not removed. However, any empty subdirectories are removed.

如果事实证明子目录中的任何一个都不为空,则会显示一条错误消息,指出删除失败,并且该子目录及其子目录未删除。 但是,任何空的子目录都将被删除。

05_using_rmdir_command

Be very careful with the rm command. Using it the wrong way can delete all the files on your hard drive.

rm命令要非常小心。 以错误的方式使用它可能会删除硬盘驱动器上的所有文件。

You can also create a directory containing several subdirectories, or a directory tree, using one command.

您还可以使用一个命令创建包含多个子目录的目录或目录树

翻译自: https://www.howtogeek.com/276516/how-to-remove-multiple-subdirectories-with-one-linux-command/

linux删除目录命令

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值