74

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?

Alex Shroyer
  • 3,499
  • 2
  • 28
  • 54

4 Answers4

120

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

Cam_Aust
  • 2,529
  • 2
  • 19
  • 28
  • 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. – Minh Nghĩa Sep 05 '19 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 Sep 06 '19 at 10:07
  • Yes I use zsh, and `ls -lah` returns that all my folder' size are 4K :| – Minh Nghĩa Sep 06 '19 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 Sep 07 '19 at 02:48
  • 1
    Can I suggest you post this issue as a new question on stackoverflow, rather than seek an answer here. – Cam_Aust Sep 07 '19 at 02:50
  • Note that the op discussed using with `du` so the size of the directories (ie. recursivly) can be shown. So the coreutils answer is better to enable you to write `du -sh * | gsort -h` – HankCa Oct 20 '19 at 02:28
28

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.

Marcin Krasowski
  • 668
  • 6
  • 12
  • 1
    NB: by default the commands are installed as g____. So gsort -h. – Xælias Sep 22 '15 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 Oct 20 '19 at 02: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 Aug 02 '22 at 21:14
10

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}'
Seema Kadavan
  • 2,538
  • 1
  • 16
  • 31
2

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

haimcqueen
  • 21
  • 2