Wednesday, July 15, 2009

Finding out how much disk space you use

Sufficient quantity of free disk space is an absolute necessity irrespective of the Linux distro that you run. We can find out statistics about disk usage with certain simple command line utilities. Let us explore a few of them.
Let us try the df command

 ~$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda5 91G 62G 25G 72% /
varrun 497M 116K 497M 1% /var/run
varlock 497M 0 497M 0% /var/lock

It prints the disk usage on u physical drives and some special directories.

At the creation of a file system on a disk the number of i nodes are fixed. If you have a large number of small files you can run out of i-node. The following example list the i-node utilization.
$ df -hi
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/sda5 5.8M 436K 5.3M 8% /

If you have network mounts (such as Samba or NFS), these will show up too in your df output. To limit df output to local file systems, type the following:
$  df -hl
Filesystem Size Used Avail Use% Mounted on
/dev/sda5 91G 62G 25G 72% /

You can find the file system type with -T flag
$ df -hT
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda5 ext3 91G 62G 25G 72% /

You can check usage of disk space by a particular file or directory wth du command
$ du -h SAS
220K SAS/lesson2_files
52K SAS/lesson5c_files
144K SAS/lesson5_files
136K SAS/lesson5b_files
76K SAS/lesson6_files
68K SAS/lesson6_1_files
148K SAS/lesson3_files
448K SAS/lesson7_files
8.0K SAS/lesson1_files
168K SAS/lesson4_files
1.6M SAS

The above example lists the usage of SAS by SAS directory.

On ubuntu systems you can find out the total space used by all users with

 $sudo  du -sh /home
55G /home

You can specify multiple directories with the -c option and total them up.

$ sudo du -sch /home /var
55G /home
293M /var
55G total

You can exclude files that match a certain pattern from being counted using the exclude option. See the following example which excludes iso images from being counted.

 $ sudo du -sh --exclude=’*.iso’ /home/fermi
588M /home/fermi

You can specify what depth in the tree you want to summarize. Set --max-depth to a number greater than 1 to dig deeper into disk space usage:
$ sudo du -h --max-depth=2 /home
4.0K /home/fermi/Mail
52K /home/fermi
55 G /home

No comments: