Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort 'find' results in bash by size

I am wondering if there's an "easy" way (through a pipe or something) to order (by file size) the results of a "find" command in bash such as:

find /location/of/directory/ -type f -size +2G
like image 799
HotDogCannon Avatar asked Oct 14 '25 08:10

HotDogCannon


2 Answers

You can use %k for example to print the size in kilobytes:

find . -type f -size +2G -printf "%kKB %p\n" | sort -n
  • By saying -printf "%kKB %p\n" you are printing the file in kilobytes and then the name.
  • sort -n gets this input and sorts it accordingly.

See an example:

$ find . -type f -size +1M -printf "%p %kKB\n" | sort -n -k2
./arrr.txt.gz 1664KB
./brrr.gz 32388KB
like image 156
fedorqui 'SO stop harming' Avatar answered Oct 17 '25 03:10

fedorqui 'SO stop harming'


Try this :

find /location/of/directory/ -type f -size +2G -exec du -s {} + |sort -n

-exec executes command du -s on each search result and and sort -n sorts the result numerically.

like image 36
RBH Avatar answered Oct 17 '25 01:10

RBH



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!