86

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?

0

5 Answers 5

137

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".

6
  • 3
    My ls -h is broken, it shows some very small folder size, just a few bytes or kb, instead of gb. Is there something wrong? I'm using Mojave. Commented Sep 5, 2019 at 15:11
  • @MinhNghĩa Are sure the files sizes in the folder you are listing with ls command are not small files ie showing correctly. I am not running that OS, still working with OSX 10.11 currently, so can not confirm issue. Be aware, Apple plans to move to zsh, rather than bash as default in their next OSX release.
    – Cam_Aust
    Commented Sep 6, 2019 at 10:07
  • Yes I use zsh, and ls -lah returns that all my folder' size are 4K :| Commented Sep 6, 2019 at 23:30
  • @MinhNghĩa Checking manual file (man) for ls under zsh does not indicate any changes to ls under zsh, though perhaps a later update of zsh does. Are you apply ls in the file directory that you are thinking you are in or another. I have run ls - ahl under zsh with the zsh install in OSX 10.11.6 and my file sizes show as they should. If you have recently started using Oh My Zsh, perhaps a conflict arises there, though I would think not likely. Do post if you find the problem and hence answer. I would be interested.
    – Cam_Aust
    Commented Sep 7, 2019 at 2:48
  • 1
    Can I suggest you post this issue as a new question on stackoverflow, rather than seek an answer here.
    – Cam_Aust
    Commented Sep 7, 2019 at 2:50
27

You don't. Unless you install GNU Coreutils.

With brew for example

brew install coreutils

and then You get gsort command that supports -h option.

3
  • 1
    NB: by default the commands are installed as g____. So gsort -h.
    – Xælias
    Commented Sep 22, 2015 at 14:12
  • I wanted this rather than the ls variants discussed so I can use with du. Eg cd ~; du -sh * | gsort -h
    – HankCa
    Commented Oct 20, 2019 at 2:24
  • This option worked for me when trying to get a human-readable format not only for individual files sizes, but also for the total listed on the first line when using the -l option.
    – cpit
    Commented Aug 2, 2022 at 21:14
12

The command line given below will list all files and directories in current directory sorted based on size (largest to smallest). The output is formatted to

ls -S -lh | awk '{print $5, $9}'
1
  • 2
    It doesn't size the contents of the directories like du does. Commented Aug 23, 2014 at 5:00
3

I just want to expand on the answers above for Mac using zsh. So the correct way is to use du -sh directory_name/.

If you use ls -lh you will likely notice that your folder/directory sizes are around 4KB. This is bc it displays the space size on the disk used to store the meta-data for that specific folder/directory, NOT of it's content.

have a nice day

1

As per the accepted answer, use ls with various options. Personally, I have the following bash aliases setup which I find extremely useful:

~/.bash_profile (or ~/.bashrc)

# list files with human-readable size including hidden files
_la='ls -lah'
alias la=$_la
# la + sort by size
alias las=$_la'S'

The _la variable provides an easy way to build the las alias by simply appending the S option. Use this pattern to build the aliases that you need.

Don't forget to save and source ~/.bash_profile (or source ~/.bashrc) to make the aliases available in your current bash session.

Not the answer you're looking for? Browse other questions tagged or ask your own question.