Linux provides several commands for managing disks, partitions, and file systems. Here are some essential disk management commands along with their usage examples:
🔥 Summary Table of Commands
Command | Description |
---|---|
lsblk |
List block devices |
fdisk |
Partition a disk |
parted |
Advanced partitioning |
df -h |
Check disk usage |
du -sh |
Check directory size |
mount |
Mount a file system |
umount |
Unmount a file system |
mkfs.ext4 |
Format partition as ext4 |
blkid |
Get partition UUID |
fsck |
Check and repair file system |
resize2fs |
Resize ext4 file system |
lvdisplay |
Display LVM logical volumes |
pvcreate |
Initialize disk for LVM |
vgcreate |
Create a volume group |
lvcreate |
Create a logical volume |
swapon -s |
Show active swap |
iostat |
Show disk I/O stats |
smartctl |
Check disk health |
1. lsblk
(List Block Devices)
Example:
Output: Displays a tree-like structure of disks and partitions.
Usage: Shows file system details of each partition.
2. fdisk
(Partition Management)
Example:
Usage: Lists all partitions on available disks.
Usage: Opens interactive mode to create, delete, or modify partitions on /dev/sdb
.
3. parted
(Advanced Partition Management)
Example:
Usage: Starts parted
to manage partitions on /dev/sdb
.
Output: Lists all disk partitions with details.
4. df
(Check Disk Space Usage)
Example:
Output: Shows disk space usage in a human-readable format.
Usage: Displays file system types along with disk usage.
5. du
(Check Directory Size)
Example:
Output: Displays the total size of the /home/user
directory.
Usage: Lists the top 10 largest files/folders.
6. mount
(Mount a File System)
Example:
Usage: Mounts /dev/sdb1
partition to /mnt
.
Output: Shows currently mounted file systems in a readable format.
7. umount
(Unmount a File System)
Example:
Usage: Unmounts the /mnt
directory.
Usage: Unmounts the specific partition /dev/sdb1
.
8. mkfs
(Format a Partition)
Example:
Usage: Formats /dev/sdb1
as an ext4
file system.
Usage: Formats /dev/sdb1
as an XFS
file system.
9. blkid
(Get UUID of Partitions)
Example:
Output: Lists UUIDs and file system types of all partitions.
Usage: Shows UUID and file system type for /dev/sdb1
.
10. tune2fs
(Modify ext Filesystem Parameters)
Example:
Output: Displays file system settings of /dev/sda1
.
Usage: Reserves 5% of disk space for system use.
11. fsck
(Check and Repair File System)
Example:
Usage: Checks and repairs file system errors on /dev/sda1
.
12. resize2fs
(Resize ext File System)
Example:
Usage: Shrinks or expands /dev/sda1
to 20GB.
Usage: Expands the file system to fill available space.
13. lvdisplay
(Show LVM Logical Volumes)
Example:
Output: Displays details of logical volumes.
14. pvcreate
(Initialize a Disk for LVM)
Example:
Usage: Prepares /dev/sdb
as a physical volume for LVM.
15. vgcreate
(Create a Volume Group)
Example:
Usage: Creates a volume group named my_vg
using /dev/sdb
.
16. lvcreate
(Create a Logical Volume)
Example:
Usage: Creates a 10GB logical volume my_lv
inside my_vg
.
17. mount -o loop
(Mount an ISO File)
Example:
Usage: Mounts an ISO file to /mnt
.
18. swapon
and swapoff
(Manage Swap Space)
Example:
Output: Shows active swap partitions.
Usage: Enables swap space on /dev/sdb1
.
Usage: Disables swap space.
19. iostat
(Disk I/O Statistics)
Example:
Output: Shows real-time disk read/write stats.
20. smartctl
(Check Disk Health)
Example:
Output: Displays SMART health information of /dev/sda
.
Usage: Runs a short disk self-test.