How to format a drive on Linux from the command line

3 weeks ago 17
BOOK THIS SPACE FOR AD
ARTICLE AD
command line concept
fatmawati lauda/Getty Images

My current Linux desktop has one main drive housing the OS and four other drives where I store different data. I have a drive for music, one for video, one for documents, and one for virtual machines. Each secondary drive was purchased new, so they had to be formatted before use.

Because my desktop machine has a GUI, I could have used the GNOME Disks tool and formatted those drives without opening the terminal window. However, I'm old-school with some things, so I automatically go to the command line for such jobs -- even when the task is easier with a GUI.

Also: The first 5 Linux commands every new user should learn

So, how do you format a drive so it's ready to use on a Linux system? 

Let me show you.

Locating the drive name

What you'll need: The only things you'll need are a running instance of Linux (it doesn't matter which distribution you use), an external drive attached to the machine, and a user with sudo privileges. 

That's it. Let's get to work.

The first thing you must do is attach the drive to the machine. Once you've done that, you'll need to locate the device name (which will be something like /dev/sdb). To do that, use the lsblk command like this:

You should see a listing of all attached drives.

The first thing we'll do is set a partition scheme. If your machine uses legacy BIOS mode, go with the MBR type. If your machine uses UEFI, go with GPT mode. Let's assume your machine uses UEFI, so type g and hit Enter on your keyboard.

Also: Thinking about switching to Linux? 10 things you need to know

Next, create a new partition by typing n. After this, hit Enter to accept the defaults for partition number, first sector, and last sector.

When you finish, type p to list the new partition table and then save your changes by writing the partition table with the w option.

Formatting the drive

Now that the drive has a partition table, you can format it.

If you want to go with the popular ext4 format, the command for this is:

sudo mkfs -t ext4 /dev/sdb1

The -t option is for type, which is ext4. Notice we add the 1 to our drive name. Why? Because that's the first partition in the drive that we created.

You might want to format the drive so it's accessible by Windows machines. For that, the command would be:

sudo mkfs -t ntfs /dev/sdb1

Mounting the drive

With the drive formatted, it's time to mount it so it's accessible. The process for this task is simple.

First, create a new directory to serve as the mount point. Let's assume you are the only person who will access that drive. That setup means we can mount the drive within your home directory. Let's create a mount point with the command:

You can name the directory anything you like.

With the mount point created, let's mount the drive with the command:

sudo mount -t auto /dev/sdb1 ~/EXTERNAL

The drive should now be mounted and usable.

Also: The 4 best MacOS text editors (and why you should be using one)

And that's all there is to formatting a drive in Linux from the command line.

Read Entire Article