What Does the Find Large Files Script Look Like?
Your disk hit 100% and now nothing works — nginx can't write access logs, your database refuses new transactions, and deploys fail silently. Before you can fix anything, you need to know what consumed the space. This script answers that question in under 10 seconds.
How it works, line by line
TARGET_DIR="${1:-/}"— Defaults to root filesystem. Pass a specific path to narrow the scan.MIN_SIZE="${2:-500M}"— Minimum file size for thefindpass. Use100M,1G, etc.du -ah "$TARGET_DIR"—-aincludes files (not just directories),-hgives human-readable sizes.--exclude=/proc --exclude=/sys --exclude=/dev— Virtual filesystems report phantom sizes that waste your time.sort -rh— Reverse human-numeric sort: 10G before 500M before 2K.head -n "$TOP_COUNT"— Only the top 20 — enough to identify the problem without flooding your terminal.find ... -type f -size +"$MIN_SIZE"— Locates individual files (not directories) exceeding the threshold.-exec ls -lh {} \;— Prints full details (permissions, owner, size, date) for each match.
How Do I Set Up and Run the Find Large Files Script?
Step 1: Create the script file
Paste the script above. Save with Ctrl+X, Y, Enter.
Step 2: Set your target and threshold
| Target | When to use |
|---|---|
/ | Full system scan — find the biggest offenders anywhere |
/var | Logs, caches, mail spools — the most common growth directory on servers |
/home | User data, downloads, old project builds |
/tmp | Orphaned temp files from crashed processes |
Step 3: Make it executable and run
Root access is required to read all directories. Without sudo, you'll miss files owned by other users and system directories.
Step 4: Scan a specific directory with a custom threshold
What Are Common Variations of This Script?
Variation 1: Top directories only (faster scan)
When you need speed over granularity — useful on large filesystems where a full du -a takes minutes:
Variation 2: Find files modified in the last 24 hours over 100 MB
Narrows the search to recent growth — answers "what changed since yesterday that filled the disk?"
Variation 3: Output to a report file with timestamp
For audit trails or comparing disk consumption over time:
How Do I Automate This with Cron?
Run a weekly disk consumption report so you catch growth before it becomes an emergency:
Pair this with the disk space warning script for threshold-based alerts between reports.
When the fix is more disk, not less data — spin up a DigitalOcean droplet in 60 seconds.
Get $200 free credit — DigitalOcean
Get $200 Free →Affiliate link · we earn a commission
Related Scripts
- Disk Space Warning — Get alerted before the disk fills completely
- Delete Old Log Files — Automated cleanup of aged logs consuming space
- Find Duplicate Files — Locate byte-for-byte duplicates wasting disk
FAQ
How do I find what is using the most disk space in Linux?
Run du -ah /target | sort -rh | head -n 20 to see the 20 largest files and directories under /target, ranked by size. Replace /target with / to scan the entire filesystem — exclude /proc and /sys to avoid false positives from virtual filesystems.
What is the difference between du and df for checking disk space?
df reports total, used, and available space per mounted filesystem — it answers "how full is this drive?" du measures actual consumption per file and directory — it answers "what is taking the space?" Use df first to confirm which partition is full, then du to find what filled it.
How do I find files larger than 1 GB in Linux?
Run find / -type f -size +1G -exec ls -lh {} \; to locate every regular file over 1 GB with human-readable sizes. Add --exclude paths for /proc and /sys to avoid permission errors and false results from virtual filesystems.
Is it safe to delete large files I find with du?
Never delete a file without confirming no running process holds it open. Use lsof +D /path to check. Log files held open by a service will not free space until that service restarts or the file descriptor closes. Compress with gzip or truncate with > filename instead of rm for active log files.
Why does du show different sizes than ls?
ls -l shows the apparent file size (bytes written). du shows actual disk blocks allocated, which accounts for filesystem overhead, sparse files, and block alignment. A 1-byte file uses at least one 4 KB block on ext4, so du reports 4K while ls reports 1 byte.