Linux fdisk command - check hard disk partitions

You need to check your hard disk partitions from time to time to keep your eyes on hard disk usage and to make sure your hard disk is not out of space. Normally, the /home partition always running out of space if you setup Linux as a desktop. It's because you are using a normal user account to do your job and keep all data in /home/ directory. If you setup your Linux as a server, then give most of your hard disk space to the main partition (the mount point). For example, if you setup a web server, then, you should give /var directory a bigger space because all website data will be kept in /var/www/htdocs. That's why it's very important for you to properly plan the hard disk partition and give a proper hard disk space to every mount point (directory) depends on the needs.

This tutorial is about how to check hard disk partitions from an already running Linux operating system using Linux fdisk command. If you are looking for fdisk tutorial to partition your hard disk, check the step by step partition guide with screenshots in Slackware hard disk partition with fdisk tutorial.

Check hard disk partitions with Linux fdisk command

The Linux fdisk command is a popular tool used to create hard disk partitions. Whoever installed Slackware Linux before should be familiar with Linux fdisk command. However, fdisk command also can be used to check hard disk partitions on the running Linux system. It's not only Slackware but you can find fdisk command in other Linux distributions too including Ubuntu. So this tutorial can be used to check hard disk space usage in any Linux distributions.

You need root privileges to run fdisk, otherwise you will have a command not found message if you are using Slackware or Cannot open /dev/sda if you are using Ubuntu like in the example below:

Invoke fdisk command without root privilege in Slackware Linux.

luzar@slackware:~$ fdisk -l
-bash: fdisk: command not found

Invoke fdisk command without root privilege in Ubuntu Linux.

luzar@ubuntu:~$ fdisk -l /dev/sda
Cannot open /dev/sda

You can use su - in Slackware and for Ubuntu, you can use sudo command to gain root privilege.

Slackware example:

luzar@slackware:~$ su -
Password:

Ubuntu example:

luzar@ubuntu:~$ sudo fdisk -l /dev/sda
[sudo] password for luzar: 

Disk /dev/sda: 80.0 GB, 80026361856 bytes
255 heads, 63 sectors/track, 9729 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0xa403a403

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1               1         124      995998+  82  Linux swap / Solaris
/dev/sda2            5100        9728    37182442+   f  W95 Ext'd (LBA)
/dev/sda3   *         125        2556    19535040   83  Linux
/dev/sda4            2557        5099    20426647+  83  Linux
/dev/sda5            5100        9728    37182411    7  HPFS/NTFS

Partition table entries are not in disk order

Here is a part of manual page for Linux fdisk command. The important part is the SYNOPSIS which will be our reference in all the examples.

NAME
       fdisk - Partition table manipulator for Linux

SYNOPSIS
       fdisk [-u] [-b sectorsize] [-C cyls] [-H heads] [-S sects] device

       fdisk -l [-u] [device ...]

       fdisk -s partition ...

       fdisk -v

DESCRIPTION
       Hard  disks can be divided into one or more logical disks called parti-
       tions.  This division is described in the partition table found in sec-
       tor 0 of the disk.

       In the BSD world one talks about `disk slices' and a `disklabel'.

We can use the second syntax in the synopsis above to check hard disk partitions in our current system. If we want to check the first hard disk in Linux system, issue fdisk command and the first hard disk. See example below:

root@slackware:~# fdisk -l /dev/sda

Disk /dev/sda: 160.0 GB, 160041885696 bytes
255 heads, 63 sectors/track, 19457 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0xb262b262

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1               1         250     2008093+  82  Linux swap
/dev/sda2             251        3898    29302560   83  Linux
/dev/sda3            3899        5115     9775552+  83  Linux
/dev/sda4            5116       19457   115202115   83  Linux
root@slackware:~#

The Linux fdisk -l option if invoked without giving specific hard disk, will print hard disk based on data found in the /proc/partitions.

root@slackware:~# fdisk -l

Disk /dev/sda: 160.0 GB, 160041885696 bytes
255 heads, 63 sectors/track, 19457 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0xb262b262

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1               1         250     2008093+  82  Linux swap
/dev/sda2             251        3898    29302560   83  Linux
/dev/sda3            3899        5115     9775552+  83  Linux
/dev/sda4            5116       19457   115202115   83  Linux

Disk /dev/sdb: 250.0 GB, 250059350016 bytes
255 heads, 63 sectors/track, 30401 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0xa5bbe44b

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        5099    40957686    7  HPFS/NTFS
/dev/sdb2            5100       30400   203230282+   f  W95 Ext'd (LBA)
/dev/sdb5            5100        8923    30716248+   7  HPFS/NTFS
/dev/sdb6            8924       15297    51199123+   7  HPFS/NTFS
/dev/sdb7           15298       21671    51199123+   7  HPFS/NTFS
/dev/sdb8           21672       30400    70115661    7  HPFS/NTFS
root@slackware:~# 

The result above shows all hard disk used in the current Linux system. If we want to print size in sector instead of cylinder, add -u option with the command. See example below:

root@slackware:~# fdisk -l -u /dev/sda

Disk /dev/sda: 160.0 GB, 160041885696 bytes
255 heads, 63 sectors/track, 19457 cylinders, total 312581808 sectors
Units = sectors of 1 * 512 = 512 bytes
Disk identifier: 0xb262b262

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1              63     4016249     2008093+  82  Linux swap
/dev/sda2         4016250    62621369    29302560   83  Linux
/dev/sda3        62621370    82172474     9775552+  83  Linux
/dev/sda4        82172475   312576704   115202115   83  Linux
root@slackware:~#

We can display the size of partition in block using fdisk - s option. However we need to provide the partition name. See the example below:

root@slackware:~# fdisk -s /dev/sda4
115202115
root@slackware:~#

That's all. Now you can check your hard disk usage with Linux fdisk command and take appropriate action before any partition runs out of space. Remember to check hard disk space regularly or you can write a script to let you know if a certain partition is reaching the limit.

Back to Linux basic commands main page.


Post new comment

The content of this field is kept private and will not be shown publicly.
This blog uses the CommentLuv Drupal plugin which will try and parse your sites feed and display a link to your last post, please be patient while it tries to find it for you.

Custom Search