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

08
2013-08
  • Željko Filipin

    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?

  • Answers
  • dertoni

    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.

  • 8088

    Assuming it's just a folder, open the folder in Finder. Then click the "Detail" view button. Finally, click the "Size" column. The largest files will sort to the top. Additionally, folders within this folder are also sorted by size -- simply click the triangle next to them, and you will see their files sorted as well. (Don't "open" the folder by double-clicking, just click the triangle to expand their contents.)

    Screen Shot

    If you wanted to do a search, you can search by file size. Go to the File menu, and choose Find. Click the list that starts out saying "Kind," and choose "Other." Select Size from the list that comes up. Enter your criteria and search. However, I cannot find a way to make the search results display the file size column, so it's easier to use that to, say, find files greater than a certain size than to actually sort by size.

  • Željko Filipin
    • 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)
  • 8088

    For cleaning my disk of big files, I prefer simply using Grand Perspective:

    cool looking and useful

  • Lake
    • Navigate to the folder in the .
    • Momentarily enter anything in the search field on the toolbar.
      • search field
    • Once you have the search mode active you can delete what you entered to avoid filtering on that text.
    • Click the (+) icon at the right to add a search criteria.
    • Select File size, is greater than, and whatever minimum threshold works for you.
      • Zero (0) is an option, but enough results may make things slow.
    • You may need to right-click (control-click, two-finger click, etc) on the column headers to √ the Size column.

    It will look something like this: files over 1 GB


  • Related Question

    osx - Permanently deleting files on Mac OS
  • Jonik

    A while back, as relatively new Mac OS X user, I was surprised to learn that you cannot easily delete files. Directly, that is, without moving them to the trash first. On Windows and Linux this can obviously be done with ease, but not so on the Mac.

    I noticed this when trying clear up files from a USB memory stick — removing the files ("move to trash") does not free up space; that happens only after emptying the whole system-wide Trash. Not particularly convenient! (It seems stupid to have to empty the whole trashcan just to make some space on the USB stick. There might be gigabytes of stuff in there, and this sort of defeats its purpose - what if you'd actually need to restore something from the trash some day.)

    So, what's your way of getting around this? Have you bought a 3rd party application like RAW Trash for $16.95 just to delete files, or do you diligently empty the trashcan whenever needed? Or did I miss something? Also, can you convince me that this is actually the way it should be — that users shouldn't be able to fiddle with the filesystem easily? :)


  • Related Answers
  • Daan

    I'm not so happy with the way Mac OS X handles this either. If I really want to delete something, especially from USB memory sticks, I usually fire up Terminal and rm the files manually.

  • jtbandes

    Since so many people seem to want it, I just created a Service to do this, for Snow Leopard, called Delete Immediately. It shows up in the Services menu and the context menu in Finder.

    You can download it from GitHub. Uncompress the .zip archive and move the resulting "Delete Immediately.service" to the Services folder in your library folder, ~/Library/Services/. You may need to log out and back in (and/or enable it in the Services section of the Keyboard preferences) for the system to recognize the service.

    I'd also suggest checking out the README file on the main page on GitHub. And if you want to contribute to the code, localize it, or make an icon, feel free!

    Context menu

    Services menu

  • Arjan

    The Terminal-less way of solving this conundrum is to cast the following spell to appease the Apple gods, like so :

    Select the file to be sacrificed followed by reciting:

    ⌘ Delete

    ⌘ ⇧ ⌥ ⌫

    This will send the file to Trash, and then delete the whole Trash, thus should send the victim (and all other trashed items) to oblivion.

  • Andrew J. Brehm

    I use rm -rf in the command line. It deletes a lot faster and it deletes everything. A common problem I have with Windows is that deleting is a process rather than a point in time and I often find myself watching Windows delete folders and files for several minutes. Rm -rf is quick.

    You can also use rm -rf to delete a specific Trash. The trash can is a hidden folder named .Trash in the root of the relevant volume or directory.

    And yes, I think this is how it should be. Users shouldn't be able to delete files too quickly using Finder. Files should be recoverable from the Trash.

  • avstrallen

    Part of the beauty of Mac OS X is that while it's dead easy for inexperienced users to find their way around and get stuff done, power users can "fiddle with the system" by using the power of the underlying Unix via a command shell (i.e. Terminal.app).

    @Leauki is right about using the unix command rm, but BE VERY CAREFUL, particularly if using the -rf flags! There's no safety-net and you can do serious damage, up to and including deleting your own root filesystem!

    My recommendation is not to use absolute paths with rm, but to cd into the diretory you want to work with first, and then for example:

    rm -rf ./<subpath to file to delete> where the ./ in the path forces rm to operate only in the directory you're currently in.

  • dbr

    To delete a specific file, without going through the trash..

    • open a Terminal
    • type rm (with a trailing space), or rm -r if you plan to remove a directory
    • drag and drop the file onto the Terminal window, which enters the full path to the dropped file
    • hit enter

    jtbandes's "Delete Immediately" service solution is much more elegant, but the "Terminal way" doesn't require any additional software (so is good when working on machines that you don't regularly use)

  • fatboy

    I came up with the following applescript, which I bound to shift del with Keyboard Maestro, so it now works as in windows. With the item selected in Finder, shift del will run the script, which displays a dialog warning of the permanent deletion of the item with its name. Clicking OK completes the delete.

    I don't know much about shell commands, and some of the previous posters warned of the danger of using rm -rf, which is something I have done in the script. It seems to work as predicted for me so far though.

    tell application "Finder"
        set myPosixPath to selection as text --returns an alias path
        set myPosixPath to POSIX path of myPosixPath --set it to posix style path with backslashes
    
        --identify whether it's a file or folder. Only for the warning dialog.
        if character -1 of myPosixPath is "/" then
            set itemType to "folder"
        else
            set itemType to "file"
        end if
    
        --display a warning
        display dialog myPosixPath & "
    
    This cannot be undone." with title "Really permanently delete this " & itemType & "?"
    do shell script "rm -rf " & quoted form of myPosixPath --execute a shell script to delete the item
    end tell
    
  • Bruce McLeod

    I think that this is done for user consistency, and safety. I think that is better to keep files for longer than required and use space instead of being gung-ho and deleting them straight away.

    This is a bit annoying though when you are working with USB sticks and you move them to a windows machine and go ... where is all my space ... and what is this annoying .trash folder.

  • neoneye

    Use muCommander for deleting files (it's free)

    Instead of using the Finder then perhaps use another file manager such as muCommander. It can delete files and move files to the trashcan as well. It's much safer than pasting "rm -rf" into a Terminal, where you risk pasting the wrong commands causing damages that are irreversible.