In Linux, how can I delete a file with a size of 0? I’d like to run this without using any other scripts in a crontab.
l filename.file | grep 5th-tab | not eq 0 | rm
Something like this?
Asked by Franz Kafka
This will delete any files with a size of zero in a directory (and below).
find /tmp -size 0 -print -delete
If you only need a single file;
if [ ! -s /tmp/foo ] ; then rm /tmp/foo fi
Answered by Paul Tomblin
You’ll want to utilize the following search terms:
find . -size 0 -delete
Answered by James.Xu
To find and delete empty files in the current directory and subdirectories, use the following commands:
find . -type f -empty -delete
Because directories are also specified as being of size zero, -type f is required.
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
Documentation can be found at GNU:
Answered by Antonio
You may accomplish this with the command locate. We can use -type f to match files, and -size 0 to match empty files. Then we can use -delete to remove the matches.
find . -type f -size 0 -delete
Answered by PYK
When you don’t need find(1) on Linux, the stat(1) command comes in handy:
(( $(stat -c %s "$filename") )) || rm "$filename"
The -c percent s option in the stat command allows us to merely acquire the file size (see the man pages for other formats). That’s the $, I’m running the stat software and recording its results ( ). This output is numerical, as indicated by the outside (()). If the size is zero, the result is FALSE, and the OR’s second portion is executed. Because non-zero (non-empty file) is TRUE, the rm command will not be executed.
Answered by cdarke
Post is based on https://stackoverflow.com/questions/5475905/linux-delete-file-with-size-0