Ubuntu:Format Drive CLI: Difference between revisions

Created page with "Before you can use an SD card or USB drive, it needs to be formatted and partitioned. Typically most USB drives and SD cards come preformatted using the FAT file system and do not need to be formatted out of the box. However, in some cases, you may need to format the drive. In Linux, you can use a graphical tool like GParted or command-line tools such as <code>fdisk</code> or <code>parted</code> to format the drive and create the required partitions. This article expla..."
 
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 <code>parted</code> # ==
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 --version
Line 15: Line 14:
  ...
  ...
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
  sudo apt updatesudo apt install parted
 
=== Install <code>parted</code> on CentOS and Fedora ===
=== Install <code>parted</code> on CentOS and Fedora # ===
  sudo yum install parted
  sudo yum install parted
 
== 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
  lsblk
Line 39: Line 35:
  [  +0.000232] sd 1:0:0:0: [sdb] 30218842 512-byte logical blocks: (15.5 GB/14.4 GiB)
  [  +0.000232] sd 1:0:0:0: [sdb] 30218842 512-byte logical blocks: (15.5 GB/14.4 GiB)
  ...
  ...
== 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 55: Line 50:
  3777355+0 records out
  3777355+0 records out
  15472047104 bytes (15 GB, 14 GiB) copied, 802.296 s, 19.3 MB/s
  15472047104 bytes (15 GB, 14 GiB) copied, 802.296 s, 19.3 MB/s
== 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:
Line 82: Line 75:
   1      1049kB  15.5GB  15.5GB  primary  fat32        lba
   1      1049kB  15.5GB  15.5GB  primary  fat32        lba
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
  sudo parted /dev/sdb --script -- mklabel gpt
Line 113: Line 105:
  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   
== 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]
[[Category:Linux]]
[[Category:Linux]]
[[Category:Ubuntu]]
[[Category:Ubuntu]]
[[Category:Tutorial]]
[[Category:Tutorial]]