Results 1 to 5 of 5

Thread: total size of all files on a partition, easy way to get this in bash script?

  1. #1

    total size of all files on a partition, easy way to get this in bash script?

    I need to get the total size of all files on a partition into a bash script. It doesn't have to be a pure number and it doesn't have to be exact, merely accurate to the last significant digit shown. For instance, it could be a string like "2.1 giB", with the unit being appropriate to the size being reported.

    The best approach I've been able to come up with is to use something like
    Code:
    df --total -h /dev/sda1
    but that puts out a table 3 rows by six columns. I suppose I could figure out how to massage that output with some of the string processing commands like awk or sed. I confess I always contemplate useing those commands with a certain amount of dread but perhaps that means I need more practice.

    Is there some easier way? A command that will spit out something closer to what I want without embedding it in a lot of stuff I don't?

  2. #2
    Join Date
    Mar 2010
    Location
    Metro-ATL; PM free zone.
    Beans
    Hidden!
    Distro
    Lubuntu 14.04 Trusty Tahr

    Re: total size of all files on a partition, easy way to get this in bash script?

    df -h /dev/part.... | cut -f ....

  3. #3
    Join Date
    Apr 2012
    Beans
    6,119

    Re: total size of all files on a partition, easy way to get this in bash script?

    Don't dread awk - could be as simple as

    Code:
    $ df -h | awk '$1 ~ /sda6/ {print $3}'
    12G
    or (if you need to run it for variable block devices) you can pass parameters in like

    Code:
    $ blockdev="sda6"
    $ df -h | awk -v blockdev="$blockdev" '$1 ~ blockdev {print $3}'
    12G

  4. #4
    Join Date
    Nov 2008
    Location
    Metro Boston
    Beans
    10,094
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: total size of all files on a partition, easy way to get this in bash script?

    If you are willing to mount the partition temporarily, say to /mnt, you can use du like this
    Code:
    sudo du -s /mnt/*
    You'll get a list of directories on the partition along with total size of the files in each directory.
    If you ask for help, please have the courtesy to check for responses and thank the people who helped you.

    Blog · Linode System Administration Guides · Android Apps for Ubuntu Users

  5. #5

    Re: total size of all files on a partition, easy way to get this in bash script?

    Thank you much, Gents!

    Steeldriver's awk approach solves the immediate problem nicely, but I'll study all of these.

Bookmarks

Posting Permissions

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