Linux delete file with size 0 [duplicate]

2019-01-20 23:40发布

This question already has an answer here:

How do I delete a certain file in linux if its size is 0. I want to execute this in an crontab without any extra script.

l filename.file | grep 5th-tab | not eq 0 | rm

Something like this?

8条回答
小情绪 Triste *
2楼-- · 2019-01-20 23:52

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

rm `du * | awk '$1 == "0" {print $2}'`
查看更多
一夜七次
3楼-- · 2019-01-20 23:55

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.

查看更多
唯我独甜
4楼-- · 2019-01-20 23:56

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

find . -size 0 |  xargs rm
查看更多
乱世女痞
5楼-- · 2019-01-20 23:57

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
查看更多
成全新的幸福
6楼-- · 2019-01-21 00:00
find . -type f -empty -exec rm -f {} \;
查看更多
女痞
7楼-- · 2019-01-21 00:04

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
查看更多
登录 后发表回答