The Linux command du stands for disk usage which is used to check the amount of disk storage any particular directory or file is using. By default, the simple command:
du
Would return the disk usage in God-knows-what-unit of each of the directories in the current working directory and those beneath them — in a recursive manner. If you happen to have lots of them, the returned stats would be scrolling down crazily which barely makes it any useful.
Even if you have specified a specific directory such as "somedir":
du somedir
It still works in this uncomfortable way.
The solution is to use the -sh
switch, the one switch a beginner will ever need:
du -sh
Which simply returns the amount of disk space the current directory and all those stuff in it are using as a whole, something like:
2.4G
Much much more intuitive and readable.
By:
du -sh somedir
You can find out how much disk storage directory "somedir" is using:
101M somedir
To get all the subsequent / child directories disk usage from the current directory, simply use the asterisk:
du -sh *
It will then list the disk usage of all of them (but not recursively) one by one in a very readable manner:
8.0K dir1
1.4G dir2
135M dir3
This can be a useful thing to web administrators.
I have a website and I want to check how much a folder /upload/ is consuming space. This folder contains, mostly, images of hotels etc.
I would use your “du” command this way, please comment if I am wrong,
du -sh /public_html/upload/*
Since I do not have a direct shell access, I would put it into a php file and then open that php in the browser.
I think that would do. 🙂
how to get the list of top space consuming directories?
@pradebban Excellent question. I’ve seen it asked all over the net. The best answer I’ve ever seen is as follows:
du –max-depth=0 -k * | sort -nr | awk ‘{ if($1>=1024*1024) {size=$1/1024/1024; unit=”G”} else if($1>=1024) {size=$1/1024; unit=”M”} else {size=$1; unit=”K”}; if(size<10) format="%.1f%s"; else format="%.0f%s"; res=sprintf(format,size,unit); printf "%-8s %s\n",res,$2 }'
Hey Nick et al, that was a great du/awk combo syntax you posted. I had to rewrite it a bit since the syntax characters int he browser got changed or something to that extent:
du -max-depth=0 -k *|sort -nr|awk ‘{if($1>=1024*1024) {size=$1/1024/1024; unit=”G”} else if($1>=1024) {size=$1/1024; unit=”M”} else {size=$1; unit=”K”}; if(size<10) format="%.1f%s"; else format="%.0f%s"; res-sprintf(format,size,unit); print "%-8s %s\n",res,$2}'
Out of that I got the following output when I ran the command against my /home/ dir:
%-8s %s
VirtualBox
%-8s %s
ISO
%-8s %s
Downloads
…….etc
Are these "%-8s %s" supposed to be printed out like that ?
Thanks,
-Roman
I couldn’t get the du * command to list all the directories (just gave me files) on Ubuntu 12.04 so I made a quick mod using a find loop:
while IFS= read -r -d ” file; do sudo du –max-depth=0 -k $file; done < =1024*1024) {size=$1/1024/1024; unit=”G”} else if($1>=1024) {size=$1/1024; unit=”M”} else {size=$1; unit=”K”}; if(size<10) format="%.1f%s"; else format="%.0f%s"; res=sprintf(format,size,unit); printf "%-8s %s\n",res,$2 }'
Change “find ~/” to the directory of your choice and remove the “-type d” directive to include files as well. Note that I used sudo as I had a few folders throw permission errors.