Linux delete file with size 0 [duplicate]
Asked Answered
A

8

216

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?

Awakening answered 29/3, 2011 at 16:39 Comment(0)
B
334

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

find {directory_location} -size 0 -print -delete

If you just want a particular file;

if [ ! -s /tmp/foo ] ; then
  rm /tmp/foo
fi
Bradney answered 29/3, 2011 at 16:41 Comment(10)
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.Trinitarianism
@Frank, you are incorrect about xargs. The '-print0` and xargs -0 corrects for the spaces.Bradney
@FrankH: Plus, even if using find -exec, always favour + over ; in cases where you can (and this is one such case).Hygeia
Would rm -- (note the trailing -- characters) be safer here than simply rm to prevent rogue filenames? serverfault.com/questions/337082/…Bougainville
Is there also a command for running a dry run, to see which files would get removed?Monitory
@SimonBaars find /tmp -size 0 -print0 |xargs -0 echo rmBradney
Why not simply -delete?Cathrin
@Cathrin because -delete is new, and my version works with every version of find produced in the last 40 years.Bradney
Is -print0 really 40 years old and ubiquitous?Cathrin
@Rusian pretty sure it was in BSD 4.3, although apparently it didn't get into strict SysV Unices like early AIX.Bradney
G
274

you would want to use find:

 find . -size 0 -delete
Garner answered 29/3, 2011 at 16:44 Comment(5)
I would add "-type f", as also directory are marked to be of size zero. The "dot" is optional.Eshelman
The "dot" is optional for Linux, but not optional for Mac OSCarriole
There is an -empty option :-)Casares
@Casares Exactly, if we're allowed to use non POSIX flags, find . -empty -delete is the coolest. :-)Outdare
You can add -maxdepth 1 for the current folder.Quantifier
E
151

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.

Eshelman answered 5/8, 2013 at 7:24 Comment(3)
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 -deleteDyane
@jspek, well, that depends if you have that specific use... Usually when you are after empty files you are up to kill them all. :)Eshelman
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!Medarda
M
18

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
Metagenesis answered 28/2, 2016 at 13:3 Comment(2)
find . -maxdepth 1 -type f -size 0 -delete This finds the empthy files in the current directory without going into sub-directories.Underwrite
To generalize this slightly, note that the value passed to -size can have a suffix indicating the units. The default is not bytes.Bursa
I
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.

Instruct answered 13/5, 2011 at 9:35 Comment(0)
M
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
Mutazilite answered 18/2, 2016 at 19:31 Comment(2)
Doesn't BSD support -delete option? freebsd.org/cgi/man.cgi?find(1)Eshelman
@Eshelman OpenBSD 6.0 at least doesn't.Burress
G
1

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

rm `du * | awk '$1 == "0" {print $2}'`
Grendel answered 2/9, 2014 at 18:27 Comment(1)
-bash: /usr/bin/du: Argument list too longDulcy
G
1
find . -type f -empty -exec rm -f {} \;
Greaseball answered 28/6, 2015 at 9:49 Comment(1)
you can just use the -delete flag - saves an exec.Argile

© 2022 - 2024 — McMap. All rights reserved.