how do you find out how big your directories are... all at once!!

Discussion in 'Command Line' started by gcawood, Oct 29, 2011.

  1. gcawood

    gcawood Administrator Staff Member

    Messages:
    49
    Likes Received:
    17
    Trophy Points:
    0
    Easy! Loop through the results of `ls` and then apply a `du -hs`. See below!!

  2. gcawood

    gcawood Administrator Staff Member

    Messages:
    49
    Likes Received:
    17
    Trophy Points:
    0
    Code:
    for i in `ls`; do du -hs $i; done
    
  3. Nathan Rich

    Nathan Rich New Member

    Messages:
    4
    Likes Received:
    1
    Trophy Points:
    0
    Where to begin

    -Never parse ls. The results are meant only for human consumption and it's considered bad practices to do so.
    -When parsing commands ` ` is deprecated, $() is preferred.
    -Because you have not quoted properly, your script will break when applied to a listing where expansion becomes an issue. An obvious example of this is a file name with a space in it.
    -The script doesn't strictly show you the size of your directories, it's in fact all files including directories in your current directory.
    -Your script doesn't need to parse ls because your shell will already have a built in way to return all the files in your directory: * , which means this could be shortened to for i in *; do du -hs $i; done . However you will still suffer from improper quotes.
    -And finally, this command like most linux commands accepts multiple IFS-separated arguments already. Therefore you could shorten this script to simply: du -hs * , and that will NOT suffer from the improper quotes.

    In short:
    Code:
    du -hs *
  4. brittonstanfield

    brittonstanfield New Member

    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    If you prefer the GUI, try JDiskReport
  5. majcherek2048

    majcherek2048 New Member

    Messages:
    4
    Likes Received:
    1
    Trophy Points:
    0
    Hey thanks! I just learned something new there :)
    Do you have a source for that?
  6. ryran

    ryran New Member

    Messages:
    10
    Likes Received:
    4
    Trophy Points:
    0
  7. ryran

    ryran New Member

    Messages:
    10
    Likes Received:
    4
    Trophy Points:
    0

Share This Page