Using ls to Show File Size in Human-Readable FormatUsing the -h flag shows the total size of files and directories and the individual size of each file and directory in a human-readable format. You can also specify the block size for displaying the file size. By default, the file size is in bytes.
Using the ls Command –l – displays a list of files and directories in long format and shows the sizes in bytes. –h – scales file sizes and directory sizes into KB, MB, GB, or TB when the file or directory size is larger than 1024 bytes. –s – displays a list of the files and directories and shows the sizes in blocks.
don't worry we have a got a UNIX command to do that for you and command is "df" which displays the size of the file system in UNIX. You can run "df" UNIX command with the current directory or any specified directory.
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".
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.
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}'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With