• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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: 27752
196
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic