Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How sort find result by file sizes

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.

like image 475
Daivid Avatar asked Mar 23 '14 22:03

Daivid


People also ask

Which command would you use to list all the files in a folder sorted by size?

The ls command is one of the most basic commands in Linux, and it is used to list the contents of a directory. By default, the ls command sorts files alphabetically, but you can also use it to sort files by size, by date, or by other attributes.

How do I sort by file size in Windows 10?

With right-click sort Another way to sort files in a given folder is by right-clicking on the body of the folder. A pop-up will appear. Here, click on “Sort by -> Size.” The files will be sorted accordingly.


2 Answers

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
like image 87
xav Avatar answered Oct 10 '22 20:10

xav


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

Size in MiB, path is relative.

like image 36
MetalGodwin Avatar answered Oct 10 '22 21:10

MetalGodwin