Disk Operations in Linux - Notes By ShariqSP
Managing Disks in Linux
Checking Disk Usage
To monitor disk usage and free space, you can use several commands. The most common ones are df and du.
df -h
In this command:
df -hdisplays the disk space usage for all mounted filesystems in a human-readable format (e.g., GB, MB).
du -sh /path/to/directory
In this command:
du -sh /path/to/directoryshows the disk usage of a specific directory in a human-readable format.
Formatting Disks
Formatting a disk prepares it for use by creating a filesystem. The mkfs command is used to format disks with various filesystems such as ext4, ntfs, and vfat.
sudo mkfs.ext4 /dev/sdX1
In this command:
sudo mkfs.ext4 /dev/sdX1formats the partition/dev/sdX1with the ext4 filesystem. ReplacesdX1with the appropriate device identifier.
To format with a different filesystem, replace ext4 with ntfs, vfat, etc.
Mounting and Unmounting Disks
Mounting a disk makes it accessible at a certain point in the filesystem hierarchy, while unmounting disconnects it. Use the mount and umount commands for these operations.
sudo mount /dev/sdX1 /mnt/point
In this command:
sudo mount /dev/sdX1 /mnt/pointmounts the partition/dev/sdX1to the directory/mnt/point. ReplacesdX1and/mnt/pointwith the appropriate device and mount point.
sudo umount /mnt/point
In this command:
sudo umount /mnt/pointunmounts the filesystem mounted at/mnt/point.
Checking Disk Health
To check the health of your disk and filesystem, you can use the fsck command. This command checks and repairs filesystem inconsistencies.
sudo fsck /dev/sdX1
In this command:
sudo fsck /dev/sdX1checks and repairs the filesystem on the partition/dev/sdX1. ReplacesdX1with the appropriate device identifier.
Note: It's best to run fsck when the filesystem is not mounted to avoid data corruption.
Conclusion
Understanding and managing disk operations in Linux is essential for maintaining system performance and data integrity. Regularly checking disk usage, formatting disks, and monitoring disk health ensures that your Linux environment remains stable and reliable.