Linux delete file with size 0

211,790

Solution 1

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

Solution 2

you would want to use find:

 find . -size 0 -delete

Solution 3

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.

Solution 4

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

Solution 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
Share:
211,790

Related videos on Youtube

Franz Kafka
Author by

Franz Kafka

Czech beer is awesome

Updated on March 01, 2022

Comments

  • Franz Kafka
    Franz Kafka about 2 years

    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?

  • FrankH.
    FrankH. about 13 years
    shortcut: [ -s /tmp/foo ] || rm /tmp/foo (test if size is zero, else remove). Also note the xargs is unsafe if file/directory names contain spaces; find ... -exec rm '{}' \; is safe in that situation.
  • Paul Tomblin
    Paul Tomblin about 13 years
    @Frank, you are incorrect about xargs. The '-print0` and xargs -0 corrects for the spaces.
  • C. K. Young
    C. K. Young almost 13 years
    @FrankH: Plus, even if using find -exec, always favour + over ; in cases where you can (and this is one such case).
  • Antonio
    Antonio almost 11 years
    I would add "-type f", as also directory are marked to be of size zero. The "dot" is optional.
  • Jakub M.
    Jakub M. over 10 years
    The "dot" is optional for Linux, but not optional for Mac OS
  • Antonio
    Antonio over 10 years
    @JakubM. Thanks, I edited my answer accordingly.
  • Nick
    Nick over 9 years
    Very elegant solution!
  • Yehosef
    Yehosef almost 9 years
    you can just use the -delete flag - saves an exec.
  • OnlineCop
    OnlineCop over 8 years
    Would rm -- (note the trailing -- characters) be safer here than simply rm to prevent rogue filenames? serverfault.com/questions/337082/…
  • lilydjwg
    lilydjwg about 8 years
    There is an -empty option :-)
  • Anne van Rossum
    Anne van Rossum over 7 years
    @lilydjwg Exactly, if we're allowed to use non POSIX flags, find . -empty -delete is the coolest. :-)
  • aloisdg
    aloisdg over 7 years
    You can add -maxdepth 1 for the current folder.
  • Antonio
    Antonio about 7 years
    Doesn't BSD support -delete option? freebsd.org/cgi/man.cgi?find(1)
  • Anthony
    Anthony almost 7 years
    -bash: /usr/bin/du: Argument list too long
  • user7194913
    user7194913 over 6 years
    find . -maxdepth 1 -type f -size 0 -delete This finds the empthy files in the current directory without going into sub-directories.
  • Simon Baars
    Simon Baars about 6 years
    Is there also a command for running a dry run, to see which files would get removed?
  • Paul Tomblin
    Paul Tomblin about 6 years
    @SimonBaars find /tmp -size 0 -print0 |xargs -0 echo rm
  • Colin 't Hart
    Colin 't Hart over 5 years
    @Antonio OpenBSD 6.0 at least doesn't.
  • Ruslan
    Ruslan about 5 years
    Why not simply -delete?
  • Paul Tomblin
    Paul Tomblin about 5 years
    @Ruslan because -delete is new, and my version works with every version of find produced in the last 40 years.
  • Ruslan
    Ruslan about 5 years
    Is -print0 really 40 years old and ubiquitous?
  • Paul Tomblin
    Paul Tomblin about 5 years
    @Rusian pretty sure it was in BSD 4.3, although apparently it didn't get into strict SysV Unices like early AIX.
  • jspek
    jspek about 5 years
    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
  • Antonio
    Antonio about 5 years
    @jspek, well, that depends if you have that specific use... Usually when you are after empty files you are up to kill them all. :)
  • ArendE
    ArendE about 4 years
    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!