Format a hard drive in Linux.

In the past couple of articles I have shown how to detect a new drive under Linux as well as how to partition the disk and prepare it for use. I have also gone over how to test a disk to make sure that it is reliable. Now I am going to cover how to format a drive using the Linux utility ‘mkfs’. In this post I will go over formatting a drive with the ‘ext4’ file-system which is a commonly used and (somewhat) modern file-system.

Note: Most of the commands shown below require you to have ‘root’ privileges. If you are running Ubuntu you will want to preface the commands with ‘sudo’ to get elevated privileges. I have kept the commands simple by not including the ‘sudo’ preface. 

In order to format the disk drive we must know a few things about the disk. First, you need to have the hard drive designation (e.g. sda). Secondly, you should have the number of the partition that you would like to format (e.g. sda1). I have shown how to find this information in my previous posts. In the following examples I will be using the first partition on ‘sdb’, so we will be formatting ‘sdb1’.

Once you have that information on hand you can execute the following command to format the disk drive:

mkfs.ext4 /dev/sdb1

Keep in mind that by using the ‘ext4’ file system your drive will most likely not be able to be read or written to by a Windows based system. If you are formatting a thumb drive using this method you will want to format the disk using FAT32 file-system, in which case you would use the mkfs.vfat command. If you are going to be preparing a hard drive for use on a Windows machine, you would want to format the disk using the ‘ntfs’ file-system by using the mkfs.ntfs command.

You will have to wait for a while depending on the size of the partition you are formatting. When the format is complete you will be presented with a command prompt as shown below.

mkfs.ext4

Now that you have a formatted disk you are going to want to be able to write and read data to and from the disk. In order to be able to do this you will have to ‘mount’ the disk to a directory on your system. This is a relatively simple processes which includes creating a mount point (a folder) and then mounting the file-system you just created to that directory.

To create a directory enter the following:

mkdir <location>

For example you could create a mount point in the ‘/mnt’ directory named ‘mountpoint’ with the following:

mkdir /mnt/mountpoint

Lastly, you will want to mount the disk to the mount-point you just created.

mount /dev/sdb1 /mnt/mountpoint

To ensure that the disk was correctly mounted, run the ‘df’ command by simply typing:

df

In addition to viewing the disk you mounted above, you can also use the ‘df’ command to view the location of all currently mounted file-systems as shown below.

df_command

Now you can browse to the disk using the ‘cd’, or “change directory” command as follows:

cd /mnt/mountpoint

Keep in mind that once you reboot your computer the drive will be un-mounted and you will have to manually re-mount the drive using the ‘mount’ command. In tomorrows article I will show you how to permanently mount the disk by using the ‘fstab’ file.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s