How to check disk space in Cent OS/Linux?

Linux OS Topics
Post Reply
User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

How to check disk space in Cent OS/Linux?

Post by Saman » Thu Mar 22, 2012 1:26 am

Linux 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:

Code: Select all

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

Code: Select all

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:

Code: Select all

# df -h

Code: Select all

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:

Code: Select all

#!/bin/sh

Code: Select all

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:

Code: Select all

# 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….

Code: Select all

# ./info.sh hda3
Post Reply

Return to “Linux”