Disk Management Commands in Linux
Introduction
Disk management is a fundamental aspect of system administration in Linux. It involves managing disk partitions, filesystems, and storage devices. This tutorial will cover common disk management commands used in Linux, complete with detailed explanations and practical examples.
Checking Disk Space: df
The df command displays the amount of disk space used and available on Linux filesystems.
Example:
df -h
/dev/sda1 20G 5.9G 13G 32% /
tmpfs 3.9G 1.2M 3.9G 1% /dev/shm
Checking Disk Usage: du
The du command estimates file space usage. It can be used to find the size of a folder or file.
Example:
du -sh /home/user
Listing Block Devices: lsblk
The lsblk command lists information about all available or the specified block devices.
Example:
lsblk
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 20G 0 part /
sdb 8:16 0 50G 0 disk
├─sdb1 8:17 0 50G 0 part /mnt/data
Managing Partitions: fdisk
The fdisk command is a text-based utility for viewing and managing disk partitions. It is used for creating, deleting, and modifying partitions on a disk.
Example:
sudo fdisk /dev/sda
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x12345678
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 41943039 41940992 20G 83 Linux
Creating Filesystems: mkfs
The mkfs command is used to create a filesystem on a partition. Different filesystems can be specified using options.
Example:
sudo mkfs.ext4 /dev/sdb1
Creating filesystem with 13107200 4k blocks and 3276800 inodes
Filesystem UUID: 1234abcd-5678-efgh-90ij-klmnopqrstuv
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632,
2654208, 4096000, 7962624, 11239424
Allocating group tables: done
Writing inode tables: done
Creating journal (65536 blocks): done
Writing superblocks and filesystem accounting information: done
Mounting Filesystems: mount
The mount command attaches a filesystem to the directory tree at the specified mount point.
Example:
sudo mount /dev/sdb1 /mnt/data
Unmounting Filesystems: umount
The umount command detaches the filesystem mounted at the specified directory or device.
Example:
sudo umount /mnt/data
Viewing Disk Usage by Files: ncdu
The ncdu command provides a disk usage analyzer with an ncurses interface.
Example:
ncdu /home/user
Disk Health Check: smartctl
The smartctl command is a tool for querying and controlling SMART (Self-Monitoring, Analysis, and Reporting Technology) attributes of hard drives.
Example:
sudo smartctl -a /dev/sda
=== START OF INFORMATION SECTION ===
Model Family: Toshiba 2.5" HDD MQ01ABD...
Device Model: TOSHIBA MQ01ABD100
Serial Number: 1234567890
LU WWN Device Id: 5 000039 123456789
Firmware Version: AX0P3D
User Capacity: 1,000,204,886,016 bytes [1.00 TB]
Sector Sizes: 512 bytes logical, 4096 bytes physical
Rotation Rate: 5400 rpm
Device is: In smartctl database [for details use: -P show]
ATA Version is: ACS-2, ATA8-ACS T13/2015-D revision 3
SATA Version is: SATA 3.0, 6.0 Gb/s (current: 6.0 Gb/s)
Local Time is: Fri Oct 4 13:09:11 2019 UTC
SMART support is: Available - device has SMART capability.
SMART support is: Enabled