macosterminaldu

Show human-readable file sizes in OSX terminal


In Linux, I type du -sh * | sort -rh to display the files and directories in my current directory, sorted from largest to smallest.

How do I do this in OSX terminal, preferably without installing anything extra?


Solution

  • A better answer to this question 2 years on (using Terminal in OSX 10.11) is to simply add -h to the ls command to get human readable file sizes.

    To my understanding in later OSX editions, using brew to install coreutils is not required. (If I am wrong, someone correct me.)

    For example, to get a list of files in the current directory, as a single column list, and showing file details, enter:

    ls -l
    

    If you want human readable file sizes, simply add an "h":

    ls -hl
    

    or

    ls -lh
    

    The letters after the "-" can be in any order in this instance.

    If, as the question detail states, one wants the file list to be ordered by file size, largest file size at the top, then add a capital "S":

    ls -lhS
    

    If you want invisible files listed as well then add an "a":

    ls -alhS
    

    Again the letters after the "-" can be in any order.


    If your creating and editing files in the current directory often, perhaps because you are in the process of working on a task or project, an alternative combination is:

    ls -hltur
    

    This lists files in human readable file size format, in a long list down the Terminal window.

    The "t" instructs the sort order of the files by their last modified date/times.

    The "u" then alters this slightly to use the time the files were last accessed, rather than last modified.

    The "r" then reverse the order of the list so that the more recently accessed or 'touched' files are listed last, at the bottom of the list.

    The full combination means that you have a detailed list, with the files that you have read, opened or modified most recently, or been 'touched' similarly by a process you have run or another process, all at the bottom of the list.

    Hence even if the list in your current directory is long such that the beginning of the list can no longer be read without scrolling up, the files you have interacted with will likely remain visible immediately above your next, ready to type in, command line.

    These options, and more, are in the man (manual) page for the ls command:

    man ls
    

    If you want to list files regularly in one of the above formats, or another of your choice upon reading the man page, you can add an alias to your .bash_profile file, (eg using nano to open this file, and doing so while you are in your home directory).

    For example, to fulfill that desired by the original poster of the question, open the file and on a fresh line add:

    alias lss='ls -hlS'
    

    Then, upon saving the file and exiting that Terminal window and opening a new Terminal window, typing "lss" in the command line should provide that which you seek routinely when listing files.

    If you do not know how to use nano, first bring up its man(ual) page by typing

    man nano
    

    The man page will explain how to use nano.

    To exit a man page and get back to where you can enter a command, press the key "q".