How to sort 'find' results in bash by size

南笙酒味 提交于 2020-01-21 12:24:27

问题


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

回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/27189663/how-to-sort-find-results-in-bash-by-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!