Register now for better personalized quote!

HOT NEWS

How to format a drive on Linux from the command line

Oct, 31, 2024 Hi-network.com
fatmawati lauda/Getty Images

My current Linux desktophas one main drive housingthe 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.

1. Locate the drive name

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 thelsblkcommand like this:

lsblk -f

You should see a listing of all attached drives.

2. Partition the drive

This will only apply to disks that have yet to be partitioned (such as a new drive). If your disk has already been used, you might not have to run this command.

Also: The top 5 GNOME extensions I install first (and what they can do for you)

To partition a drive, we'll use thefdiskcommand (assuming our drive name is /dev/sdb) like this:

sudo fdisk /dev/sdb

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 typeg and hit Enter on your keyboard.

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

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

When you finish, typep to list the new partition table and then save your changes by writing the partition table with thewoption.

Formatting the drive

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

1. Formatting as ext4

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 fortype, 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.

2. Formatting for NTFS

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:

sudo mkdir ~/EXTERNAL

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.

Open Source

10 Linux apps I can't do without - and whyLinux and open-source documentation is a mess: Here's the solution6 ways LibreOffice is better than Google Docs for serious writing workThe Linux file system structure explained
  • 10 Linux apps I can't do without - and why
  • Linux and open-source documentation is a mess: Here's the solution
  • 6 ways LibreOffice is better than Google Docs for serious writing work
  • The Linux file system structure explained

tag-icon Hot Tags : Tech Services & Software Operating Systems

Copyright © 2014-2024 Hi-Network.com | HAILIAN TECHNOLOGY CO., LIMITED | All Rights Reserved.
Our company's operations and information are independent of the manufacturers' positions, nor a part of any listed trademarks company.