close
close

Semainede4jours

Real-time news, timeless knowledge

How to format a drive from command line in Linux
bigrus

How to format a drive from command line in Linux

command line concept

Fatmawati Lauda/Getty Images

my current situation Linux desktop It has one main drive that houses the operating system and four other drives where I store different data. I have one 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.

Since my desktop machine has a GUI, GNOME I used the Disks tool and formatted these drives without opening the terminal window. But I’m old school about some things, so I automatically go to the command line for this kind of work – even if the task would be easier with a GUI.

Moreover: Top 5 Linux commands every new user should learn

So how do you format a drive so that it is ready for use on a Linux system?

Let me show you.

Find the driver name

What you will need: All you need is a running Linux instance (it doesn’t matter which distribution you use), an external drive connected to the machine, and a user with sudo privileges.

This is it. Let’s get to work.

The first thing you need to do is connect the drive to the machine. Once you’ve done this you’ll need to find the device name (this will be something like /dev/sdb). To do this, use: lsblk command like this:

You should see a list of all connected drives.

The first thing we will do is create a partition scheme. If your machine uses legacy BIOS mode, choose MBR type. If your machine uses UEFI, switch to GPT mode. Let’s assume your machine uses UEFI, so type: g and press Enter on your keyboard.

Moreover: Are you thinking of switching to Linux? 10 things you need to know

Then create a new section by typing . N. After that press Enter to accept the defaults for partition number, first sector and last sector.

Write when you’re done P. to list the new partition table and then save your changes by typing the partition table with the following command: w choice.

Formatting the drive

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

If you want to use the popular ext4 format, the command for that is:

sudo mkfs -t ext4 /dev/sdb1

The -t option is for: medicinethe ext4 one. Notice that we added 1 to our driver name. From where? Because this is the first partition we created on the drive.

You may want to format the drive so it can be accessed by Windows machines. The command for this will be:

sudo mkfs -t ntfs /dev/sdb1

Mounting the drive

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

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

You can give the directory any name you want.

After the mount point is created, let’s mount the drive with the following command:

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

The drive should now be mounted and usable.

Moreover: The 4 best MacOS text editors (and why you should use one)

That’s all there is to formatting a drive from the command line in Linux.