Human Readable Sizes from the Command Line
In this article
To see file sizes in a human readable format from the command line, use the -h flag with ls (e.g., ls -lh) or the --human-readable option with du and df (e.g., du -sh). These options convert raw byte counts into units like KB, MB, and GB, making output much easier to scan.
Why Raw Bytes Are Hard to Read
Unix and Linux systems store file sizes as a plain number of bytes. A file that is 1,500,000 bytes is technically accurate, but most people cannot quickly tell whether that is larger or smaller than a file listed as 1,400,000 bytes. Human readable formats group bytes into powers of 1024 (in most command line tools) and append a unit suffix, so you see 1.4M instead of 1468006. This is especially useful when listing directories with mixed file types, checking disk usage, or copying files where size matters.
How the -h Flag Works
The -h flag is a common convention in GNU coreutils, which provide the standard file and text utilities on Linux and macOS (macOS uses BSD versions, but the flag is supported). When you run ls -lh, each file’s size is displayed with one digit after the decimal point and a unit letter:
Bfor bytes (no suffix in some versions)Kfor kibibytes (1024 bytes)Mfor mebibytes (1024 KiB)Gfor gibibytes (1024 MiB)Tfor tebibytes (1024 GiB)
The underlying calculation divides the byte count by 1024 repeatedly until the value is less than 1024. For example, a file of 5,242,880 bytes becomes 5.0M because 5,242,880 / 1024 = 5120, and 5120 / 1024 = 5.0.
Commands That Support Human Readable Sizes
Several common commands accept the -h or --human-readable option:
ls -lh– lists files with sizes in a readable format.du -h– shows disk usage of files and directories (add-sfor a summary, e.g.,du -sh *).df -h– displays filesystem disk space usage in readable units.stat -c %s file– does not have-h, but you can pipe tonumfmt(see below).
If your version of ls does not support -h (rare on modern systems), you can use ls -l --block-size=K to force a specific unit, or use ls -l | awk to convert manually.
How to Choose Between ls, du, and df
Each command serves a different purpose:
ls -lh– best for viewing sizes of individual files in a directory. It shows the apparent size (the actual length of the file data), not the disk blocks used.du -sh– best for seeing how much disk space a directory or file consumes, including blocks reserved for metadata. This is what you want when cleaning up disk space.df -h– best for checking overall filesystem capacity and free space. It reports on mounted partitions, not individual files.
For a quick overview of the largest files in a folder, du -ah . | sort -rh | head -20 is a common one-liner. It lists all files and directories, sorts them by human readable size in reverse, and shows the top 20.
Common Problems and Pitfalls
One frequent issue is that ls -lh shows sizes in powers of 1024, while some tools (like ls -lh on macOS) may use 1000 for certain options. Always check your system’s manual (man ls) if you see unexpected numbers.
Another pitfall is that du by default counts disk blocks, which can be larger than the apparent file size for small files. For example, a 10-byte file may occupy 4096 bytes on disk. Using du -h without --apparent-size shows the disk usage, not the file size. If you need the apparent size, use du -h --apparent-size.
Finally, when scripting, remember that human readable output is not ideal for arithmetic. If you need to compare sizes in a script, use raw bytes (ls -l without -h) and convert only for display.
Alternatives and Formatting Options
If you need more control over the format, numfmt is a dedicated utility that converts numbers to human readable strings. For example:
numfmt --to=iec 5242880This outputs 5.0M. You can also use --to=si for powers of 1000 (e.g., 5.2M). This is handy when scripting your own output.
Another alternative is to use ls -l --block-size=M to force sizes in megabytes, but this rounds to the nearest MB, which may be too coarse. The -h flag is usually the best balance of readability and precision.
Frequently Asked Questions
Why does ls -lh show 4.0K for a file that is only 10 bytes?
Because ls -lh reports the apparent size, but many filesystems allocate disk space in blocks (often 4096 bytes). The -h flag in ls shows the apparent size, so a 10-byte file should display as 10, not 4.0K. If you see 4.0K, you may be using du or a different command. To see the true file size, use stat -c %s file.
Can I make ls always use human readable sizes by default?
Yes, you can add an alias in your shell configuration file (e.g., .bashrc or .zshrc): alias ls='ls -h'. This will apply to all interactive sessions. For scripts, it is safer to call ls -h explicitly.
What is the difference between KB and KiB?
KB (kilobyte) is 1000 bytes, while KiB (kibibyte) is 1024 bytes. Most command line tools use binary units (KiB, MiB) but label them as KB, MB, etc. This is a historical convention. If you need decimal units, use numfmt --to=si.
How do I see human readable sizes for files in a directory tree?
Use du -ah to list all files and directories recursively with human readable sizes. For a summary per directory, use du -h without -a. To sort by size, pipe to sort -h (GNU sort supports -h for human numeric sort).
Does the -h flag work on Windows or macOS?
On macOS, the BSD versions of ls, du, and df support -h. On Windows, the native command prompt does not have these tools, but if you use PowerShell, you can get human readable sizes with Get-ChildItem | Format-Table Length or use the --human-readable option in Windows Subsystem for Linux (WSL).