Determine disk usage from the command line on macOS

Disk management is an integral part of system administration, and the ability to analyze and track disk usage is crucial. In this article, we’ll delve into different techniques to determine disk usage from the command line on macOS.

Introduction

Command-line interface (CLI) based disk usage examination offers several advantages over graphical user interface (GUI) alternatives, particularly regarding flexibility and control. The CLI allows users to inspect disk usage more granularly and versatilely, as you'll see in this article.

Before diving into the various commands, it’s essential to understand some basic disk usage concepts:

  • Disk usage: Disk usage is the amount of hard disk space used by a file, directory, or partition. It's typically measured in bytes (B), kilobytes (KB), megabytes (MB), gigabytes (GB), and terabytes (TB).

  • Disk capacity: Disk capacity is the total amount of data a disk can store. It's crucial to regularly monitor disk usage to ensure that it doesn't approach or exceed the disk's capacity.

  • File system: A file system is the way an operating system organizes files on a disk. macOS uses a file system called APFS (Apple File System) or HFS+ for older versions.

Now, let's explore some commands to help determine disk space usage. You can also use these commands on Linux.

The df command

The df command, short for "disk free", displays the amount of disk space used and available on file systems. It's one of the most commonly used commands for disk space analysis.

To use the df command, open a Terminal and type df:

1df

By default, df displays disk space in 1-kilobyte blocks. For a more human-readable format (in MB or GB), use the -h option:

1df -h

The df -h command results provide an overview of your file system's disk usage.

The du command

While df provides a file system overview, the du command, short for "disk usage", helps you calculate the disk usage of directories and files.

Here's how you use du:

1du

The output lists the disk usage of all directories and subdirectories in the current working directory. If you want the disk usage of a specific directory, use du with the directory path:

1du /path/to/directory

To display the disk usage in a human-readable format, use the -h option:

1du -h /path/to/directory

For a summary of a directory's total size, use the -s option:

1du -sh /path/to/directory

The ncdu command

While not installed by default on macOS, ncdu (NCurses Disk Usage) is a simple, fast, and easy-to-use CLI disk usage analyzer. It provides a more detailed and interactive way to navigate the file system and inspect disk usage.

Use Homebrew to install ncdu:

1brew install ncdu

To analyze a directory, use ncdu with the directory path:

1ncdu /path/to/directory

ncdu displays a navigable interface with directories and files sorted by their size.

The ls command

Another way to examine disk usage is using the ls command, typically used for listing directory contents. When combined with the -l and -h options, ls shows the size of files and directories in a human-readable format:

1ls -lh

However, ls doesn't calculate the total size of directories, making it less suitable for comprehensive disk usage analysis.

The find command

The find command is a powerful tool for searching files based on various criteria, including size. You can use find to locate all files exceeding a certain size:

1find /path/to/directory -size +1G

This command finds all files larger than 1GB in the specified directory and its subdirectories.

Summary

CLI disk usage analysis tools offer an in-depth view of how space is used on your system. They allow you to identify large files or directories, monitor your disk's available space, and make informed decisions about data management.

Each tool has strengths and weaknesses, so you might use a combination depending on your needs. The key is understanding how each command works and how to use it effectively.

References