linux删除0字节的文件,如何在linux中删除许多0字节的文件?

I've a directory with many number of 0 byte files in it. I can't even see the files when I use the ls command. I'm using a small script to delete these files but sometimes that does not even delete these files. Here is the script:

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

i=100

while [ $i -le 999 ];do

rm -f file${i}*;

let i++;

done

Is there any other way to do this more quickly?

还有其他更快的方法吗?

9 个解决方案

#1

92

Use find combined with xargs.

使用find与xargs结合。

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

You avoid to start rm for every file.

避免为每个文件启动rm。

#2

62

With GNU's find (see comments), there is no need to use xargs :

使用GNU的find(参见注释),就不需要使用xargs:

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

#3

4

Delete all files named file... in the current directory:

删除所有命名文件…在当前目录:

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

This will still take a long time, as it starts rm for every file.

这仍然需要很长时间,因为它为每个文件启动rm。

#4

3

You can use the following command:

您可以使用以下命令:

find . -maxdepth 1 -size 0c -exec rm {} \;

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

And if are looking to delete the 0 byte files in subdirectories as well, omit -maxdepth 1 in previous command and execute.

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

#5

2

you can even use the option -delete which will delete the file.

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

from man find, -delete Delete files; true if removal succeeded.

从man查找,-delete文件;如果删除成功。

#6

1

Here is an example, trying it yourself will help this to make sense:

这里有一个例子,你自己尝试一下,会让你更有意义:

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

If you have a look at the man page for find (type man find), you will see an array of powerful options for this command.

如果查看find(类型为man find)的手册页,您将看到此命令的强大选项数组。

#7

1

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

This finds the files with size 0 in the current directory, without going into sub-directories, and deletes them.

它在当前目录中查找大小为0的文件,而不进入子目录,并删除它们。

To list the files without removing them:

列出文件而不删除:

find . -maxdepth 1 -type f -size 0

#8

0

"...sometimes that does not even delete these files" makes me think this might be something you do regularly. If so, this Perl script will remove any zero-byte regular files in your current directory. It avoids rm altogether by using a system call (unlink), and is quite fast.

“…有时甚至不删除这些文件“让我觉得这可能是你经常做的事情”。如果是,这个Perl脚本将删除当前目录中的任何零字节的常规文件。它通过使用系统调用(unlink)来避免rm,而且速度很快。

#!/usr/bin/env perl

use warnings;

use strict;

my @files = glob "* .*";

for (@files) {

next unless -e and -f;

unlink if -z;

}

#9

0

Going up a level it's worth while to figure out why the files are there. You're just treating a symptom by deleting them. What if some program is using them to lock resources? If so your deleting them could be leading to corruption.

升级一个级别,这是值得的,以找出为什么文件在那里。你只是通过删除症状来治疗症状。如果某个程序使用它们来锁定资源会怎样?如果是这样,删除它们可能会导致腐败。

lsof is one way you might figure out which processes have a handle on the empty files.

lsof是一种确定哪些进程对空文件具有句柄的方法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值