Linux delete file with size 0
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
Related videos on Youtube
Comments
-
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. about 13 yearsshortcut:
[ -s /tmp/foo ] || rm /tmp/foo
(test if size is zero, else remove). Also note thexargs
is unsafe if file/directory names contain spaces;find ... -exec rm '{}' \;
is safe in that situation. -
Paul Tomblin about 13 years@Frank, you are incorrect about
xargs
. The '-print0` andxargs -0
corrects for the spaces. -
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 almost 11 yearsI would add "-type f", as also directory are marked to be of size zero. The "dot" is optional.
-
Jakub M. over 10 yearsThe "dot" is optional for Linux, but not optional for Mac OS
-
Antonio over 10 years@JakubM. Thanks, I edited my answer accordingly.
-
Nick over 9 yearsVery elegant solution!
-
Yehosef almost 9 yearsyou can just use the -delete flag - saves an exec.
-
OnlineCop over 8 yearsWould
rm --
(note the trailing--
characters) be safer here than simplyrm
to prevent rogue filenames? serverfault.com/questions/337082/… -
lilydjwg about 8 yearsThere is an
-empty
option :-) -
Anne van Rossum over 7 years@lilydjwg Exactly, if we're allowed to use non POSIX flags,
find . -empty -delete
is the coolest. :-) -
aloisdg over 7 yearsYou can add
-maxdepth 1
for the current folder. -
Antonio about 7 yearsDoesn't BSD support
-delete
option? freebsd.org/cgi/man.cgi?find(1) -
Anthony almost 7 years-bash: /usr/bin/du: Argument list too long
-
user7194913 over 6 yearsfind . -maxdepth 1 -type f -size 0 -delete This finds the empthy files in the current directory without going into sub-directories.
-
Simon Baars about 6 yearsIs there also a command for running a dry run, to see which files would get removed?
-
Paul Tomblin about 6 years@SimonBaars
find /tmp -size 0 -print0 |xargs -0 echo rm
-
Colin 't Hart over 5 years@Antonio OpenBSD 6.0 at least doesn't.
-
Ruslan about 5 yearsWhy not simply
-delete
? -
Paul Tomblin about 5 years@Ruslan because
-delete
is new, and my version works with every version offind
produced in the last 40 years. -
Ruslan about 5 yearsIs
-print0
really 40 years old and ubiquitous? -
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 about 5 yearsI 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 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 about 4 yearsHad 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!