Ubuntu:Format Drive CLI: Difference between revisions

No edit summary
 
Line 6: Line 6:


It’s important to note that formatting is a destructive process, and it will erase all the existing data. If you have data on the UDB drive or the SD card, make sure you back it up.
It’s important to note that formatting is a destructive process, and it will erase all the existing data. If you have data on the UDB drive or the SD card, make sure you back it up.
== Installing Parted ==
== Installing Parted ==
GNU Parted is a tool for creating and managing partition tables. The parted package is pre-installed on most Linux distros nowadays. You can check if it is installed on your system by typing:
GNU Parted is a tool for creating and managing partition tables. The parted package is pre-installed on most Linux distros nowadays. You can check if it is installed on your system by typing:
parted --version


parted (GNU parted) 3.2
<syntaxhighlight lang="bash">
Copyright (C) 2014 Free Software Foundation, Inc.
parted --version
...
</syntaxhighlight>
 
<syntaxhighlight lang="bash">
parted (GNU parted) 3.2
Copyright (C) 2014 Free Software Foundation, Inc.
...
</syntaxhighlight>
 
If <code>parted</code> is not installed on your system, you can install it using your distribution package manager.
If <code>parted</code> is not installed on your system, you can install it using your distribution package manager.
=== Install <code>parted</code> on Ubuntu and Debian ===
=== Install <code>parted</code> on Ubuntu and Debian ===
sudo apt updatesudo apt install parted
 
<syntaxhighlight lang="bash">
sudo apt updatesudo apt install parted
</syntaxhighlight>
 
=== Install <code>parted</code> on CentOS and Fedora ===
=== Install <code>parted</code> on CentOS and Fedora ===
sudo yum install parted
 
<syntaxhighlight lang="bash">
sudo yum install parted
</syntaxhighlight>
 
== Identifying the USB or SD Card Name ==
== Identifying the USB or SD Card Name ==
Insert the USB flash drive or SD card into your Linux machine and find the device name using the <code>lsblk</code> command:
Insert the USB flash drive or SD card into your Linux machine and find the device name using the <code>lsblk</code> command:
lsblk
 
<syntaxhighlight lang="bash">
lsblk
</syntaxhighlight>
 
The command will print a list of all available block devices:
The command will print a list of all available block devices:
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
 
...
<syntaxhighlight lang="bash">
sdb      8:16  1  14.4G  0 disk  
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
└─sdb1  8:17  1  1.8G  0 part /media/data
...
...
sdb      8:16  1  14.4G  0 disk  
└─sdb1  8:17  1  1.8G  0 part /media/data
...
</syntaxhighlight>
 
In the example above, the name of the SD device is <code>/dev/sdb</code>, but this may vary on your system.
In the example above, the name of the SD device is <code>/dev/sdb</code>, but this may vary on your system.


You can also use the <code>dmesg</code> command to find the device name:
You can also use the <code>dmesg</code> command to find the device name:
lsblk
 
<syntaxhighlight lang="bash">
lsblk
</syntaxhighlight>
 
Once you attach the device, <code>dmesg</code> will show the device name:
Once you attach the device, <code>dmesg</code> will show the device name:
...
 
[  +0.000232] sd 1:0:0:0: [sdb] 30218842 512-byte logical blocks: (15.5 GB/14.4 GiB)
<syntaxhighlight lang="bash">
...
...
[  +0.000232] sd 1:0:0:0: [sdb] 30218842 512-byte logical blocks: (15.5 GB/14.4 GiB)
...
</syntaxhighlight>
 
== Securely Wipe Up the Data (Optional) ==
== Securely Wipe Up the Data (Optional) ==
Before formatting the drive, you can securely wipe out all the data on it by overwriting the entire drive with random data. This ensures that the data cannot be recovered by any data recovery tool.
Before formatting the drive, you can securely wipe out all the data on it by overwriting the entire drive with random data. This ensures that the data cannot be recovered by any data recovery tool.


Line 41: Line 74:


Be very careful before running the following command and irrevocably erase the drive data. The <code>of=...</code> part of the <code>dd</code> command must point to the target drive.
Be very careful before running the following command and irrevocably erase the drive data. The <code>of=...</code> part of the <code>dd</code> command must point to the target drive.
sudo dd if=/dev/zero of=/dev/sdb bs=4096 status=progress
 
<syntaxhighlight lang="bash">
sudo dd if=/dev/zero of=/dev/sdb bs=4096 status=progress
</syntaxhighlight>
 
Depending on the size of the drive, the process will take some time to complete.
Depending on the size of the drive, the process will take some time to complete.


Once the disk is erased, the <code>dd</code> command will print “No space left on device”:
Once the disk is erased, the <code>dd</code> command will print “No space left on device”:
15455776768 bytes (15 GB, 14 GiB) copied, 780 s, 19.8 MB/s  
 
dd: error writing '/dev/sdb': No space left on device
<syntaxhighlight lang="bash">
3777356+0 records in
15455776768 bytes (15 GB, 14 GiB) copied, 780 s, 19.8 MB/s  
3777355+0 records out
dd: error writing '/dev/sdb': No space left on device
15472047104 bytes (15 GB, 14 GiB) copied, 802.296 s, 19.3 MB/s
3777356+0 records in
3777355+0 records out
15472047104 bytes (15 GB, 14 GiB) copied, 802.296 s, 19.3 MB/s
</syntaxhighlight>
 
== Creating a Partition and Formatting ==
== Creating a Partition and Formatting ==
The most common file systems are exFAT and NTFS on Windows, EXT4 on Linux, and FAT32, which can be used on all operating systems.
The most common file systems are exFAT and NTFS on Windows, EXT4 on Linux, and FAT32, which can be used on all operating systems.


We will show you how to format your USB drive or SD card to FAT32 or EXT4. Use EXT4 if you intend to use the drive only on Linux systems, otherwise format it with FAT32. A single partition is sufficient for most use cases.
We will show you how to format your USB drive or SD card to FAT32 or EXT4. Use EXT4 if you intend to use the drive only on Linux systems, otherwise format it with FAT32. A single partition is sufficient for most use cases.
=== Format with FAT32 ===
=== Format with FAT32 ===
First, create the partition table by running the following command:
First, create the partition table by running the following command:
sudo parted /dev/sdb --script -- mklabel msdos
 
<syntaxhighlight lang="bash">
sudo parted /dev/sdb --script -- mklabel msdos
</syntaxhighlight>
 
Create a Fat32 partition that takes the whole space:
Create a Fat32 partition that takes the whole space:
sudo parted /dev/sdb --script -- mkpart primary fat32 1MiB 100%
 
<syntaxhighlight lang="bash">
sudo parted /dev/sdb --script -- mkpart primary fat32 1MiB 100%
</syntaxhighlight>
 
Format the boot partition to FAT32:
Format the boot partition to FAT32:
sudo mkfs.vfat -F32 /dev/sdb1


mkfs.fat 4.1 (2017-01-24)
<syntaxhighlight lang="bash">
sudo mkfs.vfat -F32 /dev/sdb1
</syntaxhighlight>
 
<syntaxhighlight lang="bash">
mkfs.fat 4.1 (2017-01-24)
</syntaxhighlight>
 
Once done, use the command below to print the partition table and verify that everything is set up correctly:
Once done, use the command below to print the partition table and verify that everything is set up correctly:
sudo parted /dev/sdb --script print
 
<syntaxhighlight lang="bash">
sudo parted /dev/sdb --script print
</syntaxhighlight>
 
The output should look something like this:
The output should look something like this:
<syntaxhighlight lang="bash">
  Model: Kingston DataTraveler 3.0 (scsi)
  Model: Kingston DataTraveler 3.0 (scsi)
  Disk /dev/sdb: 15.5GB
  Disk /dev/sdb: 15.5GB
Line 74: Line 138:
  Number  Start  End    Size    Type    File system  Flags
  Number  Start  End    Size    Type    File system  Flags
   1      1049kB  15.5GB  15.5GB  primary  fat32        lba
   1      1049kB  15.5GB  15.5GB  primary  fat32        lba
</syntaxhighlight>
That’s all! You have formatted your device.
That’s all! You have formatted your device.
=== Format with EXT4 ===
=== Format with EXT4 ===
Create a GPT partition table by issuing:
Create a GPT partition table by issuing:
sudo parted /dev/sdb --script -- mklabel gpt
 
<syntaxhighlight lang="bash">
sudo parted /dev/sdb --script -- mklabel gpt
</syntaxhighlight>
 
Run the following command to create a EXT4 partition that takes the whole space:
Run the following command to create a EXT4 partition that takes the whole space:
sudo parted /dev/sdb --script -- mkpart primary ext4 0% 100%
 
<syntaxhighlight lang="bash">
sudo parted /dev/sdb --script -- mkpart primary ext4 0% 100%
</syntaxhighlight>
 
Format the partition to ext4:
Format the partition to ext4:
sudo mkfs.ext4 -F /dev/sdb1


<syntaxhighlight lang="bash">
sudo mkfs.ext4 -F /dev/sdb1
</syntaxhighlight>
<syntaxhighlight lang="bash">
  mke2fs 1.44.1 (24-Mar-2018)
  mke2fs 1.44.1 (24-Mar-2018)
  /dev/sdb1 contains a vfat file system
  /dev/sdb1 contains a vfat file system
Line 94: Line 175:
  Creating journal (16384 blocks): done
  Creating journal (16384 blocks): done
  Writing superblocks and filesystem accounting information: done   
  Writing superblocks and filesystem accounting information: done   
</syntaxhighlight>
Verify it by printing the partition table:
Verify it by printing the partition table:
<code>sudo parted /dev/sdb --script print</code>
 
<syntaxhighlight lang="bash">
sudo parted /dev/sdb --script print
</syntaxhighlight>
 
The output should look something like this:
The output should look something like this:
<syntaxhighlight lang="bash">
  Model: Kingston DataTraveler 3.0 (scsi)
  Model: Kingston DataTraveler 3.0 (scsi)
  Disk /dev/sdb: 15.5GB
  Disk /dev/sdb: 15.5GB
Line 104: Line 193:
   
   
  Number  Start  End    Size    File system  Name    Flags
  Number  Start  End    Size    File system  Name    Flags
   1      1049kB  15.5GB  15.5GB  ext4        primary   
   1      1049kB  15.5GB  15.5GB  ext4        primary
</syntaxhighlight>  
 
== Source ==
== Source ==
* [https://linuxize.com/post/how-to-format-usb-sd-card-linux/ linuxize.com]
* [https://linuxize.com/post/how-to-format-usb-sd-card-linux/ linuxize.com]