Mastering Disk Commands in Linux: 15 Essential Commands You Must Know

Managing disk space is crucial for every Linux user, whether you’re an administrator or a casual user. This post will delve into disk commands in Linux, covering essential commands that help you view, monitor, and manage your disks effectively. By the end, you’ll have a solid understanding of how to keep your system running smoothly.

disk commands in Linux

1. Viewing Disk Information

lsblk

The lsblk command provides a detailed view of all block devices on your system.

  • Syntax:
  lsblk [options]
  • Flags:
  • -a: Show all devices, including empty ones.
  • -f: Display filesystem information.
  • -o: Specify output columns, such as NAME, FSTYPE, SIZE, etc.

  • Example:
  lsblk -f

fdisk

fdisk is a powerful command-line utility used for partitioning disks. It allows you to create, delete, and modify disk partitions.

  • Syntax:
  fdisk [options] <device>
  • Flags:
  • -l: List all partitions on all devices.
  • -u: Use sectors instead of cylinders.

  • Example:
  sudo fdisk -l

df

The df command reports the amount of disk space used and available on mounted filesystems.

  • Syntax:
  df [options] [file...]
  • Flags:
  • -h: Display sizes in a human-readable format.
  • -T: Show filesystem type.

  • Example:
  df -h

du

The du command is used to estimate file space usage, displaying the disk space used by files and directories.

  • Syntax:
  du [options] [file...]
  • Flags:
  • -h: Display sizes in a human-readable format.
  • -s: Show only a total for each argument.
  • -a: Show sizes for files as well.

  • Example:
  du -sh /path/to/directory

2. Monitoring Disk Usage

iostat

The iostat command reports CPU and input/output statistics for devices and partitions.

  • Syntax:
  iostat [options] [interval] [count]
  • Flags:
  • -x: Show extended statistics.
  • -p: Report statistics per partition.

  • Example:
  iostat -x 1

iotop

iotop is a top-like utility that shows which processes are using the most I/O resources in real-time.

  • Syntax:
  iotop [options]
  • Flags:
  • -o: Only show processes or threads actually doing I/O.
  • -P: Show I/O usage by process.

  • Example:
  sudo iotop -o

smartctl

smartctl is part of the SMART (Self-Monitoring, Analysis, and Reporting Technology) suite, which helps monitor the health of your drives.

  • Syntax:
  smartctl [options] <device>
  • Flags:
  • -a: Show all SMART information.
  • -t short: Run a short self-test.

  • Example:
  sudo smartctl -a /dev/sda

ncdu

ncdu (NCurses Disk Usage) is a disk usage analyzer with an ncurses interface that allows you to explore disk usage interactively.

  • Syntax:
  ncdu [options] [directory]
  • Flags:
  • -x: Stay on the same filesystem.
  • -q: Quiet mode (less output).

  • Example:
  ncdu /

tune2fs

tune2fs allows you to adjust tunable filesystem parameters on ext2/ext3/ext4 filesystems.

  • Syntax:
  tune2fs [options] <device>
  • Flags:
  • -l: List current parameters.
  • -o: Change filesystem options.

  • Example:
  sudo tune2fs -l /dev/sda1

3. Disk Performance Tuning

hdparm

hdparm is used to set or view hardware parameters of hard disk drives.

  • Syntax:
  hdparm [options] <device>
  • Flags:
  • -I: Show information about the drive.
  • -t: Measure the speed of reading from the disk.

  • Example:
  sudo hdparm -t /dev/sda

fstrim

fstrim is used to discard unused blocks on SSDs, helping to maintain performance.

  • Syntax:
  fstrim [options] <mount_point>
  • Flags:
  • -v: Show verbose output.

  • Example:
  sudo fstrim -v /

btrfs

The btrfs command is a filesystem and logical volume manager designed to address the shortcomings of other filesystems.

  • Syntax:
  btrfs [options] <command> [subcommand]
  • Flags:
  • device: Specify the device to operate on.
  • filesystem: Operate on a filesystem.

  • Example:
  sudo btrfs device add /dev/sdb /mnt/my_btrfs

lvm

The Logical Volume Manager (LVM) is used for managing disk drives and similar resources.

  • Syntax:
  lvm [options] <command>
  • Flags:
  • lvcreate: Create a new logical volume.
  • lvremove: Remove a logical volume.

  • Example:
  sudo lvcreate -L 10G -n my_volume my_volume_group

resize2fs

resize2fs is used to resize ext2/ext3/ext4 filesystems.

  • Syntax:
  resize2fs [options] <device>
  • Flags:
  • -f: Force resizing.
  • -p: Print progress.

  • Example:
  sudo resize2fs /dev/sda1

Summarizing on Disk Commands in Linux

Understanding disk commands in Linux is crucial for effective system management. With commands like lsblk, df, and iostat, you can monitor your disk usage and optimize performance. Master these commands to keep your Linux system running smoothly. For further reading and advanced techniques, visit GeekersHub and explore more about Linux system management. Additionally, check out resources from the Linux Foundation for comprehensive insights and training on Linux systems.

FAQs

  1. What is the purpose of the lsblk command? It provides a list of all block devices on your system.
  2. How do I create a new partition using fdisk? Run sudo fdisk /dev/sdX to manage partitions, where X is your disk.
  3. What does the df command display? It shows the amount of disk space used and available on mounted filesystems.
  4. How can I check the disk usage of a specific directory? Use du -sh /path/to/directory to see its size.
  5. What information does iostat provide? It reports CPU and I/O statistics for devices and partitions.
  6. How can I monitor real-time disk I/O usage? Use iotop to see which processes are using the most I/O.
  7. What is the function of smartctl? It monitors the health of hard drives using SMART data.
  8. How does hdparm help optimize disk performance? It adjusts settings like caching and read/write parameters.
  9. What does fstrim do for SSDs? It discards unused blocks to enhance SSD performance.
  10. How can tune2fs improve filesystem performance? It modifies parameters for better performance based on usage.
  11. Can I use du to see sizes for all files in a directory? Yes, du -ah /path/to/directory lists sizes for all files.
  12. How do I interpret the output of df? It shows total size, used space, available space, and mount points.
  13. **What risks are involved with fdisk?** Incorrect use can lead to data loss or corruption.
  14. Is iotop included in all Linux distributions? It may require installation via your package manager.
  15. How do I install smartctl if it’s missing? Install it as part of the smartmontools package through your package manager.