Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

How to get Size of directory/folder in linux?

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Author
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Saloon Keeper
Posts: 25467
180
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Tim......

It's works.
 
Your mother is a hamster and your father smells of tiny ads!
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic