mdu Command in Linux

Introduction

In this tutorial, you will discover how to leverage the Linux mdu command, a powerful tool also known as the "Disk Usage" command, to efficiently assess the disk usage of directories and files. This guide covers understanding the fundamental purpose and correct syntax of the mdu command, accurately measuring the disk usage of a directory, and effectively excluding specific files and directories from the mdu command's output. Mastering these techniques will empower you to effectively manage disk space and gain insights into the storage demands of your Linux environment, essential skills for any systemadmin.

This tutorial provides clear, step-by-step instructions and practical examples to smoothly guide you through each process. By the conclusion of this tutorial, you will possess a comprehensive understanding of how to effectively utilize the mdu command for continuous monitoring and strategic optimization of disk usage on your Linux system. Learn how to become a proficient systemadmin with this essential Linux command.

Understand the Purpose and Syntax of the mdu Command

This section focuses on familiarizing you with the purpose and correct syntax of the mdu command in Linux environments. The mdu command, a crucial utility for any systemadmin, is specifically designed to measure the disk usage of both directories and individual files.

To fully grasp the utility of the mdu command, let's begin by examining its syntax:

mdu [options] [directory]

The standard syntax of the mdu command comprises the following components:

  • mdu: This is the name of the command itself.
  • [options]: These are optional flags or parameters that can be included to modify the command's default behavior.
  • [directory]: This represents the specific directory or file path for which you intend to measure disk usage.

Several commonly used options for the mdu command are:

  • -h: This option instructs the command to display disk usage in a more easily understandable, human-readable format (e.g., KB, MB, GB).
  • -s: Use this option to display only the total size of the directory, instead of displaying the size of each individual file within.
  • -x: This option is used to exclude directories that reside on different file systems.
  • -a: By using this option, you can include all files in the output, even those that begin with a dot (hidden files).

Let's explore the use of the mdu command by executing it with a few of these options:

$ mdu -h ~/project

Example output:

1.2M	/home/labex/project

In this illustrative example, we've used the -h option to format the disk usage output in a human-readable manner. The output indicates that the ~/project directory is utilizing 1.2 MB of disk space.

$ mdu -s ~/project

Example output:

1.2M	/home/labex/project

Here, we employed the -s option to show the total size of the ~/project directory, rather than the sizes of individual files contained within.

By thoroughly understanding the purpose and syntax of the mdu command, you'll be well-equipped to efficiently measure disk usage for directories and files across your entire Linux environment.

Measure the Disk Usage of a Directory

This section details how to employ the mdu command to measure the disk usage of a directory within your Linux system.

To start, let's create a sample directory and populate it with some files to facilitate our demonstration:

$ mkdir -p ~/project/documents
$ touch ~/project/documents/file1.txt ~/project/documents/file2.txt ~/project/documents/file3.txt

Now, we'll utilize the mdu command to measure the disk usage of the newly created ~/project/documents directory:

$ mdu ~/project/documents

Example output:

12K	/home/labex/project/documents/file1.txt
12K	/home/labex/project/documents/file2.txt
12K	/home/labex/project/documents/file3.txt
36K	/home/labex/project/documents

The output clearly presents the disk usage for each individual file within the ~/project/documents directory, alongside the total disk usage of the directory itself.

For improved readability, the -h option can be used to present the disk usage in a more human-friendly format:

$ mdu -h ~/project/documents

Example output:

12K	/home/labex/project/documents/file1.txt
12K	/home/labex/project/documents/file2.txt
12K	/home/labex/project/documents/file3.txt
36K	/home/labex/project/documents

In this example, disk usage values are displayed in kilobytes (K), facilitating a quicker and more intuitive understanding of the actual size of both the files and the directory.

By effectively combining the mdu command with the appropriate options, you can accurately measure the disk usage of directories and files in your Linux environment, empowering you to effectively manage disk space as a systemadmin or Linux user.

Exclude Specific Files and Directories from the mdu Command

This section explains how to selectively exclude specific files and directories from the mdu command's disk usage calculation, allowing for more focused analysis.

Let's start by creating additional files and directories within the ~/project directory to demonstrate the exclusion functionality:

$ mkdir -p ~/project/temp ~/project/backup
$ touch ~/project/temp/file4.txt ~/project/backup/file5.txt

Now, we'll run the mdu command to measure the disk usage of the ~/project directory, including all subdirectories:

$ mdu ~/project

Example output:

12K	/home/labex/project/documents/file1.txt
12K	/home/labex/project/documents/file2.txt
12K	/home/labex/project/documents/file3.txt
12K	/home/labex/project/temp/file4.txt
12K	/home/labex/project/backup/file5.txt
84K	/home/labex/project

As the output demonstrates, the mdu command includes the disk usage of both the temp and backup directories when calculating the total disk usage of the ~/project directory.

To exclude specific directories from the mdu command's calculation, the -x option is your tool of choice:

$ mdu -x ~/project/temp -x ~/project/backup ~/project

Example output:

12K	/home/labex/project/documents/file1.txt
12K	/home/labex/project/documents/file2.txt
12K	/home/labex/project/documents/file3.txt
60K	/home/labex/project

In this example, we've utilized the -x option twice, effectively excluding both the ~/project/temp and ~/project/backup directories from the overall disk usage calculation. This helps refine the output for the systemadmin.

The -x option can also be used to exclude specific files from the mdu command's output, providing even finer control:

$ mdu -x ~/project/documents/file2.txt ~/project

Example output:

12K	/home/labex/project/documents/file1.txt
12K	/home/labex/project/documents/file3.txt
24K	/home/labex/project/documents
60K	/home/labex/project

By strategically using the -x option, you gain the ability to selectively exclude both files and directories from the mdu command's disk usage calculations. This allows you, as a systemadmin, to focus precisely on the information that is most relevant to your current needs and optimize your Linux system.

Summary

In this tutorial, you have gained a thorough understanding of the purpose and syntax of the mdu command in Linux, a tool essential for measuring disk usage of directories and files. You explored the command's basic syntax, including key options such as -h for human-readable output, -s for total directory size, and -x for excluding directories on different file systems. Furthermore, you learned how to practically apply the mdu command to measure directory disk usage, creating sample directories and files to test the command effectively. This knowledge is critical for any aspiring or current systemadmin looking to manage Linux systems efficiently and maintain optimal disk space utilization, even as root.

400+ Linux Commands