如何通过终端快速删除文件和目录(bash shell)[关闭]

本文翻译自:How to remove files and directories quickly via terminal (bash shell) [closed]

From terminal window: 从终端窗口:

When I use the rm command it can only remove files. 当我使用rm命令时,它只能删除文件。
When I use the rmdir command it only removes empty folders. 当我使用rmdir命令时,它只删除空文件夹。

If I have a directory nested with files and folders within folders with files and so on, is there any way to delete all the files and folders without all the strenuous command typing? 如果我有一个目录与文件夹中的文件和文件夹嵌套,等等,有没有办法删除所有的文件和文件夹而没有所有的命令输入?

If it makes a difference, I am using the mac bash shell from terminal, not Microsoft DOS or linux. 如果它有所作为,我使用终端的mac bash shell,而不是Microsoft DOS或linux。


#1楼

参考:https://stackoom.com/question/B5LH/如何通过终端快速删除文件和目录-bash-shell-关闭


#2楼

So I was looking all over for a way to remove all files in a directory except for some directories, and files, I wanted to keep around. 所以我一直在寻找一种方法来删除目录中的所有文件,除了一些目录和文件,我想保留。 After much searching I devised a way to do it using find. 经过多次搜索,我设计了一种使用find的方法。

find -E . -regex './(dir1|dir2|dir3)' -and -type d -prune -o -print -exec rm -rf {} \;

Essentially it uses regex to select the directories to exclude from the results then removes the remaining files. 本质上,它使用正则表达式来选择要从结果中排除的目录,然后删除剩余的文件。 Just wanted to put it out here in case someone else needed it. 只是想把它放在这里以防其他人需要它。


#3楼

rm -rf some_dir

-r "recursive" -f "force" (suppress confirmation messages) -r“recursive”-f“force”(禁止确认消息)

Be careful! 小心!


#4楼

rm -rf *

Would remove everything (folders & files) in the current directory. 将删除当前目录中的所有内容(文件夹和文件)。

But be careful! 不过要小心! Only execute this command if you are absolutely sure, that you are in the right directory. 如果您完全确定,只有执行此命令才能执行此命令。


#5楼

Yes, there is. 就在这里。 The -r option tells rm to be r ecursive, and remove the entire file hierarchy rooted at its arguments; -r选项告诉rmR ecursive,并删除其参数为根的整个文件层次; in other words, if given a directory, it will remove all of its contents and then perform what is effectively an rmdir . 换句话说,如果给定一个目录,它将删除其所有内容,然后执行有效的rmdir

The other two options you should know are -i and -f . 您应该知道的其他两个选项是-i-f -i stands for i nteractive; -i代表i nteractive; it makes rm prompt you before deleting each and every file. 它会让rm在删除每个文件之前提示你。 -f stands for f orce; -f代表f ; it goes ahead and deletes everything without asking. 它继续前进并删除所有内容而不询问。 -i is safer, but -f is faster; -i更安全,但-f更快; only use it if you're absolutely sure you're deleting the right thing. 只有在你完全确定你正在删除正确的东西时才使用它。 You can specify these with -r or not; 您可以使用-r指定它们; it's an independent setting. 这是一个独立的环境。

And as usual, you can combine switches: rm -r -i is just rm -ri , and rm -r -f is rm -rf . 像往常一样,你可以组合开关: rm -r -i只是rm -ri ,而rm -r -frm -rf

Also note that what you're learning applies to bash on every Unix OS: OS X, Linux, FreeBSD, etc. In fact, rm 's syntax is the same in pretty much every shell on every Unix OS. 另请注意,您正在学习的内容适用于每个Unix操作系统上的bash :OS X,Linux,FreeBSD等。实际上, rm的语法在每个Unix操作系统的几乎每个shell中都是相同的。 OS X, under the hood, is really a BSD Unix system. 引擎盖下的OS X实际上是一个BSD Unix系统。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值