MC, 2025
Ilustracja do artykułu: Linux Disk Management Commands: A Comprehensive Guide with Examples

Linux Disk Management Commands: A Comprehensive Guide with Examples

Managing disk space and partitions on a Linux system is an essential skill for system administrators, developers, and anyone who works with Linux servers. Whether you're setting up a new server, managing your data, or optimizing storage space, understanding how to use Linux disk management commands is key to success. In this article, we’ll explore the most commonly used disk management commands, provide practical examples, and offer tips for handling storage on Linux with ease.

What is Linux Disk Management?

Linux disk management involves the process of handling and organizing data stored on storage devices like hard drives, SSDs, and USB drives. Unlike other operating systems, Linux offers a wide range of command-line tools that allow you to manage disks, partitions, file systems, and storage devices effectively. Whether you’re formatting disks, resizing partitions, or checking disk usage, Linux provides powerful utilities to help you control and maintain your system’s storage.

Essential Linux Disk Management Commands

Below, we’ll explore several important Linux disk management commands that are crucial for anyone working with Linux systems. Each command will be explained with an example to help you understand its functionality and use.

1. lsblk - List Information About Block Devices

The lsblk command is used to list all the block devices attached to your Linux system. Block devices include hard drives, SSDs, CD-ROM drives, and more. This command provides valuable information about your system’s disk configuration, including device names, sizes, types, and mount points.

lsblk

Example Output:

NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0   500G  0 disk
├─sda1   8:1    0   100G  0 part /
└─sda2   8:2    0   400G  0 part /mnt
sdb      8:16   0   250G  0 disk
└─sdb1   8:17   0   250G  0 part /media/usb

The lsblk command shows you a tree structure of disks and partitions. It helps you quickly view your disks and understand their setup.

2. fdisk - Partition Table Manipulation

The fdisk command is a powerful tool used to create, delete, and modify disk partitions. It’s one of the most commonly used utilities for managing partitions on Linux. With fdisk, you can easily partition disks, create new partitions, and delete or resize existing ones.

To start partitioning a disk with fdisk, you need to specify the disk you want to work with. For example, to partition the first hard drive (/dev/sda), you would run:

sudo fdisk /dev/sda

Once inside the interactive fdisk menu, you can create, delete, and modify partitions using various options. The most common commands are:

  • n: Create a new partition.
  • d: Delete a partition.
  • w: Write changes to the disk.

For a simple example, let’s create a new partition on /dev/sda. Run the command sudo fdisk /dev/sda, press n to create a new partition, and follow the prompts to define the partition size and type.

3. mkfs - Create a Filesystem

Once you've created a partition with fdisk, you need to create a filesystem on that partition so that it can store data. The mkfs command is used to create different types of filesystems on partitions, such as ext4, xfs, and btrfs.

To format a partition as ext4 (the most common Linux filesystem), you can use the following command:

sudo mkfs.ext4 /dev/sda1

This will create an ext4 filesystem on the partition /dev/sda1. You can replace ext4 with other filesystem types, such as xfs or btrfs, depending on your needs.

4. mount - Mount a Filesystem

Once you've created a filesystem, the next step is to mount it. Mounting a filesystem makes it accessible for reading and writing. The mount command is used to attach a filesystem to a specific directory on your system, known as the mount point.

For example, to mount the partition /dev/sda1 to the /mnt directory, use the following command:

sudo mount /dev/sda1 /mnt

Now, the filesystem on /dev/sda1 is accessible from the /mnt directory. You can navigate to that directory and start adding files and directories.

5. df - Disk Space Usage

To check how much disk space is being used and how much is available, the df command is your go-to tool. It provides a summary of disk usage on all mounted filesystems, including the total space, used space, available space, and the mount point.

df -h

Example Output:

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        100G  20G  80G  20% /
/dev/sdb1        250G  100G 150G  40% /media/usb

The -h option makes the output human-readable, showing sizes in KB, MB, or GB instead of raw block numbers.

6. du - Disk Usage of Files and Directories

While df shows disk usage for the entire filesystem, the du command allows you to view the disk usage of specific files and directories. You can use this command to analyze which files or directories are consuming the most space on your disk.

To check the disk usage of a specific directory, use:

du -sh /path/to/directory

Example Output:

100M    /path/to/directory

In this case, -s provides the total size of the directory, and -h makes the size human-readable.

7. lsblk -f - Show Filesystem Information

If you want to see the type of filesystem on each partition, lsblk with the -f option will provide detailed information about each device, including filesystem type, label, and UUID.

lsblk -f

Example Output:

NAME   FSTYPE LABEL UUID                                 MOUNTPOINT
sda                                                       
├─sda1 ext4         a7f8db5f-bad4-4d1a-b21d-67a8bb2f030e /
└─sda2 ext4         98a6314f-d01b-4a2d-945d-ffcd61c9d116 /mnt

Conclusion

Managing disks and partitions on a Linux system might seem daunting at first, but with the right tools and commands, it becomes an easy and powerful task. In this article, we've covered some of the most essential Linux disk management commands that every Linux user should know. From listing block devices to formatting partitions and checking disk usage, these commands will help you maintain and optimize your storage system.

With this knowledge, you can now confidently manage your disks, partitions, and filesystems on Linux. Practice these commands, experiment with them on test systems, and soon you’ll be a disk management pro!

Komentarze (0) - Nikt jeszcze nie komentował - bądź pierwszy!

Imię:
Treść: