Linux mdu Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Linux mdu command, also known as the "Disk Usage" command, to measure the disk usage of directories and files. The lab covers understanding the purpose and syntax of the mdu command, measuring the disk usage of a directory, and excluding specific files and directories from the mdu command. This will help you effectively manage disk space and understand the storage requirements of your Linux environment.

The lab provides step-by-step instructions and practical examples to guide you through the process. By the end of the lab, you will have a better understanding of how to utilize the mdu command to monitor and optimize disk usage on your Linux system.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") linux/BasicFileOperationsGroup -.-> linux/rm("`File Removing`") linux/BasicFileOperationsGroup -.-> linux/wc("`Text Counting`") linux/FileandDirectoryManagementGroup -.-> linux/find("`File Searching`") linux/SystemInformationandMonitoringGroup -.-> linux/df("`Disk Space Reporting`") linux/SystemInformationandMonitoringGroup -.-> linux/du("`File Space Estimating`") subgraph Lab Skills linux/ls -.-> lab-422798{{"`Linux mdu Command with Practical Examples`"}} linux/rm -.-> lab-422798{{"`Linux mdu Command with Practical Examples`"}} linux/wc -.-> lab-422798{{"`Linux mdu Command with Practical Examples`"}} linux/find -.-> lab-422798{{"`Linux mdu Command with Practical Examples`"}} linux/df -.-> lab-422798{{"`Linux mdu Command with Practical Examples`"}} linux/du -.-> lab-422798{{"`Linux mdu Command with Practical Examples`"}} end

Understand the Purpose and Syntax of the mdu Command

In this step, you will learn about the purpose and syntax of the mdu command in Linux. The mdu command, also known as the "Disk Usage" command, is a utility that allows you to measure the disk usage of directories and files.

To understand the purpose of the mdu command, let's first explore its syntax:

mdu [options] [directory]

The basic syntax of the mdu command includes the following:

  • mdu: The command name.
  • [options]: Optional flags or parameters that modify the behavior of the command.
  • [directory]: The directory or file path you want to measure the disk usage for.

Some common options for the mdu command include:

  • -h: Display the disk usage in human-readable format (e.g., KB, MB, GB).
  • -s: Display the total size of the directory, instead of the individual file sizes.
  • -x: Exclude directories on different file systems.
  • -a: Include all files, even those with leading dots (hidden files).

Let's try running the mdu command with some of these options:

$ mdu -h ~/project

Example output:

1.2M	/home/labex/project

In this example, we used the -h option to display the disk usage in a human-readable format. The output shows that the ~/project directory is using 1.2 MB of disk space.

$ mdu -s ~/project

Example output:

1.2M	/home/labex/project

In this example, we used the -s option to display the total size of the ~/project directory, instead of the individual file sizes.

Understanding the purpose and syntax of the mdu command will help you effectively measure the disk usage of directories and files in your Linux environment.

Measure the Disk Usage of a Directory

In this step, you will learn how to use the mdu command to measure the disk usage of a directory in your Linux environment.

First, let's create a sample directory and some files to work with:

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

Now, let's use the mdu command to measure the disk usage of the ~/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 shows the disk usage of each file in the ~/project/documents directory, as well as the total disk usage of the directory itself.

You can also use the -h option to display the disk usage in a more human-readable 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, the disk usage is displayed in kilobytes (K), making it easier to understand the actual size of the files and directory.

By using the mdu command with the appropriate options, you can effectively measure the disk usage of directories and files in your Linux environment.

Exclude Specific Files and Directories from the mdu Command

In this step, you will learn how to exclude specific files and directories from the mdu command's disk usage calculation.

Let's create some additional files and directories in the ~/project directory:

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

Now, let's run the mdu command to measure the disk usage of the ~/project directory:

$ 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 you can see, the mdu command includes the disk usage of the temp and backup directories in the total disk usage of the ~/project directory.

To exclude specific directories from the mdu command, you can use the -x option:

$ 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 used the -x option twice to exclude the ~/project/temp and ~/project/backup directories from the disk usage calculation.

You can also use the -x option to exclude specific files from the mdu command's output:

$ 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 using the -x option, you can selectively exclude files and directories from the mdu command's disk usage calculation, allowing you to focus on the relevant information for your needs.

Summary

In this lab, you learned about the purpose and syntax of the mdu command in Linux, which is used to measure the disk usage of directories and files. You explored the basic syntax of the mdu command, including the available options such as -h to display the disk usage in human-readable format, -s to display the total size of a directory, and -x to exclude directories on different file systems. You also learned how to use the mdu command to measure the disk usage of a directory, creating a sample directory and files to test the command.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like