简体   繁体   中英

Get free space of HDD in linux

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM