This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 3 years ago.
The community reviewed whether to reopen this question 3 years ago and left it closed:
Original close reason(s) were not resolved
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(10)
但是这不会提示您确认或输出刚刚删除的内容。因此,最好首先在不执行 -delete 操作的情况下运行它,并检查它们是否是正确的文件。
BUT this won't prompt you for confirmation or output what it just deleted. Therefore best to run it without the -delete action first and check that they're the correct files.
您可以将
find
与-type f
仅用于文件和-maxdepth 1
一起使用,这样find
就不会搜索/path/to/directory
子目录中的文件。rm -i
将在每次删除时提示您,以便您确认或拒绝删除。如果您不介意每次删除时都会被要求确认,请将其更改为rm -fv
(-f
表示强制删除)。-v
标志使得每次删除时都会打印一条消息,说明刚刚删除了哪个文件。这应该满足以下标准:
You can use
find
with-type f
for files only and-maxdepth 1
sofind
won't search for files in sub-directories of/path/to/directory
.rm -i
will prompt you on each delete so you can confirm or deny the delete. If you dont care about being asked for confirmation of each delete, change it torm -fv
(-f
for force the delete). The-v
flag makes it so that with each delete, a message is printed saying what file was just deleted.This should meet the criteria:
由于这在谷歌搜索中很高,所以最简单的答案是:
其中 $directoryPath 是您要清空的目录。学分应该归cbm3384(由于某种原因,这个答案得到了反对票,为什么?)
如果您不想确认:
如果您不相信,请尝试
man rm
或上面创建了一个目录结构,每个文件夹中都有“helloX.txt”(X 是目录级别)。
rm 1/2/*
删除hello2.txt
并保持其他结构不变。另外,
rm */*/*
仅删除hello2.txt
。这是唯一与模式匹配的。只是清理 cakephp tmp 目录并保持目录结构完整的 Makefile 的一个示例:
rm
前面的减号表示“错误时不停止”(未删除的目录会返回错误)。如果您想保存某个级别,只需删除该行,例如第二行 rm 删除日志。如果您有一个可以执行其他操作的系统(BSD?),请告诉我。
编辑:我在 ubuntu 12.04、osx lion 和 sourceforge.net shell 上对此进行了测试。所有的行为都像上面的解释。
Since this is high on google search, the simplest answer is:
where $directoryPath is the directory you want to empty. Credits should go to cbm3384 (that for some reason has gotten negative votes for this answer, why?)
If you do not want to confirm:
If you don't believe try
man rm
orThe above creates a directory structure, that has 'helloX.txt' in each folder (X is the directory level).
rm 1/2/*
deleteshello2.txt
and leaves the other structure intact.Also
rm */*/*
deletes onlyhello2.txt
. It is the only that matches the pattern.Just an example of a Makefile that cleans cakephp tmp-directory and leaves the directory structure intact:
Minus in front of the
rm
means "do not halt on errors" (unremoved directory returns an error). If you want some level to be saved, just remove that line, e.g. second rm line removes logs.Let me know if you have a system that does something else (BSD?).
EDIT: I tested this on ubuntu 12.04, osx lion and sourceforge.net shell. All behave like the explanation above.
rm
默认情况下不会删除目录。因此,在您的示例中,假设您位于父目录并且这些是所有文件,您所需要的只是:rm
won't delete directories by default. So in your example, assuming you're in the parent directory and those are all the files, all you need is:rm -f dirname/*
将仅删除文件,而不提示输入每个文件。它还将为每个子目录显示“无法删除'subdirname':是一个目录”
。rm -f dirname/*
will remove only files without prompting for each file. It will also display"Cannnot remove 'subdirname': Is a directory"
for each sub directory.TL;DR:
查找 . -maxdepth 1 -type f -delete
等:
没什么大不了的,但上面的建议对我不起作用,因为...
find . -类型f -max深度1 -删除
TL;DR:
find . -maxdepth 1 -type f -delete
Etc:
Not a big deal but the suggestions above didn't work for me because...
find . -type f -maxdepth 1 -delete
rm 目录名/*
?如果没有-f
它不会强制删除,如果没有-r
它不会递归和删除目录和文件。rm dirname/*
? Without-f
it won't force-delete, without-r
it won't recurse and delete directories as well as files.为此,我将使用最大深度为 1 的 find,然后使用文件列表执行 rm。
编辑:这基本上与詹姆斯发布的内容相同,但我直到之后才看到他的帖子
For this, I would use find with a max depth of 1 and then exec rm with the file list.
Edit: this is essentially the same as what James posted but I didn't see his post until after
以下两个命令将递归删除当前目录中的所有文件和符号链接:
作为一个命令,可以执行以下操作:
find 。 -type f -delete&&find 。 -类型 l -删除
The following two commands will recursively delete all files and symbolic links in the current directory:
As one command, the following works:
find . -type f -delete&&find . -type l -delete
对我有用的是 PERL 脚本:
从您想要清理的目录向上运行这一级:将“subdirectory_name”替换为目录名称。
在不占用 CPU 的情况下处理数百万个文件。
What worked for me is a PERL script:
Run this one level up from the directory you wish to clean: replace "subdirectory_name" with the directories name.
Worked on millions of files without killing the CPU.