Wednesday, January 18, 2012

Format External Hard Drive Using Command Line (Linux)

To give a little background, I was backing up some files on my laptop to my external hard drive. Some of the files were greater than 4GB, which is the file size limit of FAT32 formatted drives. For more information on this, read up here: http://support.microsoft.com.

So, naturally, I decided to reformat my external hard drive to NTFS. There are certainly other options: HFS for Mac users or EXT3 for Linux users, but I need it to work on Windows 7, Mac, and Linux. After some quick research, it seems NTFS has at least some form of support in all three platforms.

Find the External Hard Drive Device


The first step is finding the hard drive to zero and format. After plugging in the device, the /dev/ directory should list the external device if it was recognized:
cd dev
ls s*
The output will be something to this effect:
sda sda1 sda2 sdb sdb1
Each letter represents a drive device and each proceeding number represents a partition.

Note: For the sake of example, /dev/sdb1 is the path to my external device! This will vary depending on the number of internal and external hard drives on the system.

Before continuing, the target device must not be mounted. To check, simply run this command:
mount | grep /dev/sdb1
There should be no resulting output. If there is, unmount the drive:
umount -l /dev/sdb1

Clearing Partitions and Creating A New Partition

The goal is to have a single partition to hold all of my files. So, using the fdisk tool, partitions can be altered on the device:
sudo fdisk /dev/sdb
Please note that it is /dev/sdb and not /dev/sdb1. We want to target the device, not the partition.

The following menu should appear:
Command (m for help):
First, delete the existing partition:
Command (m for help): d
Selected partition 1
Then, create a new primary partition on the device:
Command (m for help): n
Command action
      extended
      primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-121597, default 1): 
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-121597, default 121597): 
Using default value 121597
The numbers will vary, but the output should look similar.

Now, let fdisk write the changes:
Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
And voila! The partition is created. Almost done.

Formatting the New Partition to NTFS

In my particular case, I needed NTFS, but this part applies to any filesystem you desire.

A simple way to format a new filesystem is to use mkfs. More information about it can be found here: http://www.linfo.org.

Just execute a simple command:
sudo mkfs.ntfs /dev/sdb1
It will write zeros to the drive before setting up the filesystem. This can take up to hours depending on the size of the drive.

That's pretty much all! Comment or email if you have any questions/suggestions.

Resources:
mkfs Formatting: http://ubuntuforums.org/showthread.php?t=267869

1 comment:

  1. This post save a lot of my time. Thanks

    ReplyDelete