How do I recursively remove files that are less than 1MB in size from a directory?
Asked
Active
Viewed 2.7k times
5 Answers
26
This can be done with find:
find . -type f -size -1M -exec rm {} +
Note that this will recursively descend into subdirectories, and will unconditionally delete all files smaller than 1 megabyte. Be careful.
Sven Marnach
- 411
- 3
- 8
2
Just for variety and a possible (probably marginal) performance gain:
find <directory> -type f -size -1M -print0 | xargs -0 rm
Useless
- 303
-1
You can checkout this link http://ayaz.wordpress.com/2008/02/05/bash-quickly-deleting-empty-files-in-a-directory/ , it has exactly what you want.
for file in *;
do
file_size=$(du $file | awk '{print $1}');
if [ $file_size == 0 ]; then
echo "Deleting empty file $file with file size $file_size!";
echo "rm -f $file";
fi;
done
You can iterate through all the files with a for loop and then use du and awk to find the filesize like in the above example.
Steen Schütt
- 576