Home » Questions » Computers [ Ask a new question ]

Sort All Files (any depth) within a Folder by Size

Sort All Files (any depth) within a Folder by Size

I have a folder full of folders and files. I want to sort files by size (so I could remove the largest files).

Asked by: Guest | Views: 53
Total answers/comments: 2
Guest [Entry]

"Open Terminal, cd to the folder you want to analyze and use this command:

find . -type f -print0 | xargs -0 ls -l | sort -k5,5rn

It should print a list of all files in the hierarchy, sorted by size. At least on my machine, which is not a Mac, but some other Unix. But in principal it should be roughly the same.

Thanks to Richard Hoskins for the bug with the spaces in the names. That's actually a feature in xargs. See this site where it's explained quite nicely. Above version should work now.


Here is an explanation how the command works:

find . ==> find items from current working directory "".""

-type f ==> search for regular files

-print0 ==> print full file name to standard out, ending with a null character, instead of newline (this is for handling filenames with newlines and white space by xargs)

xargs ==> execute command xargs (executes a command for every line in standard in)

-0 ==> line delimiter is null character

ls -l ==> the command for xargs to execute. This way we get the details especially the size of the files.

sort ==> sort lines in standard in

-k5,5rn ==> sort field definition, begin at field 5 (delimiter default is blank) and end at field 5. That's the size field in ls -l display. r stands for reverse sort order, so that the biggest files are on top and n stands for numerical sort order."
Guest [Entry]

"open Finder
go to folder
Finder > File > Find...
in Search change selection from This Mac to ""folder name""
click Kind
select Other and then Size
click button OK
(solution to my problem is size greater than 1 MB)"