linux上传文件为0字节,如何在Linux中删除多个0字节文件?

如何在Linux中删除多个0字节文件?

我有一个目录,其中包含许多0字节文件。 使用ls命令时,我什至看不到文件。 我正在使用一个小的脚本来删除这些文件,但有时甚至不删除这些文件。 这是脚本:

i=100

while [ $i -le 999 ];do

rm -f file${i}*;

let i++;

done

还有其他方法可以更快地执行此操作吗?

10个解决方案

116 votes

将rm与xargs结合使用。

find . -name 'file*' -size 0 -print0 | xargs -0 rm

您避免为每个文件启动rm。

Didier Trosset answered 2019-12-29T05:32:18Z

76 votes

使用GNU的find(请参见注释),无需使用xargs:

find -name 'file*' -size 0 -delete

coredump answered 2019-12-29T05:32:38Z

6 votes

您可以使用以下命令:

找 。 -maxdepth 1 -size 0c -exec rm {} \;

而且,如果还要删除子目录中的0字节文件,请在前面的命令中省略-maxdepth 1并执行。

jitendra answered 2019-12-29T05:33:07Z

4 votes

删除当前目录中所有名为file ...的文件:

find . -name file* -maxdepth 1 -exec rm {} \;

这仍然需要很长时间,因为每个文件都从rm开始。

Sjoerd answered 2019-12-29T05:33:31Z

4 votes

find . -maxdepth 1 -type f -size 0 -delete

这将在当前目录中找到大小为0的文件,而无需进入子目录,并将其删除。

要列出文件而不删除它们:

find . -maxdepth 1 -type f -size 0

user7194913 answered 2019-12-29T05:33:56Z

4 votes

如果要查找并删除文件夹中的所有0字节文件:

find /path/to/folder -size 0 -delete

Lão Còi answered 2019-12-29T05:34:16Z

2 votes

您甚至可以使用-delete选项删除文件。

从人发现,  -删除               删除文件; 如果删除成功,则返回true。

thegeek answered 2019-12-29T05:34:40Z

1 votes

这是一个示例,亲自尝试将有助于理解这一点:

bash-2.05b$ touch empty1 empty2 empty3

bash-2.05b$ cat > fileWithData1

Data Here

bash-2.05b$ ls -l

total 0

-rw-rw-r-- 1 user group 0 Jul 1 12:51 empty1

-rw-rw-r-- 1 user group 0 Jul 1 12:51 empty2

-rw-rw-r-- 1 user group 0 Jul 1 12:51 empty3

-rw-rw-r-- 1 user group 10 Jul 1 12:51 fileWithData1

bash-2.05b$ find . -size 0 -exec rm {} \;

bash-2.05b$ ls -l

total 0

-rw-rw-r-- 1 user group 10 Jul 1 12:51 fileWithData1

如果查看find的手册页(类型man find),则将看到此命令的一系列强大选项。

Noel M answered 2019-12-29T05:35:05Z

0 votes

“ ...有时甚至不删除这些文件”使我认为这可能是您定期执行的操作。 如果是这样,此Perl脚本将删除当前目录中的所有零字节常规文件。 它通过使用系统调用(取消链接)完全避免了rm,并且速度非常快。

#!/usr/bin/env perl

use warnings;

use strict;

my @files = glob "* .*";

for (@files) {

next unless -e and -f;

unlink if -z;

}

andereld answered 2019-12-29T05:35:26Z

0 votes

值得一提的是弄清楚为什么文件在那里。 您只是通过删除症状来对待它们。 如果某些程序正在使用它们锁定资源怎么办? 如果是这样,则删除它们可能会导致损坏。

lsof是一种方法,您可以找出哪些进程在空文件上具有句柄。

Paul Rubel answered 2019-12-29T05:35:50Z

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值