215

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?

dreftymac
  • 31,404
  • 26
  • 119
  • 182
Franz Kafka
  • 10,623
  • 20
  • 93
  • 149

8 Answers8

332

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

find /tmp -size 0 -print -delete

If you just want a particular file;

if [ ! -s /tmp/foo ] ; then
  rm /tmp/foo
fi
4wk_
  • 2,458
  • 3
  • 34
  • 46
Paul Tomblin
  • 179,021
  • 58
  • 319
  • 408
273

you would want to use find:

 find . -size 0 -delete
Antonio
  • 19,451
  • 13
  • 99
  • 197
James.Xu
  • 8,249
  • 5
  • 25
  • 36
150

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.

Antonio
  • 19,451
  • 13
  • 99
  • 197
  • 2
    I would add -name '*.SomeFileExtension' for example: if you wanted to delete just text files then I would use: `find . -name '*.txt' -type f -empty -delete` – jspek Apr 18 '19 at 16:30
  • @jspek, well, that depends if you have that specific use... Usually when you are after empty files you are up to kill them all. :) – Antonio Apr 18 '19 at 20:45
  • 2
    Had to grab a coffee after running this command on a directory with 2.2 million files. :P Had worked like a charm when I came back, 350.000 remained. Thanks! – ArendE Feb 26 '20 at 19:42
17

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
David Maust
  • 8,080
  • 3
  • 32
  • 36
PYK
  • 3,674
  • 29
  • 17
  • 1
    find . -maxdepth 1 -type f -size 0 -delete This finds the empthy files in the current directory without going into sub-directories. – user7194913 Jan 12 '18 at 08:56
5

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

find . -size 0 |  xargs rm
user1874594
  • 2,277
  • 1
  • 25
  • 49
5

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.

cdarke
  • 42,728
  • 8
  • 80
  • 84
1

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

rm `du * | awk '$1 == "0" {print $2}'`
Harrison
  • 830
  • 1
  • 15
  • 28
1
find . -type f -empty -exec rm -f {} \;
josliber
  • 43,891
  • 12
  • 98
  • 133