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 999 times
2
Jeff Schaller
- 66,199
- 35
- 114
- 250
griffin_cosgrove
- 123
- 4
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
- 19,242
- 1
- 50
- 94
-
Brilliant! ```find . -maxdepth 2 -type f -size +1G -exec du -h {} + | sort -rh``` works as expected. Are there any implications I should be aware of when running ```find ... -exec``` instead of ```find … | xargs …```? New to this area, appreciate any knowledge. – griffin_cosgrove Dec 07 '20 at 17:02
-
1@griffin_cosgrove I find `find -exec` more straightforward than `find … | xargs …` because `xargs` has its options and quirks. Read good answers [here](https://unix.stackexchange.com/q/389705/108618) and [here](https://unix.stackexchange.com/q/156008/108618). – Kamil Maciorowski Dec 07 '20 at 17:44
-
thanks! this has been helpful – griffin_cosgrove Dec 17 '20 at 23:35