如何删除零字节文件 [英] How to remove zero byte files
问题描述
我有以下目录结构:-
foo/dir1/
foo/dir2/
foo/dir3/
foo/dir1/a.rb
foo/dir1/b.rb
foo/dir1/c.rb
foo/dir1/d.rb
foo/dir2/e.rb
foo/dir2/f.rb
foo/dir2/g.rb
foo/dir2/h.rb
如何从某个文件夹中删除零字节文件(dir1、dir2下的一些文件是零字节).如何查找和删除此类文件?
How to remove zero byte files from a certain folder (some of the files under dir1, dir2 are zero bytes). How do I find and remove such files?
推荐答案
假设您有一个与 POSIX 2008 兼容的 find
版本以支持 +
符号:
Assuming you have a version of find
compliant enough with POSIX 2008 to support the +
notation:
find foo -size 0 -exec rm -f {} +
如果你不这样做,你可以使用一些变体:
If you don't, there are variants you can use:
find foo -size 0 -print0 | xargs -0 rm -f # But you probably have + anyway
find foo -size 0 -exec rm -f {} \; # Slow but reliable
find foo -size 0 -print | xargs rm -f # Fails with spaces etc in file names
重复问题的已接受答案建议 -delete
,当您使用的 find
支持它时,这很好(因为它避免了执行的开销rm
命令通过在 find
) 中调用 unlink()
:
And the accepted answer to the duplicate question suggests -delete
, which is good when it is supported by the find
you are using (because it avoids the overhead of executing the rm
command by doing the unlink()
call inside find
):
find foo -size 0 -delete # Not POSIX standard
这篇关于如何删除零字节文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!