Get Size Directory Command Line
In this article
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/foldershows 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\foldershows the total in bytes; add-hif your version supports it. - Windows (Command Prompt):
dir /sgives 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:
- Run
du -h --max-depth=1(Linux/macOS) to list sizes of immediate subdirectories in human-readable form. - Pipe to
sort -hrto sort by size descending (the-hflag makes sort understand human-readable numbers). - Pipe to
head -20to 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
-sflag: Without it,duprints every subdirectory, which can be overwhelming. - Using
-hwithsortincorrectly: Sorting human-readable strings alphabetically puts1.1Gbefore900Mbecause “1” < “9”. Usesort -hon Linux, or stick to bytes for sorting. - Permission errors:
dumay not be able to read certain directories, printing warnings. Run withsudo(Linux/macOS) or as Administrator (Windows) if you need a complete count. - Symlinks: By default,
dudoes not follow symbolic links. Use-Lto 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.