Sort Folders By Size
Summary: Microsoft Scripting Guy, Ed Wilson, uses Windows PowerShell 3.0 to sort folders by size. Microsoft Scripting Guy, Ed Wilson, is here. It is amazing how things continue to go in circles I know I have written a script to sort folders by size many times in many different languages.
Let's say I want to get the size of each directory of a Linux file system. When I use ls -la
I don't really get the summarized size of the folders.
If I use df
I get the size of each mounted file system but that also doesn't help me. And with du
I get the size of each subdirectory and the summary of the whole file system.
But I want to have only the summarized size of each directory within the ROOT folder of the file system. Is there any command to achieve that?
Kamil Maciorowskimigrated from stackoverflow.comJul 12 '10 at 18:25
This question came from our site for professional and enthusiast programmers.
9 Answers
This does what you're looking for:
What this means: Sims 4 cheat mods.
-s
to give only the total for each command line argument.-h
for human-readable suffixes likeM
for megabytes andG
for gigabytes (optional)./*
simply expands to all directories (and files) in/
.Note: dotfiles are not included; run
shopt -s dotglob
to include those too.
Also useful is sorting by size:
Here:
Sort Folders By Size Windows 10
-h
ensures thatsort
interprets the human-readable suffixes correctly.
I often need to find the biggest directories, so to get a sorted list containing the 20 biggest dirs I do this:
In this case the sizes will be reported in megabytes.
Janne PikkarainenSort Folders By Size Powershell
Janne PikkarainenWindows 10 Sort By Size
I like to use Ncdu for that, you can use the cursor to navigate and drill down through the directory structure it works really well.
The existing answers are very helpful, maybe some beginner (like me) will find this helpful as well.
Very basic loop, but for me this was a good start for some other size related operations:
Very similar to the first answer and nearly the same result as 1.), but it took me some time to understand the difference of * to ./* if in a subdirectory:
The following du
invocation should work on BSD systems:
This isn't easy. The du
command either shows files and folders (default) or just the sizes of all items which you specify on the command line (option -s
).
To get the largest items (files and folders), sorted, with human readable sizes on Linux:
This will bury you in a ton of small files. You can get rid of them with --threshold
(1 MB in my example):
The advantage of this command is that it includes hidden dot folders (folders which start with .
).
If you really just want the folders, you need to use find
but this can be very, very slow since du
will have to scan many folders several times:
Be aware, that you can't compare directories with du
on different systems/machines without getting sure, both share the same blocksize of the filesystem. This might count if you rsync some files from a linux machine to a nas and you want to compare the synced directory on your own. You might get different results with du
because of different blocksizes..
You might also want to check out xdiskusage. Will give you the same information, but shown graphically, plus allows to drill down (very useful). There are other similar utilities for KDE and even Windows.
sleskesleskeYou could use ls
in conjunction with awk
:
The output of ls
is piped to awk
. awk
starts processing the data. Standard delimiter is space. The sum variable tot
is initialised to zero; the following statement is executed for each row/line outputted by ls
. It merely increments tot
with the size. $5
stands for fifth column (outputted by ls
). At the end we divide by (1024*1024) to sum in megabytes.
If you would convert this into a script or function (.bashrc) you can also use it to get the size of certain subsets of directories, according to filetypes.
If you want system wide information, kdirstat
may came in handy!
Not the answer you're looking for? Browse other questions tagged linuxunixconsole or ask your own question.
Comments are closed.