Is there a simple command to display the total aggregate size (disk usage) of all files in a directory (folder)?
I have tried these, and they don't do what I want:
ls -l
, which only displays the size of the individual files in a directory, nordf -h
, which only displays the free and used space on my disks.
The command
du
"summarizes disk usage of each FILE, recursively for directories," e.g.,-h
is to get the numbers "human readable", e.g. get140M
instead of143260
(size in KBytes)-s
is for summary (otherwise you'll get not only the size of the folder but also for everything in the folder separately)As you're using
-h
you can sort the human readable values usingThe
-h
flag onsort
will consider "Human Readable" size values.If want to avoid recursively listing all files and directories, you can supply the
--max-depth
parameter to limit how many items are displayed. Most commonly,--max-depth=1
Recently I found a great, ncurses based interactive tool, that quickly gives you an overview about directory sizes. Searched for that kind of tool for years.
Think of it as baobab for the command line:
This finds the size recursively and puts it next to each folder name, along with total size at the bottom, all in the human format
Enjoy!
More information on that command here
Below is what I am using to print total, folder, and file size:
Details
Output
tree
is another useful command for this job:Just install it via
sudo apt-get install tree
and type the following:From man tree:
The answers have made it obvious that
du
is the tool to find the total size of a directory. However, there are a couple of factors to consider:Occasionally,
du
output can be misleading because it reports the space allocated by the filesystem, which may be different from the sum of the sizes of the individual files. Typically the filesystem will allocate 4096 bytes for a file even if you stored just one character in it!Output differences due to power of 2 and power of 10 units. The
-h
switch todu
divides the number of bytes by 2^10 (1024), 2^20 (1048576) etc to give a human readable output. Many people might be more habituated to seeing powers of 10 (e.g. 1K = 1000, 1M = 1000000) and be surprised by the result.To find the total sum of sizes of all files in a directory, in bytes, do:
Example:
You can use the tool Dust:
My example is from Windows, but Linux and Apple are also supported:
https://github.com/bootandy/dust
To see the sizes of all files and directories, use
(maybe like "do you had 1")
-h
: human readable sizes-a
: show files, not just directories-d1
: show totals only at depth 1, i.e. the current directory's contentsFor the current directory, the directory argument can be left off.
du -sh dir/*
has the same effect but doesn't show hidden files and directories due to shell globbing.I'm conditioned to the
ll
command which is aliased tols -alF
. It is just missing a file count and size of files at the bottom. I played withdu
andtree
but could not get the totals I needed. So I createdlll
to do that for me.In your
~/.bashrc
place the following:Save the file and resource it using
. ~/.bashrc
(or you can restart your terminal).Sample output
The nice thing about
ll
output is it's colors. This is maintained withlll
but lost when usingfind
ordu
:TL;DR
A bonus function you can add to
~/.bashrc
is calledBytesToHuman()
. This does what most console users would expect converting large numbers to MiB, GiB, etc:Next flip the comment between two lines in
lll ()
function to look like this:Now your output looks like this:
As always don't forget to re-source with
. ~/.bashrc
whenever making changes. (Or restart the terminal of course)PS - Two weeks in self-quarantine finally gave me time to work on this five year old goal.