仅删除linux目录中的文件而不是目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7714900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 06:35:42  来源:igfitidea点击:

Remove only files in directory on linux NOT directories

linuxrm

提问by AndrewC

What delete command can be run to remove only files in given directory

可以运行什么删除命令来仅删除给定目录中的文件

  • NOT directories
  • NOT sub-directories
  • NOT files in these sub-directories.
  • 非目录
  • 非子目录
  • NOT 文件在这些子目录中。

Some files don't have extensions so rm *.*wont work...

某些文件没有扩展名,因此rm *.*无法使用...

There are thousands of files in this folder.

该文件夹中有数千个文件。

Any advice?

有什么建议吗?

采纳答案by AndrewC

What worked for me is a PERL script:

对我有用的是一个 PERL 脚本:

perl -e 'chdir "subdirectory_name" or die; opendir D, "."; while ($n = readdir D) { unlink $n }'

Run this one level up from the directory you wish to clean: replace "subdirectory_name" with the directories name.

从您要清理的目录向上运行此一级:将“subdirectory_name”替换为目录名称。

Worked on millions of files without killing the CPU.

在不杀死 CPU 的情况下处理数百万个文件。

回答by chown

You can use findwith -type ffor files only and -maxdepth 1so findwon't search for files in sub-directories of /path/to/directory. rm -iwill 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 to rm -fv(-ffor forcethe delete). The -vflag makes it so that with each delete, a message is printed saying what file was just deleted.

您可以使用find-type f用于文件仅-maxdepth 1所以find不会搜索中的子目录中的文件/path/to/directoryrm -i每次删除时都会提示您,以便您确认或拒绝删除。如果你不关心被要求对每个删除的确认,将其更改为rm -fv-f删除)。该-v标志使得每次删除时,都会打印一条消息,说明刚刚删除了哪个文件。

find /path/to/directory -maxdepth 1 -type f -exec rm -iv {} \;

This should meet the criteria:

这应该满足以下标准:

NOT directories
NOT subdirectories
NOT files in these subdirectories.

NOT 目录
NOT 子目录
NOT 这些子目录中的文件。

回答by Dave Newton

rm dirname/*? Without -fit won't force-delete, without -rit won't recurse and delete directories as well as files.

rm dirname/*? 没有-f它就不会强制删除,没有-r它就不会递归和删除目录和文件。

回答by cbm3384

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.

rm -f dirname/*将只删除文件而不提示每个文件。它还将为"Cannnot remove 'subdirname': Is a directory"每个子目录显示。

回答by James

find PATH -maxdepth 1 -type f -delete

BUTthis 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.

这个不会提示你什么,它只是删除确认或输出。因此最好先在没有 -delete 操作的情况下运行它并检查它们是否是正确的文件。

回答by Juha

Since this is high on google search, the simplest answer is:

由于这在谷歌搜索中很高,最简单的答案是:

rm $directoryPath/*

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?)

其中 $directoryPath 是您要清空的目录。积分应该转到 cbm3384(出于某种原因,这个答案得到了反对票,为什么?)

If you do not want to confirm:

如果您不想确认:

rm -f $directoryPath/*

If you don't believe try man rmor

如果你不相信尝试man rm

mkdir -p 1/2/3; echo 'hello1' > 1/hello1.txt; echo 'hello2' > 1/2/hello2.txt;echo 'hello3' > 1/2/3/hello3.txt
rm 1/2/*

The above creates a directory structure, that has 'helloX.txt' in each folder (X is the directory level). rm 1/2/*deletes hello2.txtand leaves the other structure intact.

上面创建了一个目录结构,在每个文件夹中都有 'helloX.txt'(X 是目录级别)。rm 1/2/*删除hello2.txt并保持其他结构不变。

Also rm */*/*deletes only hello2.txt. It is the only that matches the pattern.

rm */*/*只删除hello2.txt. 它是唯一与模式匹配的。

Just an example of a Makefile that cleans cakephp tmp-directory and leaves the directory structure intact:

只是一个清理 cakephp tmp-directory 并保持目录结构完整的 Makefile 示例:

clean:
    -rm -f tmp/*
    -rm -f tmp/*/*
    -rm -f tmp/*/*/*
    -rm -f tmp/*/*/*/*

Minus in front of the rmmeans "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.

前面的减号rm表示“不要因错误而停止”(未删除的目录返回错误)。如果您想保存某个级别,只需删除该行,例如第二个 rm 行删除日志。

Let me know if you have a system that does something else (BSD?).

如果您的系统可以执行其他操作(BSD?),请告诉我。

EDIT:I tested this on ubuntu 12.04, osx lion and sourceforge.net shell. All behave like the explanation above.

编辑:我在 ubuntu 12.04、osx lion 和 sourceforge.net shell 上测试了这个。所有的行为都像上面的解释。

回答by th3v0id

For this, I would use find with a max depth of 1 and then exec rm with the file list.

为此,我将使用最大深度为 1 的 find ,然后使用文件列表执行 rm 。

find ./dir -maxdepth 1 -type f -exec rm -rf '{}' \;

Edit: this is essentially the same as what James posted but I didn't see his post until after

编辑:这与詹姆斯发布的内容基本相同,但直到之后我才看到他的帖子

回答by Andriyun

rmwon't delete directories by default. So in your example, assuming you're in the parent directoryand those are all the files, all you need is:

rm默认情况下不会删除目录。因此,在您的示例中,假设您位于父目录中并且这些是所有文件,您只需要:

rm *

回答by Geoffrey Hale

TL;DR:

特尔;博士:

find . -maxdepth 1 -type f -delete

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

find . -type f -maxdepth 1 -delete

find: warning: you have specified the -maxdepth option after a non-option argument -type, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it). Please specify options before other arguments.

find: 警告:您在非选项参数 -type 之后指定了 -maxdepth 选项,但选项不是位置性的(-maxdepth 影响在它之前指定的测试以及在它之后指定的测试)。请在其他参数之前指定选项。