On Linux systems, disk space often disappears gradually across many directories and files. Checking directory and file sizes makes it possible to spot unexpected growth, clean up large items, and prevent services from failing due to full partitions.
Commands such as ls and stat expose single-file information, but overall disk consumption across entire directory trees is best inspected with du. The du tool walks directories, sums the blocks actually used on disk, and prints per-path usage in formats that are easy to scan, such as human-readable units.
Because du reports allocated space rather than only apparent size, results can differ for sparse files, compressed or copy-on-write filesystems, and bind mounts. Running du on system paths may require elevated permissions and can be intensive on large trees, so limiting recursion depth and summarizing output reduces noise and speeds up inspection.
Related: How to check disk space and usage in Linux
Related: How to free disk space on Linux
Steps to check directory and file sizes in Linux:
- Check the size of a specific file in 1K blocks.
$ du /root/sg-work/size-demo/small.txt 4 /root/sg-work/size-demo/small.txt
Shows disk usage in 1K blocks by default, which may differ slightly from the size reported by ls.
- Display the size of a specific file in a human-readable format.
$ du -h /root/sg-work/size-demo/small.txt 4.0K /root/sg-work/size-demo/small.txt
The -h option prints sizes using units such as K, M, and G.
- Show the size of all files and subdirectories within a directory recursively.
$ du -h /root/sg-work/size-demo 8.0K /root/sg-work/size-demo/archive 2.1M /root/sg-work/size-demo
Each line shows the cumulative size of the path on that line, including everything beneath it.
- Limit the depth of the recursive listing to highlight only top-level directories.
$ du -h --max-depth=1 /root/sg-work/size-demo 8.0K /root/sg-work/size-demo/archive 2.1M /root/sg-work/size-demo
The –max-depth=1 option prints sizes for the target directory and its immediate children only.
- Show folder sizes and include a grand total at the end.
$ du -hc /root/sg-work/size-demo 8.0K /root/sg-work/size-demo/archive 2.1M /root/sg-work/size-demo 2.1M total
The -c option adds a final total line summarizing all reported entries.
- Display only the total size of a directory without listing its contents.
$ du -hs /root/sg-work/size-demo 2.1M /root/sg-work/size-demo
The combination of -h and -s produces a single summarized size in human-readable units.
- Inspect the size of a system directory that requires elevated permissions.
$ sudo du -hs /var/cache/ 152M /var/cache/
Using sudo against paths like /var/cache or /var/log ensures all entries can be read and counted.
- Show non-recursive sizes of all immediate subdirectories under a system path.
$ sudo du -hs /var/cache/* 4.0K /var/cache/adduser 3.1M /var/cache/apparmor 8.0K /var/cache/app-info 121M /var/cache/apt 4.1M /var/cache/debconf 14M /var/cache/fwupd 4.0K /var/cache/fwupdmgr 28K /var/cache/ldconfig 2.1M /var/cache/man 4.0K /var/cache/motd-news 8.0K /var/cache/PackageKit 4.0K /var/cache/pollinate 4.0K /var/cache/private 1.4M /var/cache/snapd 7.6M /var/cache/swcatalog
This pattern quickly reveals which subdirectories under /var/cache consume the most space without traversing deeper levels.
- Find the largest directories under a given path using sorted output.
$ sudo du -h --max-depth=1 /var | sort -h 4.0K /var/crash 4.0K /var/local 4.0K /var/mail 4.0K /var/opt 4.0K /var/snap 16K /var/spool 52K /var/tmp 2.1M /var/backups 152M /var/cache 278M /var/lib 402M /var/log 833M /var/
Sorting with sort -h orders paths by size so the largest directories appear at the bottom of the list.
- Review available du options and behavior for more advanced usage.
$ du --help Usage: du [OPTION]... [FILE]... or: du [OPTION]... --files0-from=F Summarize device usage of the set of FILEs, recursively for directories. Mandatory arguments to long options are mandatory for short options too. -0, --null end each output line with NUL, not newline -a, --all write counts for all files, not just directories --apparent-size print apparent sizes rather than device usage; although the apparent size is usually smaller, it may be larger due to holes in ('sparse') files, internal fragmentation, indirect blocks, and the like -B, --block-size=SIZE scale sizes by SIZE before printing them; e.g., '-BM' prints sizes in units of 1,048,576 bytes; see SIZE format below -b, --bytes equivalent to '--apparent-size --block-size=1' -c, --total produce a grand total -D, --dereference-args dereference only symlinks that are listed on the command line -d, --max-depth=N print the total for a directory (or file, with --all) only if it is N or fewer levels below the command line argument; --max-depth=0 is the same as --summarize --files0-from=F summarize device usage of the NUL-terminated file names specified in file F; if F is -, then read names from standard input -H equivalent to --dereference-args (-D) ##### snipped #####Related: du man page
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.
