Check Disk Space in Linux

There are plenty of tools with which you can check your disk space. However, Linux already has a built in function to show you just what you need to know. Open a terminal window or push (ctrl+alt+F1 to go to console) and type:

# df

You will see something like this(your output may be different, depending on how many partitions/harddrives/cdroms you have mounted):

Filesystem 1K-blocks Used Available Use% Mounted on
/dev/hda3 78012484 17606992 56442660 24% /
/dev/hda1 101086 16400 79467 18% /boot
none 516808 0 516808 0% /dev/shm
/tmp 247919 7339 227780 4% /tmp

This one looks a bit unreadable, because size is represented in 1K-blocks, lets try to make it clean and more readable:

# df -h

Filesystem Size Used Avail Use% Mounted on
/dev/hda3 75G 17G 54G 24% /
/dev/hda1 99M 17M 78M 18% /boot
none 505M 0 505M 0% /dev/shm
/tmp 243M 7.2M 223M 4% /tmp

Now the size is represented by megabytes and gigabytes … better? :) Now let’s create an executable file to show the disk sizes:

#!/bin/sh

DISC=$1
PARTITION=`df -h |grep $DISC |awk ‘{print $1}’`
SIZE=`df -h|grep $DISC|awk ‘{print $2}’`
USED=`df -h|grep $DISC|awk ‘{print $3}’`
FREE=`df -h|grep $DISC|awk ‘{print $4}’`

echo “Partition: $PARTITION”
echo “Total size: $SIZE”
echo “Used space: $USED”
echo “Free space: $FREE”

Simply copy & paste this script into for example into a file named info.sh(create it with VI or JOE or even PICO). Next, you’ll need to make it executable. To do this, use the following command:

# chmod +x info.sh

Now, to execute the file, you need to run it, and pass it the correct argument. For our example, we are going to use hda3. So, to execute the file, type in the command as below….

# ./info.sh hda3

Tadaaa :)

Comments

  1. I can see this being useful if you’re not running a GUI. But if you are, most modern distros come with apps that does the same thing. For example, SUSE 9.2 came with this

  2. GUI, schmooey. I access tons of Web, DB and other server type remotely that are running various flavors of Linux and BSD. An admin always wants to know things like free disk space and what apps or logs are hogging the space if the host starts to freak out.

  3. Nice, it doesn’t so important to me, but nice idea!

  4. ubuntuBeginer says:

    You are greatest. Job well done. I think that U helped many how are beginers in linux as myself.

  5. If you have only two pennies left in the world, with the first penny, you should buy rice to feed your family. With the second penny, say the wise Japanese, you should buy a lily. The Japanese understand the importance of dreaming…

  6. casino gambling says:

    casino gambling Keyword doesn’t matter

  7. That script is pretty lame. It just lays it out in a different format. Big deal.

  8. This is very interesting site…

  9. proverka2007

  10. Just thought I would add this as we have a ton of servers that we only touch if we need to. If your server can email you can add this to the bottom and comment out the echo’s. I have it emailing my cell phone.

    if [ "$FREE" -le "$MINSPACE" ]; then
    mail -s “Running out of room on $DISC” [email protected] Just thought I would add this as we have a ton of servers that we only touch if we need to. If your server can email you can add this to the bottom and comment out the echo’s. I have it emailing my cell phone.

    if [ "$FREE" -le "$MINSPACE" ]; then
    mail -s “Running out of room on $DISC” [email protected] < sendtext
    fi

    Where sendtext is just a file with “Running out of space” in it.

  11. Thank you for posting this, it helped me finding out how much I had left. IÂŽm a starting Linux user and this is very helpful.

  12. Very well! Your site is neat!
    [url=][/url]

  13. good info. thank you
    df –si :P

  14. I found this page because gparted doesn’t recognize the partitions on my USB hard drive. df works great. thanks

  15. 10x… for df.
    if i want to see size for individual folder ?

  16. For a folder, use du. Here’s the nitty-gritty (man du for everything):
    -h print sizes in human readable format (e.g., 1K 234M 2G)
    -s display only a total for each argument
    -a write counts for all files, not just directories
    What this means is with the -s option, it only shows you stats for what you’re concerned with, otherwise it operates recursively and shows you stats for all the subdirectories of what you enter (by default it doesn’t list files without the -a switch).
    Examples:
    $ du -h -s /home/george
    107M /home/george
    $ du -h /home/george
    7.2M /home/george/openldap-2.3.39/tests
    7.1M /home/george/openldap-2.3.39/clients
    6.1M /home/george/openldap-2.3.39/doc
    98M /home/george/openldap-2.3.39
    107M /home/george

  17. thank you so much. Very helpfull information.

  18. Ashutosh Singh says:

    Fine!. The above post was very helpful for me. Thanks for posting it here…

  19. The script (above) does Not work for me? I am running Ubuntu 8.04, i get errors in grep, awk and the rest of the commands saying like you have to use a switch with grep etc.??

  20. Hello;

    How can I tell which partition has how much space AND How can I Move files from a full partition to ones with more space?
    Thanks!

    df , du -h and fdisk -l are useful But do not tell me what I need to know.
    I am not sure since I inherited these servers and didnt set up partitions?:
    for instance;

    root@myserver:/# df -h
    Filesystem Size Used Avail Use% Mounted on
    varrun 941M 64K 941M 1% /var/run
    varlock 941M 0 941M 0% /var/lock
    procbususb 941M 104K 941M 1% /proc/bus/usb
    udev 941M 104K 941M 1% /dev
    devshm 941M 0 941M 0% /dev/shm

    but when I do an fdisk -l I get;

    root@myserver:/# fdisk -l

    Disk /dev/sda: 123.5 GB, 123522416640 bytes
    255 heads, 63 sectors/track, 15017 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes

    Device Boot Start End Blocks Id System
    /dev/sda1 1 15017 120624021 83 Linux

    Disk /dev/sdb: 82.3 GB, 82348277760 bytes
    255 heads, 63 sectors/track, 10011 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes

    Device Boot Start End Blocks Id System
    /dev/sdb1 1 10011 80413326 83 Linux

    Disk /dev/sdc: 250.0 GB, 250058301440 bytes
    255 heads, 63 sectors/track, 30401 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes

    Device Boot Start End Blocks Id System
    /dev/sdc1 1 2432 19535008+ 83 Linux
    /dev/sdc2 2433 2554 979965 82 Linux swap / Solaris
    /dev/sdc3 2555 30401 223681027+ 8e Linux LVM

    SO…. HOW DO I MOVE FILES FROM like sda1 to sdc1 ?etc…

    Thank You!

  21. I want to check free space (Unalloacted Partition) in linux without calculating.

    want to check throgh command line.

    Means alternate command for “hwbrowser”

  22. I want to say, ctrl+ alt + F1 leads to shutdown my KDE unnormally but not open a terminal. :-)

    Anyway, Thanks very much! this blog entity is useful to me.

    • lyecdevf says:

      The same happens on my awesome graphical interface. The alt+ctrl+f1 logs me out. It is not useful to me in any way since there is nothing that I can do after I log back in.

  23. The script above doesn’t work quite right. Mainly, the third line should read DISC=1, and the quote marks don’t always copy/paste correctly. Try this code:

    #!/bin/sh

    DISC=1
    PARTITION=`df -h |grep $DISC |awk ‘{print $1}’`
    SIZE=`df -h|grep $DISC|awk ‘{print $2}’`
    USED=`df -h|grep $DISC|awk ‘{print $3}’`
    FREE=`df -h|grep $DISC|awk ‘{print $4}’`

    echo “Partition: $PARTITION”
    echo “Total size: $SIZE”
    echo “Used space: $USED”
    echo “Free space: $FREE”

  24. DISC=$1 works fine, DISC=1 ????????????????????????

  25. Mickeydblv says:

    Line 3 with the DISK=$1 is saying to take the parameter from the command line as the drive to do the work on. If you use DISK=1 you’ll always get readings for disk 1 – funnily enough.

    Nice script – thanks!

  26. Why available space + used space is not equal to total size ??

  27. Awesome, when you want to use linuxserver just from console, i’ll add this to my knowledge base. Thanks!

  28. thanx it worked

  29. Zune and iPod: Most people compare the Zune to the Touch, but after seeing how slim and surprisingly small and light it is, I consider it to be a rather unique hybrid that combines qualities of both the Touch and the Nano. It’s very colorful and lovely OLED screen is slightly smaller than the touch screen, but the player itself feels quite a bit smaller and lighter. It weighs about 2/3 as much, and is noticeably smaller in width and height, while being just a hair thicker.

  30. Special 10x, that is just great!

    Only one problem i have monted like that
    /usr/part – partition 1
    /usr/part/small1 – partiton 2
    /usr/part/small2 – partition 3
    in that case, the script can’t work properly for /usr/part :)

    Any way – thank you very much!!!

  31. I have an error like this when I run this script:

    awk: 1: unexpected character 0xe2
    awk: line 2: missing } near end of file

    • [email protected] says:

      @ Sarvar
      Myself also has the same error.
      awk: 1: unexpected character 0xe2
      awk: line 2: missing } near end of file
      Did u found the solution?
      Pls help me.

  32. search for files bigger than 10MB in the current directory and sub-directories and print the 100 largest ones
    find . -type f -size +10000000c -exec ls -l \{} \; | sort -n -k 5,5 | tail -100

    CLEAN IT UP!

  33. newtolinux says:

    hi. i’m not able to start the start in my linux 2.4.20-8.
    1 of the problem is due to 0 diskspace. how to release the diskspace?

    • I am facing the same problem, how to release some space by freeing up the temporary files and cache from red hat linux. please share any useful link for it too. I could nothave space to even resize my disk space… Its urgent

      regards

  34. Elegant. I wonder if there is way to do a command or 2 to check the hard-drive remaining capacity.

  35. Kakagak says:

    I copied and pasted the command and it didn’t work due to unknown character. so I typed it as follow:

    #!/bin/sh

    DISC=$1
    PARTITION=`df -h|grep $DISC|awk ‘{print $1}’`
    SIZE =`df -h| grep $DISC|awk ‘{print $2}’`
    USED =`df -h|grep $DISC|awk ‘{print $3}’`
    FREE =`df -h|grep $DISC|awk ‘{print $4}’`

    echo “Partition: $PARTITION”
    echo “Total size:$SIZE”
    echo “Used space:$USED”
    echo “Free space:$FREE”

    and I get the following error:
    ./info.sh: line 5: SIZE: command not found
    ./info.sh: line 6: USED: command not found
    ./info.sh: line 7: FREE: command not found
    Partition:
    Total size:
    Used space:
    Free space:

  36. If you want to get free space based on the mount path try this version:

    FREE=`df | grep “$FS\$” | awk ‘{print $4}’`

Speak Your Mind

*