To format a partition with the ext4 or xfs filesystem on a Linux system, you can use the mkfs.ext4 command. Make sure you have the necessary permissions and have identified the correct partition and backed up all the data if available before proceeding, as formatting will erase all data on the selected partition. Here are the steps:
1. Identify the Partition :
Use the lsblk or fdisk -l or parted command to list the available disks and partitions and identify the one you want to format. Take note of the device name (e.g., /dev/sdXn), where “X” represents the drive letter and n represents the partition number.
sudo lsblk
2. Unmount the Partition (if mounted) :
If the partition is currently mounted, you should unmount it before formatting. Use the `umount` command followed by the device name or mount point:
sudo umount /dev/sdXn
or
sudo umount /mountpoint
Replace /dev/sdXn with the actual device name or mount point.
3. Format the Partition with ext4 :
Use the mkfs.ext4 command to format the partition with the ext4 filesystem. Here’s the basic syntax:
sudo mkfs.ext4 /dev/sdXn
Replace /dev/sdXn with the actual device name of the partition you want to format.
For example, to format /dev/sda5 with ext4:
sudo mkfs.ext4 /dev/sda5
To format /dev/sda5 with xfs:
sudo mkfs.xfs /dev/sda5
4. Wait for Formatting to Complete :
The mkfs.ext4 and mkfs.xfs command will format the partition, which may take a few moments depending on the size of the partition.
5. Label the Filesystem (Optional) :
You can label the ext4 filesystem with a name for easier identification. Use the -L option followed by the label name when formatting:
sudo mkfs.ext4 -L mydata /dev/sdXn
Replace mydata with your desired label.
6. Mount the Formatted Partition :
After formatting, you can mount the partition again. You can use an existing mount point or create a new one:
# Create a new mount point if needed
sudo mkdir /opt/software
sudo mount /dev/sdXn /opt/software
Replace /dev/sdXn with the actual device name and /mnt/mydata with your desired mount point.
7. Update /etc/fstab (Optional) :
If you want the partition to be mounted automatically at boot viz., after reboot of the system, you can add an entry to the /etc/fstab file. Be sure to include the appropriate mount options. For example:
/dev/sdXn /opt/software ext4 defaults 0 0
Save the file after making changes.
Your partition is now formatted with the ext4 filesystem and can be mounted and used as needed. Be cautious when formatting partitions, as it erases all data on the selected partition.
Click here!! to know how to create a logical partions in linux.
How do you feel about this post? Drop your comments below..