For a desktop Linux system, there are many graphical tools available for disk partitioning and formatting. The most famous are GParted and QTParted. Nevertheless, there are many instances in which you might need to format a hard drive from the command line, particularly if you are running a server. Once you are finished, you will have a partitioned hard drive with a Linux file system ready for use. The example used in this article will create a single partition with an Ext4 Linux file system. Follow each command by pressing "Enter".
Set Up a New Partition
Open a terminal window and log in as root:
Video of the Day
$ su (or "sudo su" on some distributions)
Find out the name of your hard drive. To do this, enter the following from the command prompt as root:
Choose your newly installed drive after fdisk lists the current drives. The current hard drive will already have partitions assigned, and the output will look like this:
Disk /dev/sda: 16.1 GB, 16139354112 bytes /dev/sda1 * 1 1874 15052873+ 83 Linux
/dev/sda2 1875 1962 706860 5 Extended
/dev/sda5 1875 1962 706828+ 82 Linux swap / Solaris
Your new hard drive will not have partitions, so all you will see will be a line such as:
Disk /dev/sdb: 16.1 GB, 16139354112 bytes
Check to see if your disk is already mounted with this command:
Unmount your drive if it is mounted using this command:
Create a new partition by typing this command:
The fdisk prompt will open. Press "n" to make the new partition, and then press "Enter."
Press "p" to create a primary partition, and then press "Enter."
Press "1" to create the first partition followed by "Enter."
Enter the default for both the first and last cylinders when your computer asks for this information. This will make the partition cover the entire disk rather than just part of it.
Press "t" to change the file system type followed by "Enter."
Type "L" to see a list of known types followed by "Enter."
Type "83" for "Linux" followed by "Enter."
Press "w" to write the partition to the disk (this cannot be undone), and then press "Enter."
Format the New Partition
Format the new partition that you created with the following command:
Create a new directory and mount the new drive with these commands:
mkdir /media/newdrive (or whatever name you prefer)
mount /dev/sdb1 /media/newdrive
Edit your fstab file so that the new drive will be mounted at boot. Fstab is the Linux file system configuration file to mount partitions at boot. You can edit /etc/fstab with the "nano" command or "vi" depending on which editor you prefer.
nano /etc/fstab or # vi /etc/fstab
Add the following line to the end of fstab:
/dev/sdb1 /media/newdrive ext4 defaults 1 2
Save the fstab file.
Things You'll Need
Computer running Linux
Newly installed hard drive or removable drive
If this is your first time formatting with Linux, try it on an empty drive that will not be used for anything critical.
Formatting in Linux is a relatively quick process, but you should pay close attention to the details because most changes of this magnitude are irreversible.