问题
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 -ntosort -nrto get reverse order The question had
-print0but catering for file names containing newlines seem pedantic.The question mentioned relative paths, and changing
%pto%Pwill get relative paths undersrc/
来源:https://stackoverflow.com/questions/22598205/how-sort-find-result-by-file-sizes