Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm trying to find out the total size of my hard drive using df -h, but it prints the size of each partition and requires sudo.

Is there any way to get total size of my hard drive using the command line, without requiring root permission?

share|improve this question
3  
Note: Using df is imprecise if the disk has unpartitioned space, not all partitions are mounted (e.g., some Windows partitions on the same drive) or some partitions are not mounted directly but indirectly through RAID, LVM or LUKS (encryption). It will also miss the swap partition(s). So I recommend not to use df for finding out the HDD size. –  Philipp Wendler Oct 28 '11 at 10:43

5 Answers 5

up vote 3 down vote accepted

A hacky way is to bypass the need for sudo by reading out the system log with:

dmesg | grep blocks

Please note that this might not be ideal, so your mileage may vary...

share|improve this answer

I'm not at my Linux box right now, but you could try:

hdparm -I /dev/sda

or

fdisk -l

which will also probably require a sudo.

I haven't seen anywhere that df requires root. That's odd.

share|improve this answer
    
Maybe "df" is not in PATH. HVNSweeting, try "sudo which df" and then try that as user. –  Milan Babuškov Oct 27 '11 at 17:33
    
hi, this is what i get: hvnsweeting@hvnbox:~$ blockdev --getsz /dev/sda /dev/sda: Permission denied hvnsweeting@hvnbox:~$ hdparm -I /dev/sda /dev/sda: Permission denied hvnsweeting@hvnbox:~$ fdisk -l hvnsweeting@hvnbox:~$ –  HVNSweeting Oct 28 '11 at 1:25
1  
Everything which accesses the devices directly (i.e., /dev/sda) will need sudo. –  Philipp Wendler Oct 28 '11 at 5:22

If you want to use that information in a script for example, using

sudo blockdev --getsz /dev/sda

might be easier than fdisk or hdparm as it only gives you the relevant information (just multiply by 512). However, it will also require sudo.

You could of course change the configuration of sudo such that it allows to execute this specific command without asking for the password (I guess blockdev --getsz is pretty safe even when executed by a normal non-privileged user).

This would be done by adding the following line to /etc/sudoers:

ALL ALL= NOPASSWD:/sbin/blockdev --getsz /dev/*

When you edit this file, be sure to use the command sudo visudo and not your usual editor. Otherwise it is very easy to make a syntax error, which would result in not being able to use sudo anymore (you would have to reboot into rescue mode to fix this).

share|improve this answer
1  
From the man page: --getsize64 Print device size in bytes.. Also here. –  Sparhawk Aug 2 at 5:08

You either have a bad install or an absurdly paranoid sysadmin. I've never heard of df requiring root privileges before. And I've worked with quite a few different flavors of unix/unix-like operating systems.

Anyhow, this will give you the total size of all disk partitions (first df column is /dev/hd... or /dev/sd...) in blocks (kB):

df | grep '^/dev/[hs]d' | awk '{s+=$2} END {print s}'

or in GB:

df | grep '^/dev/[hs]d' | awk '{s+=$2} END {print s/1048576}'
share|improve this answer
cat /sys/block/sda/size

But the size is in block-based unit
I found this answer here.
Other solutions here.

Or you can try udisks

udisks --show-info /dev/sda | egrep "^[[:space:]]*size" | awk '{print $2}'
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.