6

Within a bash script i need to get the total disk size and the currently used size of the complete disk.

I know i can get the total disk size without needed to be root with this command:

cat /sys/block/sda/size

This command will output the count of blocks on device SDA. Multiply it with 512 and you'll get the amount of bytes on this device. This is sufficient with the total disk size.

Now for the currently used space. I want to get this value without being root. I can assume the device name is SDA. Now there is this command: df I thought i could use this command but it seems this command only outputs data of the currently mounted partitions.

Is there a way to get a total of space used on disk SDA without needing to be root and not all partitions needs to be mounted?

Let's assume the following example:

/dev/sda1 80GB Linux partition 20GB Used
/dev/sda2 80GB Linux Home partition 20GB Used
/dev/sda3 100GB Windows Parition. 30GB Used

Let's assume my SDA disk is partitioned like above. But while i'm on Linux my Windows partition (sda3) is not mounted.

The output of df will give me a grand total of 40 GB Used so it doesn't take sda3 in account.

Again the question: Is there a way without root to get me a grand total of 70 GB used space?

Baklap4
  • 3,914
  • 2
  • 29
  • 56
  • `fdisk -l` with sudo? – riteshtch Apr 13 '16 at 11:37
  • Like my question said i know the ways with sudo. Yet this program will not run with sudo rights. At least i'm trying not to. I really need a solution without sudo – Baklap4 Apr 13 '16 at 11:42
  • lsblk | tr -s ' ' | cut -d ' ' -f1,4 |column -t – Daniel Apr 13 '16 at 11:43
  • @Joda `lsblk` shows the total amount of space for each partition. I know how to get those. But i'm asking about the total amount of free space for each partition while it shouldn't matter if they are mounted or not. – Baklap4 Apr 13 '16 at 11:45

2 Answers2

1

I think stat command should help. You can get the get the partitions from /proc/partitions.

Sample stat command output:

$ stat -f /dev/sda1
  File: "/dev/sda1"
    ID: 0        Namelen: 255     Type: tmpfs
Block size: 4096       Fundamental block size: 4096
Blocks: Total: 237009     Free: 236970     Available: 236970
Inodes: Total: 237009     Free: 236386
Fazlin
  • 2,285
  • 17
  • 29
-1

You can use df.

df -h --output='used' /home 
Used
3.2G

If you combine this with some sed or awk you can have the value you seek

kameranis
  • 284
  • 1
  • 13
  • Like i've said before df will not give me what i need. As it gives you a total of a directory. I don't know what partitions i have. So this won't do the trick. Besides df only tracks currently mounted devices. which is not the complete disk space. – Baklap4 Apr 13 '16 at 13:31