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 -h
displays 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/directory
shows 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/sdX1
formats the partition/dev/sdX1
with the ext4 filesystem. ReplacesdX1
with 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/point
mounts the partition/dev/sdX1
to the directory/mnt/point
. ReplacesdX1
and/mnt/point
with the appropriate device and mount point.
sudo umount /mnt/point
In this command:
sudo umount /mnt/point
unmounts 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/sdX1
checks and repairs the filesystem on the partition/dev/sdX1
. ReplacesdX1
with 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.