Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get Size of directory/folder in linux?

 
Rahul Dhaware
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to get Size of directory/folder in linux?

I want to store folder size in variable in shell script.

But how can i do this?
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about "du -s DIR" where DIR the directory path. You can also use the --si flag to get a "human readable" version, or -b for the length in bytes. So something like:
[ December 12, 2008: Message edited by: Charles Lyons ]
 
Rahul Dhaware
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Charles Lyons,

I tried like

dir="/path/to/dir"
size=`du -bs "$dir"`

but then i got size = 843M .

but i can't understand why there is "."(dot).

Actually i want only 843M

like size=843M.

Please can you help me in that?
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
which should produce a result in bytes... that's the point of the -b argument. If it is still providing a result like 843M and not 843123456, then your version of du is different to mine (which is GNU coreutils v5.97). Check the manpage for your version. Unless you want the value with the k/M/G suffix?
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18092
48
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That "dot" is your directory name. In this case, it's the relative location of your current working directory. If I do a "du -bs /var/log, I should get something like:

123765 /var/log

as a result.

Here's one way to get the value as a number:

DIRSIZE=`du -bs $dir | cut -f 1`
 
Rahul Dhaware
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tim......

It's works.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic