Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Get Size Directory Command Line

· 4 min read · Home Tech

In this article
  1. What the Command Does
  2. Basic Usage Examples
  3. Understanding the Output
  4. Sorting and Finding the Largest Directories
  5. Common Mistakes and Pitfalls
  6. Alternatives to du
  7. Frequently Asked Questions

The quickest way to get the size of a directory from the command line is to use du -sh on Linux/macOS or du -s in PowerShell on Windows. This command prints the total disk usage of a folder and its contents in a human-readable format, such as 1.2G or 350M.

What the Command Does

du stands for “disk usage.” It walks through the directory tree, sums up the sizes of all files and subdirectories, and reports the total. The -s flag means “summarize,” showing only the grand total instead of every subdirectory. The -h flag (on Linux/macOS) makes the output human-readable, automatically choosing the appropriate unit (B, K, M, G, T). On Windows PowerShell, du is an alias for Measure-Object, but the classic Unix du is available as a standalone executable in most modern Windows systems. If you run du -sh in PowerShell and it fails, try du -s or use Get-ChildItem with Measure-Object as an alternative.

Basic Usage Examples

  • Linux/macOS: du -sh /path/to/folder shows the total size of that folder.
  • Linux/macOS: du -sh * lists the size of every item in the current directory, one per line.
  • Windows (PowerShell): du -s C:\path\to\folder shows the total in bytes; add -h if your version supports it.
  • Windows (Command Prompt): dir /s gives a byte count at the end, but it also lists all files, which can be slow for large trees.

Understanding the Output

The number du reports is the disk space used by the files, not necessarily the sum of their logical sizes. On filesystems with block allocation, a tiny file still occupies a full block (often 4 KB), so the disk usage can be larger than the actual file size. Conversely, sparse files or compressed filesystems may show less usage than the apparent size. If you need the apparent size (the sum of file lengths), use du -sh --apparent-size on Linux. The default is fine for most purposes, but it explains why du and ls -l sometimes disagree.

Sorting and Finding the Largest Directories

To find which subdirectory is eating the most space, combine du with sort and head:

  1. Run du -h --max-depth=1 (Linux/macOS) to list sizes of immediate subdirectories in human-readable form.
  2. Pipe to sort -hr to sort by size descending (the -h flag makes sort understand human-readable numbers).
  3. Pipe to head -20 to see the top 20.

On macOS, the --max-depth flag may not be available; use du -h -d 1 instead. On Windows, you can use du -s * | sort /R in Command Prompt, but the output is in bytes, making it harder to read.

Common Mistakes and Pitfalls

  • Forgetting the -s flag: Without it, du prints every subdirectory, which can be overwhelming.
  • Using -h with sort incorrectly: Sorting human-readable strings alphabetically puts 1.1G before 900M because “1” < “9”. Use sort -h on Linux, or stick to bytes for sorting.
  • Permission errors: du may not be able to read certain directories, printing warnings. Run with sudo (Linux/macOS) or as Administrator (Windows) if you need a complete count.
  • Symlinks: By default, du does not follow symbolic links. Use -L to follow them, but be cautious—this can lead to counting the same data multiple times if there are loops.

Alternatives to du

If you prefer a visual interface, graphical disk usage analyzers like WinDirStat (Windows), DaisyDisk (macOS), or Disk Usage Analyzer (Linux) provide interactive charts. For scripting, du is the standard tool because it is fast and available on virtually every Unix-like system. On Windows, you can also use Get-ChildItem -Recurse | Measure-Object -Property Length -Sum in PowerShell, but this is slower for large trees because it lists every file first. The du command is more efficient because it reads directory metadata directly.

Frequently Asked Questions

Why does du show a different size than the sum of file sizes?

As mentioned, du reports disk usage, which accounts for block allocation. A 1-byte file uses 4 KB on most filesystems, so 100 such files show as 400 KB, not 100 bytes. Use --apparent-size to get the logical size.

How do I get the size of a directory in megabytes or gigabytes?

Use -h to get human-readable output. If you need a specific unit, you can use du -sk to get kilobytes, then divide manually, or use du -sm for megabytes (on Linux, -m is equivalent to -B 1M). On macOS, -m works too.

Can I exclude certain subdirectories from the count?

Yes, du --exclude='pattern' (Linux) or du -X exclude.txt to read patterns from a file. On macOS, the --exclude flag is available in newer versions; otherwise use find with -prune for more complex logic.

What is the difference between du and df?

du measures the size of a specific directory tree, while df reports free space on an entire filesystem. They answer different questions: “How big is this folder?” vs. “How much space is left on the disk?”

Why does du take so long on large directories?

It must read the metadata of every file and directory in the tree. On network drives or filesystems with many small files, this can be slow. Use --max-depth=1 to limit the scan, or run it during off-peak hours.