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

file managementfindermacos

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

I know how to do that in Windows Explorer, but I can not find a way to do it in Mac OS X Finder.

Windows 2003:

  • open folder in Windows Explorer
  • click button Search
  • leave Search for files or folders named and Containing text text fields empty
  • click button Search Now
  • sort by size

Is there a way to do something like this in Finder on Mac OS X?

Best Answer

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.

Edit

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.

Related Question