Understanding how to check the size of a folder and its subfolders is a fundamental skill for anyone managing a Linux system, whether you are a seasoned system administrator, a developer cleaning up a build directory, or a casual user trying to free up disk space. And unlike graphical file managers that often calculate sizes recursively with a simple right-click, the command line offers powerful, flexible, and scriptable tools to get exactly the data you need. Mastering these commands allows you to identify storage hogs, audit directory structures, and automate cleanup tasks efficiently The details matter here..
The Primary Tool: The du Command
The standard utility for estimating file space usage is du (disk usage). Worth adding: it is pre-installed on virtually every Unix-like operating system. By default, du summarizes the disk usage of each file and recursively traverses directories, reporting the size in blocks (usually 1KB blocks).
To see the total size of the current directory and all its subdirectories in a human-readable format, the most common invocation is:
du -sh
Here is what those flags do:
-s(--summarize): Displays only a total for each argument. Without this,duprints the size of every single subdirectory, which can flood your terminal.-h(--human-readable): Appends size letters like K (Kilobytes), M (Megabytes), G (Gigabytes), making the output instantly understandable.
If you want to check a specific folder rather than the current working directory, simply append the path:
du -sh /var/log
Controlling Recursion Depth
Often, you don't need the size of every nested subfolder five levels deep; you just want a high-level overview of the immediate children. The --max-depth (or -d) flag controls this behavior.
To see the size of the current folder and its immediate subfolders only (depth 1):
du -h --max-depth=1
Or the shorthand:
du -h -d 1
This is incredibly useful for answering the question: "Which top-level directory inside /home is consuming the most space?" without waiting for the command to crawl the entire filesystem tree Turns out it matters..
Sorting Output for Analysis
Raw du output lists directories in filesystem order (usually alphabetical). To find the largest directories quickly, pipe the output to the sort command.
du -h --max-depth=1 | sort -hr
-h(sort): Tells sort to understand human-readable numbers (e.g., 2G > 100M).-r(reverse): Puts the largest entries at the top.
This combination is arguably the single most effective "find the space hog" one-liner in Linux administration Took long enough..
Understanding Apparent Size vs. Disk Usage
A common point of confusion arises when the size reported by du differs from the size reported by ls -lh or a file manager. This discrepancy stems from the difference between Apparent Size and Actual Disk Usage Simple, but easy to overlook..
- Disk Usage (
dudefault): Reports the physical space allocated on the block device. Filesystems allocate space in blocks (typically 4KB). A 1-byte file still consumes 4KB on disk. Sparse files (files with "holes" of zero bytes) consume less disk space than their apparent size. - Apparent Size (
du --apparent-size): Reports the actual byte count of the file contents, ignoring filesystem block allocation and sparse file optimization.
To see the apparent size (what ls shows), use:
du -sh --apparent-size /path/to/folder
When to use which?
Use standard du (disk usage) for capacity planning: "Will this fit on my 10GB partition?" Use --apparent-size for data transfer estimation: "How long will rsync or scp take to transfer this folder over the network?"
Excluding Files and Directories
Real-world directories are often cluttered with files you want to ignore during an audit—cache directories, .git folders, node_modules, or specific log formats. The --exclude flag (or -X for a pattern file) handles this elegantly.
To calculate the size of a project folder while ignoring the node_modules and .git directories:
du -sh --exclude='node_modules' --exclude='.git' /path/to/project
Patterns match against the full path relative to the starting directory. You can use wildcards:
du -sh --exclude='*.cache' --exclude='*.log' /var/www
For complex exclusion lists, create a text file (e.g., `exclude_patterns.
du -sh -X exclude_patterns.txt /path/to/folder
Handling Permissions and Errors
Running du as a standard user on system directories (like /root, /var/lib/docker, or other users' home folders) will result in "Permission denied" errors cluttering your output. While you can prefix the command with sudo, sometimes you want to run a scan as a non-root user and simply ignore unreadable directories.
Redirect standard error to /dev/null to suppress errors:
du -sh /home 2>/dev/null
Alternatively, if you are scripting and need to know which directories failed, let the errors print to the terminal but capture the standard output separately.
The Modern Alternative: ncdu (NCurses Disk Usage)
While du is perfect for scripts and quick checks, it lacks interactivity. Here's the thing — enter ncdu (NCurses Disk Usage). It is a curses-based, interactive frontend for du that allows you to work through the directory tree using arrow keys, drill down into subfolders, delete files, and see visual percentage bars representing space consumption.
Installation is straightforward on most distributions:
- Debian/Ubuntu:
sudo apt install ncdu - RHEL/CentOS/Fedora:
sudo dnf install ncdu(oryum) - Arch:
sudo pacman -S ncdu
Run it simply by typing ncdu or targeting a path: ncdu /var.
Key ncdu shortcuts:
- Up/Down Arrows: work through list.
- Right Arrow / Enter: Drill into selected directory.
- Left Arrow: Go back to parent directory.
d: Delete selected file/folder (asks for confirmation).g: Toggle display mode (size / graph / percentage / size+graph).q: Quit.
ncdu scans the directory once on startup (showing a progress bar) and then allows instant navigation. It is vastly superior for exploratory analysis compared to running du | sort repeatedly The details matter here. No workaround needed..
Finding Large Files Specifically
Sometimes the folder size is large because of a single massive file buried deep in a subfolder, not because of many small files. du aggregates directory totals, making it hard to pinpoint the specific large file. The find command excels here Worth keeping that in mind. Nothing fancy..
To find all files larger than 100MB within the current directory and subdirectories:
find . -type f -size +100M -exec ls -lh {} \;
-type f: Look only for files (ignore directories).-size +100M: Size greater than 100 Megabytes. Usek,M,Gsuffixes.-exec ls -lh {} \;: