How can I list all files and folders beyond a specified size limit?

In a partition I have several files and folders, and I can list all those file sizes with du like this:

du -h

But how can I list all the files which are beyond a specific disk space size like 5MB?


Solution 1:

find /home/stephenm/ -maxdepth 1 -size +20k -exec du -h {} \;

That should list anything over 20k in /home/stephenm

to recurse into sub directorys drop the -maxdepth 1 option.

Solution 2:

Be careful: du does not print the size of a file, it gives you an estimate of file space usage.

You can create a 10 MB file, which uses much less:

dd if=/dev/zero of=file seek=10M count=1 bs=1
stat -c %s file
ls -lh file
du -h file
cat file | wc

An example is a compressed file that uses less space on disk that its size, of like the example above a sparse file.

In the example above, find . -type f -size +5M will output file even if du is much less than 5M.

If you know you do not have newlines in your filenames, you can filter the output of find with:

find . -type f -size +5M | while IFS= read -r file; do
  du=$(du -k "$file")
  size="${du%%$(printf "\t")*}"
  if test $size -gt $((5 * 1024)); then
    echo "$file"
  fi
done

If you can have newlines in your filenames, you could use this GNU extension, but then use something else than echo in the loop:

find . -type f -size +5M -print0 | while IFS= read -r -d '' file; do
  du=$(du -k "$file")
  size="${du%%$(printf "\t")*}"
  if test $size -gt $((5 * 1024)); then
    echo "$file"
  fi
done