How to Check File and Directory Size in Linux

By 

Updated on

6 min read

Linux Directory Size

When a server is running low on disk space, one of the first checks is which directory 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.

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:

Terminal
sudo du -sh /var
output
85G	/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:

Terminal
du -sh /var/log/syslog
output
12M	/var/log/syslog

You can also use ls -lh to see the file size alongside other metadata:

Terminal
ls -lh /var/log/syslog
output
-rw-r----- 1 syslog adm 12M Feb 13 10:00 /var/log/syslog

For detailed file information including exact byte count, block size, and inode number, use stat:

Terminal
stat /var/log/syslog

Listing Subdirectory Sizes

To see how much space each first-level subdirectory uses, use the --max-depth option:

Terminal
sudo du -h --max-depth=1 /var
output
77G	/var/lib
5.0G	/var/cache
3.3G	/var/log
196K	/var/spool
28K	/var/tmp
24K	/var/db
4.0K	/var/games
4.0K	/var/local
4.0K	/var/opt
4.0K	/var/empty
85G	/var

You can also use the asterisk wildcard with the -c option to include a grand total:

Terminal
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:

Terminal
sudo du -h --max-depth=2 /var

Finding the Largest Directories

To find the directories consuming the most space, pipe the output of du to sort and head:

Terminal
sudo du -h /var/ | sort -rh | head -10
output
85G	/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/tmp

The 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:

Terminal
sudo du -ah /var/ | sort -rh | head -10

Excluding Directories

To skip specific directories during the scan, use the --exclude option:

Terminal
sudo du -sh --exclude='*.log' /var

You can exclude multiple patterns by repeating the option:

Terminal
sudo du -sh --exclude='cache' --exclude='tmp' /var

This 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:

Terminal
sudo du -sh --apparent-size /var

The 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 transferring files with scp , rsync , or sftp , the amount of data sent over the network is the apparent size. This is why the size reported by du on the source may not match the size on the 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:

Terminal
df -h
output
Filesystem      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% /data

Use 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 shows a different size than ls
The ls -l command shows the apparent file size (bytes of data). The du command shows the disk space allocated, which is rounded up to the filesystem block size. Use du --apparent-size to see the same value as ls.

du output is in bytes instead of human-readable format
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 .

CommandDescription
du -sh /pathTotal size of a directory
du -sh file.txtSize of a single file
du -h --max-depth=1 /pathSize of each first-level subdirectory
du -shc /path/*Subdirectory sizes with grand total
du -ah /path | sort -rh | head -10Top 10 largest entries
du -sh --exclude='*.log' /pathExclude files matching a pattern
du -sh --apparent-size /pathApparent size instead of disk usage
ls -lh file.txtFile size with metadata
stat file.txtDetailed file information
df -hFree 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 on the system?
Use du -ah / | sort -rh | head -20 with sudo to scan the entire filesystem and list the 20 largest entries. For a more targeted search, 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.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

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