# Formatting a drive in linux

#### To partition and format your drive in Linux   
Open the parted utility and then you will work inside (parted) that.

To partition and format your drive in Linux after zeroing, follow these steps. Replace `sdX` with your drive’s identifier (check with `lsblk`). Ensure the drive is unmounted (`sudo umount /dev/sdX\*`) and data is backed up, as these steps will erase existing structures.

Before you start: unmount your drive if its mounted. "lsblk" to find out.

**1. Create a Partition Table and Partition:**

```bash
sudo fdisk /dev/sdX
```

\- Press `g` to create a new GPT partition table. (MBR is for booting drive)  
\- Press `n` to create a new partition:  
\- Select default partition number (e.g., 1). If you want all space of the drive (one partition)  
\- Select default first sector.  
\- Select default last sector (or specify size, e.g., `+100G` for 100GB).  
\- Press `w` to write changes and exit.

\*\*2. Format the Partition:\*\*

```bash
sudo mkfs.ext4 /dev/sdX1
```

\- This formats the first partition (`sdX1`) as ext4. Replace `ext4` with `vfat` for FAT32, `ntfs` for NTFS, etc.  
\- Progress is shown during formatting.

\*\*3. Verify:\*\*

```bash
lsblk -f
```

\- Confirms the new partition and filesystem.

\*\*Notes:\*\*  
\- Check drive with `lsblk` to confirm `sdX`.  
\- For multiple partitions, repeat `n` in `fdisk` for each partition, then format each (e.g., `mkfs.ext4 /dev/sdX2`).  
\- If errors occur, ensure the drive is not mounted (`umount /dev/sdX\*`).

---

<div id="bkmrk--1"></div>#### For a more complex formatting, you can use "parted" to do that.

<div id="bkmrk-this-is-a-simple-exa">This is a simple example: </div><div id="bkmrk--2"></div><div id="bkmrk-%60sudo-parted-%2Fdev%2Fsd">`sudo parted /dev/sdb`</div><div id="bkmrk--3">  
</div><div id="bkmrk-now-you-are-in-%22part">Now you are in "parted"</div><div id="bkmrk--4">  
</div><div id="bkmrk-create-a-partition">Create a partition</div><div id="bkmrk---for-mbr">- For MBR</div><div id="bkmrk-%60mklabel-msdos%60">`mklabel msdos`</div><div id="bkmrk---for-gpt">- For GPT</div><div id="bkmrk-%60mklabel-gpt%60">`mklabel gpt`</div><div id="bkmrk--5">  
</div><div id="bkmrk-the-basic-syntax-for">The basic syntax for partition command</div><div id="bkmrk--%60mkpart-%27partition-">-`mkpart 'partition type' 'file system' start end`</div><div id="bkmrk-if-you-want-all-spac">If you want all space of the drive (one partition)</div><div id="bkmrk-%60mkpart-primary-ext4">`mkpart primary ext4 1MiB 100%`</div><div id="bkmrk-if-you-want-more-tha">If you want more than one partition</div><div id="bkmrk-%60mkpart-primary-ext4-1">`mkpart primary ext4 1MiB 2GB`</div><div id="bkmrk-if-you-want-to-creat">If you want to create another partition, use</div><div id="bkmrk-%60mkpart-primary-ext4-2">`mkpart primary ext4 2GB 3GB`</div><div id="bkmrk--6">  
</div><div id="bkmrk-to-see-the-partition">To see the partitions</div><div id="bkmrk-%60print%60">`print`</div><div id="bkmrk--7">  
</div><div id="bkmrk--8">  
</div><div id="bkmrk-%C2%A0%3F-help%3F"> ? Help?</div><div id="bkmrk-%C2%A0-%60isblk%60"> `isblk`</div>