Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 479
Baklap4 Avatar asked Apr 13 '16 11:04

Baklap4


People also ask

How do I check disk space in Linux?

That command is df -H. The -H switch is for human-readable format. The output of df -H will report how much space is used, available, percentage used, and the mount point of every disk attached to your system (Figure 1).


1 Answers

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
like image 139
Fazlin Avatar answered Oct 08 '22 20:10

Fazlin