Checking the size of a folder in Linux is one of those practical skills that can save you from a surprising amount of trouble. Even so, linux provides several powerful tools for this task, and most of them are available directly from the terminal. Whether you are trying to free up disk space, troubleshoot a system that is running out of storage, prepare a backup, or simply understand how much data a project directory is consuming, knowing how to inspect folder size is essential. In this guide, you will learn how to check the size of a folder in Linux using simple commands, understand the difference between apparent size and actual disk usage, and discover practical techniques for finding large directories quickly That alone is useful..
Why Checking Folder Size Matters
Every Linux system relies on a file system to organize data into files and directories. Over time, folders can grow much larger than expected. So log files, cache directories, downloaded packages, media files, virtual machine images, and development project folders can all consume significant amounts of storage. If you ignore them, your system may eventually run low on free space, which can cause applications to fail, updates to stop working, or even prevent the operating system from booting properly in severe cases.
Knowing how to check the size of a folder in Linux helps you:
- Identify which directories are using the most disk space.
- Clean up unnecessary files safely.
- Monitor storage usage on servers and desktops.
- Prepare for backups or migrations.
- Diagnose performance issues related to disk usage.
- Avoid accidentally deleting important system files.
The good news is that Linux makes this process straightforward. With just a few commands, you can inspect a single folder, compare multiple directories, or scan an entire file system for space-consuming locations.
Understanding Folder Size in Linux
Before diving into commands, it helps to understand what “folder size” actually means in Linux. Now, a folder, also called a directory, does not usually have a meaningful size by itself in the way a regular file does. Instead, its size is generally understood as the total size of all files and subdirectories contained within it Turns out it matters..
On the flip side, there are two important concepts to keep in mind:
-
Apparent size
This is the logical size of the files inside the folder. It reflects how many bytes the files contain according to their metadata. -
Disk usage
This is the actual amount of space the files occupy on the disk. Because file systems allocate space in blocks, even a tiny file may consume a full block. This means disk usage can be larger than apparent size Small thing, real impact..
As an example, a file that contains only 1 byte of data may still occupy 4 KB on disk because the file system allocates space in blocks. When you check folder size, most Linux tools report disk usage by default, which is usually what you want when managing storage Practical, not theoretical..
You'll probably want to bookmark this section.
The Easiest Way: Using the du Command
The most common and reliable way to check the size of a folder in Linux is with the du command. The name stands for disk usage, and it is designed specifically for reporting how much space files and directories consume Not complicated — just consistent..
Basic du Usage
To check the size of a folder, open a terminal and run:
du /path/to/folder
This command will display the disk usage of the specified folder and its subdirectories. Even so, the output is usually shown in blocks, which can be difficult to read. To make it more practical, most users prefer human-readable output.
Human-Readable Output
To see the folder size in a format that is easy to understand, use the -h option:
du -h /path/to/folder
The -h flag stands for human-readable. It converts the output into units such as K for kilobytes, M for megabytes, and G for gigabytes Most people skip this — try not to. And it works..
For example:
du -h /home/user/Documents
This might produce output like:
120K /home/user/Documents/notes
4.0M /home/user/Documents/images
18M /home/user/Documents
The last line usually represents the total size of the folder you requested.
Getting Only the Total Size
If you only want the total size of a folder and do not need to see every subdirectory, use the -s option:
du -sh /path/to/folder
Here:
-smeans summarize, showing only the total for the specified directory.-hmakes the result human-readable.
For example:
du -sh /var/log
This might return:
245M /var/log
This is one of the most useful commands for quickly checking how much space a folder is using.
Checking the Size of All Folders in the Current Directory
A very common scenario is wanting to know which folders inside your current directory are taking up the most space. You can do this with:
du -sh *
This command checks each item in the current directory and displays a summarized, human-readable size for each one.
If you want to include hidden files and folders as well, use:
du -sh .[!.]* * 2>/dev/null
This pattern includes hidden entries while suppressing error messages for special cases.
Another useful approach is to sort the results from largest to smallest:
du -sh * | sort -rh
In this command:
du -sh *calculates the size of each item.sort -rhsorts the output in reverse human-readable order, placing the largest entries first.
This is extremely helpful when you are trying to find space hogs in a project directory, home folder, or web server directory
Exploring Additional du Options
While the basics already cover most everyday needs, du offers a handful of extra flags that become indispensable when you start digging deeper or writing automation scripts Most people skip this — try not to. Surprisingly effective..
Limiting Depth with --max-depth
When you only care about the top‑level directories, the default recursive walk can be wasteful. The --max-depth switch lets you cap the recursion level:
du -h --max-depth=1 /var/www
The command prints the size of each immediate subdirectory under /var/www without descending into their own children. Combine it with -s for an even tighter view:
du -sh --max-depth=0 /var/www/* | sort -rh
Excluding Specific Files or Filesystems with -x and --exclude
If you need to ignore certain paths—perhaps large cache directories or mounted backup drives—use -x to stay on the same filesystem, or --exclude=PATTERN to skip matching entries:
du -h -x --exclude='cache' --exclude='backup' /home
-x prevents du from crossing mount points, which is especially useful on servers where separate partitions hold massive data sets you don’t want to count Most people skip this — try not to..
Reporting Apparent vs. Real Disk Usage
By default, du reports the disk blocks actually used, which may differ from the file’s logical size due to sparse files, compression, or block allocation. The --apparent-size flag tells du to show the raw byte count instead:
du -h --apparent-size /var/lib/docker
This is handy when you suspect that a filesystem is reporting more space than the files truly occupy Took long enough..
Summarizing Multiple Directories with -c
When you want a grand total alongside individual listings, -c adds a cumulative line at the end:
du -sh /etc /var /usr | sort -rh
du -ch /etc /var /usr
The last line now shows the combined size of all specified arguments, giving you a quick sanity check And it works..
Human‑Readable vs. Precise Numeric Output
For human eyes, -h (or -H for “human‑readable” without the rounding) is perfect, but scripts often need exact byte values. The -b flag forces output in bytes, eliminating the suffixes:
du -b /opt/app | tail -n 1 # total size in bytes
You can then feed that number into other tools, perform arithmetic, or log it in a structured format.
Putting du to Work in Real‑World Scenarios
Finding the Largest Files in a Directory Tree
du excels at summarizing whole trees, but sometimes you need the top files themselves. Pair du with find and sort:
find /var/log -type f -exec du -b {} + | sort -nr | head -n 10
The pipeline lists each file’s size in bytes, orders them descending, and shows the ten biggest offenders.
Monitoring Directory Growth Over Time
A simple cron job can capture du snapshots and compare them later:
# /etc/cron.daily/du_snapshot
#!/bin/bash
DATE=$(date +%F)
du -sh /srv/data > /var/log/du_snapshot_${DATE}.txt
After a few days, you can diff the logs or plot the totals with a tool like gnuplot to visualize how quickly a directory is expanding.
Using du in Combination with ncdu
For an interactive experience, ncdu (NCurses Disk Usage) offers a curses‑based UI that builds on the same data du gathers. While du is great for quick, scripted checks, ncdu shines when you need to explore the contents of a directory on the fly, delete unwanted files, and see immediate feedback.
Best Practices and Common Pitfalls
-
Permissions –
duwill report “Permission denied” for directories you cannot read. Redirect errors to/dev/nullif you want a clean output, but be aware you might miss important information. -
Sparse Files – Because
ducounts allocated blocks, sparse files may appear larger than they actually are. Use `--apparent
-size` to see the logical file size instead That's the part that actually makes a difference..
-
Mount Points and Bind Mounts – By default,
dutraverses into mounted filesystems. If you want to restrict the scan to a single filesystem, add-x(or--one-file-system). This prevents accidental inclusion of a massive NFS share or a separate/homepartition when auditing/. -
Hard Links –
ducounts the disk space of a file only once per run, even if the file has multiple hard links within the scanned tree. If you runduon two directories separately that share hard-linked files, the space will be counted in both summaries. Use-l(--count-links) to force counting each link instance if you need that behavior. -
Performance on Metadata-Heavy Trees – Directories containing millions of tiny files (e.g., Maildir queues, cache directories) can make
ducrawl slowly due tostatsyscall overhead. In those cases, considerfind /path -printf '%s\n' | awk '{sum+=$1} END {print sum}'for a raw byte total, or rely on filesystem quotas (quota,xfs_quota) which maintain running totals in the kernel That's the part that actually makes a difference.. -
Output Parsing in Scripts – Never parse the human-readable output (
-h) in scripts; locale settings can change the decimal separator and suffixes. Always use-b(bytes) or-k(kilobytes) for machine consumption, and format the output for humans only at the very last step That's the part that actually makes a difference..
Advanced Flags Worth Knowing
| Flag | Purpose |
|---|---|
--exclude=PATTERN |
Skip files/directories matching a shell pattern (e.g.Because of that, , --exclude='*. That said, log'). |
--exclude-from=FILE |
Read exclude patterns from a file, one per line. On the flip side, |
--max-depth=N |
Limit recursion depth; -d 0 is an alias for -s. |
--time / --time=WORD |
Show last modification time (mtime), access time (atime), or change time (ctime) of the newest file in each directory. |
--inodes |
Report inode usage instead of block usage—critical for filesystems that can run out of inodes before space (e.Worth adding: g. , ext4 with many tiny files). |
Conclusion
du is deceptively simple: a single binary that, when combined with standard Unix plumbing—sort, find, awk, cron—becomes a full-fledged storage observability toolkit. Whether you are hunting a runaway log directory on a production server, scripting a nightly capacity report, or just curious why your home partition hit 90%, the flags and patterns covered here turn raw block counts into actionable insight.
Master the basics (-h, -s, -d, -x), respect the quirks (sparse files, hard links, mount traversal), and lean on ncdu when you need an interactive dive. With those habits in place, you’ll never again wonder where the disk space went—you’ll know exactly which directory to clean, which quota to raise, or which application team to ping.