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
Asked
Active
Viewed 1,569 times
2
Jeff Schaller
- 67,283
- 35
- 116
- 255
griffin_cosgrove
- 123
- 5
1 Answers
7
There are at least two possible causes:
Maybe your
findprints nothing. In this casexargsrunsdu -hwhich is equivalent todu -h .. Investigate--no-run-if-emptyoption of GNUxargs. Or better get used tofind … -exec …instead offind … | xargs …. Like this:find . -maxdepth 2 -type f -size +1G -exec du -h {} + | sort -rhfind -sizetests (almost) whatdu --apparent-sizeshows, whileduwithout 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.
Kamil Maciorowski
- 21,864
find . -maxdepth 2 -type f -size +1G -exec du -h {} + | sort -rhworks as expected. Are there any implications I should be aware of when runningfind ... -execinstead offind … | xargs …? New to this area, appreciate any knowledge. – griffin_cosgrove Dec 07 '20 at 17:02find -execmore straightforward thanfind … | xargs …becausexargshas its options and quirks. Read good answers here and here. – Kamil Maciorowski Dec 07 '20 at 17:44