如何在linux中删除许多0字节文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3157343/
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
How to delete many 0 byte files in linux?
提问by small_ticket
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?
有没有其他方法可以更快地做到这一点?
采纳答案by Didier Trosset
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
为每个文件启动。
回答by Sjoerd
Delete all files named file... in the current directory:
删除当前目录下所有名为 file... 的文件:
find . -name file* -maxdepth 1 -exec rm {} \;
This will still take a long time, as it starts rm
for every file.
这仍然需要很长时间,因为它会rm
为每个文件启动。
回答by Noel M
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 (type man find
)的手册页,您将看到此命令的一系列强大选项。
回答by jitendra
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
在前面的命令中省略并执行。
回答by coredump
With GNU's find
(see comments), there is no need to use xargs :
使用 GNU find
(见评论),无需使用 xargs :
find -name 'file*' -size 0 -delete
回答by andereld
"...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 脚本将删除当前目录中的所有零字节常规文件。它通过使用系统调用(取消链接)完全避免了 rm,并且速度非常快。
#!/usr/bin/env perl
use warnings;
use strict;
my @files = glob "* .*";
for (@files) {
next unless -e and -f;
unlink if -z;
}
回答by Paul Rubel
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 是您可以确定哪些进程对空文件具有句柄的一种方法。
回答by thegeek
you can even use the option -delete which will delete the file.
您甚至可以使用选项 -delete 来删除文件。
from man find, -delete Delete files; true if removal succeeded.
from man find, -delete 删除文件;如果删除成功,则为 true。
回答by user7194913
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
回答by Giang Nguyen
If you want to find and remove all 0-byte files in a folder:
如果要查找并删除文件夹中的所有 0 字节文件:
find /path/to/folder -size 0 -delete