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?
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?
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
xargs
. The '-print0` and xargs -0
corrects for the spaces. –
Bradney find -exec
, always favour +
over ;
in cases where you can (and this is one such case). –
Hygeia rm --
(note the trailing --
characters) be safer here than simply rm
to prevent rogue filenames? serverfault.com/questions/337082/… –
Bougainville find /tmp -size 0 -print0 |xargs -0 echo rm
–
Bradney -delete
? –
Cathrin -delete
is new, and my version works with every version of find
produced in the last 40 years. –
Bradney -print0
really 40 years old and ubiquitous? –
Cathrin you would want to use find:
find . -size 0 -delete
-empty
option :-) –
Casares find . -empty -delete
is the coolest. :-) –
Outdare -maxdepth 1
for the current folder. –
Quantifier 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.
find . -name '*.txt' -type f -empty -delete
–
Dyane 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
-size
can have a suffix indicating the units. The default is not bytes. –
Bursa 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.
This works for plain BSD so it should be universally compatible with all flavors. Below.e.g in pwd
( .
)
find . -size 0 | xargs rm
-delete
option? freebsd.org/cgi/man.cgi?find(1) –
Eshelman For a non-recursive delete (using du and awk):
rm `du * | awk '$1 == "0" {print $2}'`
find . -type f -empty -exec rm -f {} \;
© 2022 - 2024 — McMap. All rights reserved.
[ -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. – Trinitarianism