2

I am trying to find a command that displays files larger than 1 GB and displays those files ordered by size. I have tried find . -maxdepth 2 -type f -size +1G -print0 |xargs -0 du -h |sort -rh but for some reason this displays files of size that are not greater than 1 GB. For example this is in the output 1.0K ./<repo>/.git/info

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255

1 Answers1

7

There are at least two possible causes:

  1. Maybe your find prints nothing. In this case xargs runs du -h which is equivalent to du -h .. Investigate --no-run-if-empty option of GNU xargs. Or better get used to find … -exec … instead of find … | xargs …. Like this:

    find . -maxdepth 2 -type f -size +1G -exec du -h {} + | sort -rh
    
  2. find -size tests (almost) what du --apparent-size shows, while du without this option may greatly disagree, especially when the file is sparse. The option is not portable.

I think in your case the first cause is the culprit. Note ./<repo>/.git/info couldn't come from find . -maxdepth 2 -type f because its depth is 3. This means du operated recursively on some directory.