Linux Folder Size: How to Check File and Directory Sizes

When a server is running low on disk space, one of the first checks is which folder is actually using the storage. The usual command for that job is du -sh /path, which prints the total disk usage for the path in a human-readable format. On Linux, “folder” and “directory” mean the same thing, so the commands below apply either way.
If you run ls -l
, directories often appear as 4096 bytes, but that value only represents directory metadata, not the size of the files below it. The du
(disk usage) command shows the real space used by files and directories. This guide explains how to use du and related commands to check sizes, find large directories, and manage disk space.
Checking Directory Size with du
To display the total size of a directory in a human-readable format, use du with the -s (summarize) and -h (human-readable) options:
sudo du -sh /var85G /var-s- Display only the total size, not each subdirectory-h- Print sizes in human-readable format (K, M, G)
The command uses sudo
because many files inside /var are owned by root. Without sudo, du prints “cannot read directory” warnings for paths it cannot access.
Getting the Size of a File
To check the size of a single file, pass the file path to du:
du -sh /var/log/syslog12M /var/log/syslogYou can also use ls -lh to see the file size alongside other metadata:
ls -lh /var/log/syslog-rw-r----- 1 syslog adm 12M Feb 13 10:00 /var/log/syslogFor detailed file information including exact byte count, block size, and inode number, use stat:
stat /var/log/syslogWhen you want the byte count on its own, pass a format string to stat with the -c option:
stat -c %s /var/log/syslog12582912Listing Subdirectory Sizes
To see how much space each first-level subdirectory uses, use the --max-depth option:
sudo du -h --max-depth=1 /var5.0G /var/cache
4.0K /var/empty
4.0K /var/games
77G /var/lib
4.0K /var/local
3.3G /var/log
4.0K /var/opt
196K /var/spool
28K /var/tmp
24K /var/db
85G /varEntries appear in the order du walks the directory, not by size, and the total for /var itself comes last. To rank the subdirectories instead, pipe the output through sort:
sudo du -h --max-depth=1 /var | sort -rhThe --max-depth option also has a short form, so du -h -d 1 /var does the same thing and is what you need on macOS and BSD, where the long form is not available.
You can also use the asterisk wildcard with the -c option to include a grand total:
sudo du -shc /var/*The -c flag adds a total line at the end of the output. Note that the wildcard does not match hidden files or directories (those starting with .).
To increase the depth and see two levels of subdirectories:
sudo du -h --max-depth=2 /varFinding the Largest Directories
To find the directories consuming the most space, pipe the output of du to sort and head:
sudo du -h /var/ | sort -rh | head -1085G /var/
77G /var/lib
75G /var/lib/libvirt/images
75G /var/lib/libvirt
5.0G /var/cache
3.3G /var/log
2.1G /var/cache/apt
1.5G /var/log/journal
196K /var/spool
28K /var/tmpThe sort -rh command sorts in reverse order by human-readable size, placing the largest entries first. The head
command limits the output to the top 10 results.
To include individual files in the output, add the -a flag:
sudo du -ah /var/ | sort -rh | head -10Repeating du and sort gets tedious once you start drilling down folder by folder. The ncdu (NCurses Disk Usage) tool scans a path and lets you browse the results interactively, sorted by disk usage by default. Install it with sudo apt install ncdu on Ubuntu and Debian, or sudo dnf install ncdu on Fedora. On RHEL, enable the EPEL repository before installing the package. Run ncdu /var and walk into the largest folders with the arrow keys.
Excluding Directories
To skip specific directories during the scan, use the --exclude option:
sudo du -sh --exclude='*.log' /varYou can exclude multiple patterns by repeating the option:
sudo du -sh --exclude='cache' --exclude='tmp' /varThis is useful when you want to measure disk usage without counting log files, caches, or temporary directories.
Apparent Size vs Disk Usage
By default, du reports the disk space allocated to a file, which depends on the filesystem block size. A 1-byte file on a 4K-block filesystem still occupies 4 KB of disk space.
The --apparent-size option shows the actual amount of data in the file instead:
sudo du -sh --apparent-size /varThe apparent size is the number of bytes the file contains. The disk usage is the space the filesystem allocates. The two can differ significantly for directories with many small files.
When planning a transfer, apparent size is closer to the amount of file data than allocated disk usage, but it is not an exact measure of network traffic. rsync
can send only changed data and compress it, while scp
and sftp
add protocol overhead. Filesystem differences can also make du report different allocated sizes on the source and destination.
Checking Free Disk Space with df
The du command measures how much space files and directories use. The df
command shows how much free space remains on each mounted filesystem:
df -hFilesystem Size Used Avail Use% Mounted on
/dev/sda1 50G 35G 13G 73% /
tmpfs 2.0G 0 2.0G 0% /dev/shm
/dev/sdb1 200G 120G 70G 63% /dataUse du when you need to know which directories are consuming space. Use df when you need to know how much space is left on a partition.
Troubleshooting
du: cannot read directory ‘/path’: Permission denied
The current user does not have read permission on the directory. Run du with sudo to access all paths.
du and ls show different sizes
The ls -l command shows a regular file’s apparent size in bytes. The du command shows allocated disk space in blocks by default. Use du -b file or stat -c %s file when you need the apparent size as an exact byte count.
du output uses blocks instead of human-readable units
Add the -h flag to display sizes in K, M, and G. Without -h, du defaults to 1 KB blocks on most systems.
du is slow on large directory trees
The du command reads every file and subdirectory. Use --max-depth=1 to limit depth, or --exclude to skip large directories you do not need to measure.
Quick Reference
For a printable quick reference, see the du cheatsheet .
| Command | Description |
|---|---|
du -sh /path | Total size of a directory |
du -sh file.txt | Size of a single file |
du -h --max-depth=1 /path | Size of each first-level subdirectory |
du -h --max-depth=1 /path | sort -rh | Subdirectories ranked by size |
du -shc /path/* | Subdirectory sizes with grand total |
du -ah /path | sort -rh | head -10 | Top 10 largest entries |
du -sh --exclude='*.log' /path | Exclude files matching a pattern |
du -sh --apparent-size /path | Apparent size instead of disk usage |
ls -lh file.txt | File size with metadata |
stat file.txt | Detailed file information |
stat -c %s file.txt | Exact size in bytes |
df -h | Free disk space per filesystem |
FAQ
What is the difference between du and df?
The du command measures the space used by specific files and directories. The df command shows the total, used, and available space on each mounted filesystem. Use du to find what is consuming space and df to check how much space is left.
Why does du show a larger size than the sum of the files?
Filesystems allocate space in fixed-size blocks (typically 4 KB). A 1-byte file still uses one full block. Directories with many small files show a larger du total than the sum of their apparent sizes.
How do I find the largest files and directories on the system?
Use sudo du -ahx / | sort -rh | head -20 to list the 20 largest entries on the root filesystem. The output includes both files and directories. To search only for regular files, see Find Large Files in Linux
.
What does the -s flag do in du?
The -s (summarize) flag tells du to display only the total size of the specified directory, instead of listing every subdirectory individually.
Can I check directory sizes without sudo?
Yes, for directories you own. You only need sudo when the directory contains files owned by other users or root. Without sudo, du skips unreadable paths and prints permission warnings.
Conclusion
Use du -sh /path when you need a quick total, then move to --max-depth or sort -rh when you need to find what is taking space. For filesystem-level free space, use df -h instead.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page