Reputation: 3829
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?
Upvotes: 90
Views: 134869
Reputation: 635
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.
Upvotes: 1
Reputation: 51
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
Upvotes: 5
Reputation: 2799
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".
Upvotes: 144
Reputation: 2648
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}'
Upvotes: 13
Reputation: 686
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.
Upvotes: 28