Linux delete file with size 0 [duplicate]

放肆的年华 提交于 2019-11-28 15:11:55

This will delete all the files in a directory (and below) that are size zero.

find /tmp -size  0 -print0 |xargs -0 rm --

If you just want a particular file;

if [ ! -s /tmp/foo ] ; then
  rm /tmp/foo
fi
James.Xu

you would want to use find:

 find . -size 0 -delete

To search and delete empty files in the current directory and subdirectories:

find . -type f -empty -delete

-type f is necessary because also directories are marked to be of size zero.


The dot . (current directory) is the starting search directory. If you have GNU find (e.g. not Mac OS), you can omit it in this case:

find -type f -empty -delete

From GNU find documentation:

If no files to search are specified, the current directory (.) is used.

PYK

You can use the command find to do this. We can match files with -type f, and match empty files using -size 0. Then we can delete the matches with -delete.

find . -type f -size 0 -delete

This works for plain BSD so it should be universally compatible with all flavors. Below.e.g in pwd ( . )

find . -size 0 |  xargs rm

On Linux, the stat(1) command is useful when you don't need find(1):

(( $(stat -c %s "$filename") )) || rm "$filename"

The stat command here allows us just to get the file size, that's the -c %s (see the man pages for other formats). I am running the stat program and capturing its output, that's the $( ). This output is seen numerically, that's the outer (( )). If zero is given for the size, that is FALSE, so the second part of the OR is executed. Non-zero (non-empty file) will be TRUE, so the rm will not be executed.

For a non-recursive delete (using du and awk):

rm `du * | awk '$1 == "0" {print $2}'`
Diệu Thu Lê
find . -type f -empty -exec rm -f {} \;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!