The Windows dir
directory listing command has a line at the end showing the total amount of space taken up by the files listed. For example, dir *.exe
shows all the .exe
files in the current directory, their sizes, and the sum total of their sizes. I'd love to have similar functionality with my dir
alias in bash, but I'm not sure exactly how to go about it.
Currently, I have alias dir='ls -FaGl'
in my .bash_profile
, showing
drwxr-x---+ 24 mattdmo 4096 Mar 14 16:35 ./
drwxr-x--x. 256 root 12288 Apr 8 21:29 ../
-rw------- 1 mattdmo 13795 Apr 4 17:52 .bash_history
-rw-r--r-- 1 mattdmo 18 May 10 2012 .bash_logout
-rw-r--r-- 1 mattdmo 395 Dec 9 17:33 .bash_profile
-rw-r--r-- 1 mattdmo 176 May 10 2012 .bash_profile~
-rw-r--r-- 1 mattdmo 411 Dec 9 17:33 .bashrc
-rw-r--r-- 1 mattdmo 124 May 10 2012 .bashrc~
drwx------ 2 mattdmo 4096 Mar 24 20:03 bin/
drwxrwxr-x 2 mattdmo 4096 Mar 11 16:29 download/
for example. Taking the answers from this question:
dir | awk '{ total += $4 }; END { print total }'
which gives me the total, but doesn't print the directory listing itself. Is there a way to alter this into a one-liner or shell script so I can pass any ls
arguments I want to dir
and get a full listing plus sum total? For example, I'd like to run dir -R *.jpg *.tif
to get the listing and total size of those file types in all subdirectories. Ideally, it would be great if I could get the size of each subdirectory, but this isn't essential.
ls -lh
help you ? It prints total sum in top. You can also rundu -sh *.exe
to get disk space usage information in human readable form. – bagavadhar Apr 16 '13 at 19:40ls -lh
is printing, but it's not always related to what theawk
scripts below calculate, or what I add up by hand. Sometimes it's close to the number of KB of files in the directory, but it doesn't seem to take the allocated sizes of subdirectories into effect. I'd be grateful if you could point me toward an explanation of what exactly that number is... – MattDMo Apr 17 '13 at 17:05ls -lh
does not show the total of size of a dir calculating it's contents – aequalsb Feb 09 '17 at 14:35