Find the answer to your Linux question:
Results 1 to 10 of 10
I am trying to write a script that return the total used space for all sub directories 1 level down. What I mean is imagine the following structure: /home/bob contains ...
  1. #1
    Just Joined!
    Join Date
    Jun 2007
    Location
    Montreal
    Posts
    2

    Script returning total used space for every sub directory

    I am trying to write a script that return the total used space for all sub directories 1 level down.
    What I mean is imagine the following structure:
    /home/bob contains d1 (3mb) , d2 (1mb) and d3 (5 mb)
    home/bob/d1 contains d11 (1mb), d12 (10mb)
    home/bob/d2 does not have any subdirs
    home/bob/d3 contains d31 (35mb)

    When I run my script against home/bob, it should return this:
    d1 16mb
    d2 1mb
    d3 40mb
    total 57mb

    note 16mb for d1 is found by adding al the files in d1 (3mb) + all files in d11 (1mb) + all files in d12 (1omb)


    when run in home/bob/d1 it should return
    d11 1mb
    d12 12mb
    total 13mb



    I tried a combo of for and du but I don't seem to be able to get to anything edible.

    Any ideas ?

    Thx in advance.
    MB

  2. #2
    Super Moderator devils casper's Avatar
    Join Date
    Jun 2006
    Location
    Chandigarh, India
    Posts
    24,316
    Code:
    df -h --max-depth=1 /home/bob
    It is amazing what you can accomplish if you do not care who gets the credit.
    New Users: Read This First

  3. #3
    Linux Guru smolloy's Avatar
    Join Date
    Apr 2005
    Location
    CA, but from N.Ireland
    Posts
    2,413
    Quote Originally Posted by devils_casper View Post
    Code:
    df -h --max-depth=1 /home/bob
    Shouldn't that be du instead of df?
    Code:
    du -h --max-depth=1 /home/bob
    Registered Linux user #388328 || Registered LFS user #15880
    AMD 64 X2 4600+ :: 2X1GB DDR2 800 :: GeForce 9400 GT 512MB :: ASUS M2N32 Deluxe :: 4X250GB SATAII
    Need instant help? Try us on IRC -- #linuxforums on freenode

  4. #4
    Super Moderator devils casper's Avatar
    Join Date
    Jun 2006
    Location
    Chandigarh, India
    Posts
    24,316
    ouch.......
    you are right as usual.
    thanx again smolloy.
    It is amazing what you can accomplish if you do not care who gets the credit.
    New Users: Read This First

  5. #5
    Just Joined!
    Join Date
    Jun 2007
    Location
    Montreal
    Posts
    2
    Thanks for your replies.
    Indeed, du got the job done, df did not accept the --max-depth.
    BM

  6. #6
    Super Moderator devils casper's Avatar
    Join Date
    Jun 2006
    Location
    Chandigarh, India
    Posts
    24,316
    Indeed, du got the job done, df did not accept the --max-depth.
    df is for calculating file-system disk space usage and du is for estimate file space usage.
    i confuses in df and du sometimes..
    well ! i'll try to remember from now on..
    It is amazing what you can accomplish if you do not care who gets the credit.
    New Users: Read This First

  7. #7
    tpl
    tpl is offline
    Linux User
    Join Date
    Jan 2007
    Location
    cleveland
    Posts
    452
    Quite handy, thanks for the idea--it's in my /usr/local/bin. I think the
    report is readable. You could use awk's "length" function to replace
    the magic numbers.

    #/bin/bash
    #Dirs--says how big are files/directories
    #
    du --max-depth=1 | sed 's/\.\///g;$d' | sort -k 2 |
    awk '{a+=$1; printf "%20s\t%8d\n",$2,$1} END {printf "%20s\t%8d\n", "TOTAL",a}'
    the sun is new every day (heraclitus)

  8. #8
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by tpl View Post
    Quite handy, thanks for the idea--it's in my /usr/local/bin. I think the
    report is readable. You could use awk's "length" function to replace
    the magic numbers.

    #/bin/bash
    #Dirs--says how big are files/directories
    #
    du --max-depth=1 | sed 's/\.\///g;$d' | sort -k 2 |
    awk '{a+=$1; printf "%20s\t%8d\n",$2,$1} END {printf "%20s\t%8d\n", "TOTAL",a}'
    actually, the total will be displayed at the last line of du --max-depth output. so there's no need to do counting again.

  9. #9
    Just Joined!
    Join Date
    May 2005
    Location
    India
    Posts
    2
    Hi I want to know is there any way with bash script to get from and to id with grep command in maillog of postfix

  10. #10
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    You don't want to put this request here, precisely. You want to click the "create new thread" link. And speaking of precisely, the more details you give us, the better we can help you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...