How sort find result by file sizes

感情迁移 提交于 2019-12-23 09:36:50

问题


How can I sort by file size the results of the find command?

I trying to sort the result of this find command:

find ./src -type f -print0

I don't want the size of directories, I need the files relative paths sorted by size only.


回答1:


Here is how to do using find command:

find . -type f -exec ls -al {} \; | sort -k 5 -n | sed 's/ \+/\t/g' | cut -f 9

Here is how to do using recursive ls command:

ls -lSR | sort -k 5 -n

Or, if you want to display only file names:

ls -lSR | sort -k 5 -n | sed 's/ \+/\t/g' | cut -f 9



回答2:


find -type f -exec du -sm {} \; | sort -nk1

Size in MiB, path is relative.




回答3:


Parsing ls or variations thereof will add unnecesary complexity.

Sorted file paths by file size:

find src -type -printf '%s\t%p\n' | sort -n | cut -f2-

Notes:

  • Change sort -n to sort -nr to get reverse order
  • The question had -print0 but catering for file names containing newlines seem pedantic.

  • The question mentioned relative paths, and changing %p to %P will get relative paths under src/



来源:https://stackoverflow.com/questions/22598205/how-sort-find-result-by-file-sizes

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