Skip to main content
Bash and Linux

Find Files by Size

mediumfind and xargs

Problem statement

Use find to list files bigger than 1 MiB, biggest first with readable sizes, and then list empty files and folders. When a disk fills up, the first question is "which files are huge?", and the cleanup question that follows is "what is left over and empty?".

The script builds this tree (truncate makes files of an exact size):

tree

TEXT
cache/blob.bin 5 MiB
logs/app.log 3 MiB
logs/old.log 1500 KiB
logs/exact.log exactly 1 MiB
docs/readme.txt 2 KiB
tmp/empty.txt 0 bytes
tmp/unused/ an empty folder
  1. List files larger than 1 MiB, biggest first, as size path with the size in MiB.
  2. List empty files and empty folders.

Expected output:

TEXT
== files over 1 MiB, biggest first ==
5.0M ./cache/blob.bin
3.0M ./logs/app.log
1.5M ./logs/old.log
== empty files and folders ==
./tmp/empty.txt
./tmp/unused

Hints

Hint 1: find . -type f -size +1M finds regular files over 1 MiB. -printf '%s %p\n' prints the size in bytes and the path, ready for sort -rn.

Approach

Optimal: find -size with -printf and sort

Covers: find start folder and tests, -type f / -type d, -name, -size +N with units, -empty, -maxdepth, -printf (GNU), sort -rn, why not to parse ls.

find walks a tree and tests every entry. You give it a starting folder and a list of tests. Each test must be true for an entry to be printed, as if they were joined with AND:

%%{init: {"flowchart": {"padding": 18, "nodeSpacing": 30, "rankSpacing": 40, "htmlLabels": true}, "themeVariables": {"fontSize": "18px"}}}%% flowchart TB S(["find . starts here"]):::purple --> T1{{"-type f?"}}:::blue T1 --> T2{{"-size +1M?"}}:::blue T2 --> P["-printf size and path"]:::green T1 --> X["no: skip"]:::gray classDef blue fill:#dbeafe,stroke:#2563eb,color:#1e3a8a,stroke-width:2px classDef yellow fill:#fef3c7,stroke:#d97706,color:#78350f,stroke-width:2px classDef green fill:#d1fae5,stroke:#059669,color:#064e3b,stroke-width:2px classDef red fill:#fee2e2,stroke:#dc2626,color:#7f1d1d,stroke-width:2px classDef purple fill:#ede9fe,stroke:#7c3aed,color:#4c1d95,stroke-width:2px classDef gray fill:#f3f4f6,stroke:#6b7280,color:#111827,stroke-width:2px linkStyle default stroke:#94a3b8,stroke-width:2px
Test Means
-type f a regular file (d folder, l link)
-name '*.log' the name matches (quote it, so the shell does not expand *)
-size +1M larger than 1 MiB (-1M smaller, 1M about 1)
-empty an empty file or an empty folder
-mtime +7 changed more than 7 days ago (next page)
-maxdepth 1 do not go deeper than this many levels

Size units: c bytes, k KiB, M MiB, G GiB.

How -size rounds. find rounds each file's size up to whole units before comparing. So with M:

%%{init: {"flowchart": {"padding": 18, "nodeSpacing": 30, "rankSpacing": 40, "htmlLabels": true}, "themeVariables": {"fontSize": "18px"}}}%% flowchart LR subgraph IN["counts as more than 1M"] direction TB A1["old.log 1500K
rounds up to 2"]:::green end subgraph OUT["does not count"] direction TB B1["exact.log 1M
stays 1"]:::red end IN ~~~ OUT classDef blue fill:#dbeafe,stroke:#2563eb,color:#1e3a8a,stroke-width:2px classDef yellow fill:#fef3c7,stroke:#d97706,color:#78350f,stroke-width:2px classDef green fill:#d1fae5,stroke:#059669,color:#064e3b,stroke-width:2px classDef red fill:#fee2e2,stroke:#dc2626,color:#7f1d1d,stroke-width:2px classDef purple fill:#ede9fe,stroke:#7c3aed,color:#4c1d95,stroke-width:2px classDef gray fill:#f3f4f6,stroke:#6b7280,color:#111827,stroke-width:2px linkStyle default stroke:#94a3b8,stroke-width:2px style IN fill:transparent,stroke:#059669,stroke-width:2px style OUT fill:transparent,stroke:#dc2626,stroke-width:2px

old.log is 1500 KiB, about 1.46 MiB, which rounds up to 2, and 2 is more than 1, so it counts. exact.log is exactly 1 MiB, which stays 1, and 1 is not more than 1, so it is left out. For exact limits, use bytes: -size +1048576c.

Printing sizes with -printf. GNU find can print details itself: %s is the size in bytes, %p the path, \n a new line. Bytes sort perfectly with sort -rn, and a small awk turns them into MiB for reading. This avoids ls -l, whose columns change with dates and locales and break on odd file names.

Walking through the code. The # Setup: lines only build the sample tree, so skip past them.

  1. find . -type f -size +1M -printf '%s %p\n' | sort -rn lists the three big files, biggest first. The awk divides by 1048576 and prints one decimal.
  2. find . -empty | sort lists the empty file and the empty folder.

Edge cases. On a real server, start from / with -xdev to stay on one disk and 2>/dev/null to hide "Permission denied". -printf is GNU only; on macOS, use find . -type f -size +1M -exec ls -l {} + or stat -f '%z %N'.

# Setup: build the sample tree with exact sizes in a fresh temporary folder
cd "$(mktemp -d)"
mkdir -p cache logs docs tmp/unused
truncate -s 5M    cache/blob.bin
truncate -s 3M    logs/app.log
truncate -s 1500K logs/old.log
truncate -s 1M    logs/exact.log
truncate -s 2K    docs/readme.txt
: > tmp/empty.txt

echo "== files over 1 MiB, biggest first =="
find . -type f -size +1M -printf '%s %p\n' | sort -rn |
  awk '{ printf "%.1fM %s\n", $1 / 1048576, $2 }'

echo "== empty files and folders =="
find . -empty | sort

Interview follow-ups

  • Only look in /var/log, and only at files not changed in 30 days.

    Add the tests you need: find /var/log -type f -size +100M -mtime +30 -printf '%s %p\n' | sort -rn. Every test is joined with AND, so a file must be big and old to appear. Those are usually safe candidates for compressing or deleting, after you check that no program still writes to them. The next page shows how to delete old files safely.

Frequently asked questions

ls -l is made for people to read, not for scripts. Its columns move when dates are older than six months, when owner names are long, or when the locale changes, and file names with spaces or newlines break any awk '{print $9}'. find with -printf, or stat, gives you exactly the fields you ask for in a fixed format. For a quick look at the terminal, ls -lhS (sorted by size) is fine; for scripts, use find.

The size (%s, ls -l) is how many bytes the file holds. Disk usage (du) is how much space it takes on the disk, which can be larger (space is used in blocks) or much smaller (sparse files, like the ones truncate makes, have holes that take no space). When a disk is full, du is the number that matters. That is why the Biggest Directories page uses du, while this page uses size to find big files by content.

sudo find / -xdev -type f -size +100M -printf '%s %p\n' 2>/dev/null | sort -rn | head -n 10. The -size +100M filter makes find skip printing most files, so the sort stays small. -xdev keeps it on the root disk; repeat for other disks like /var or /data if they are separate mounts. Start with a big threshold and lower it if nothing shows up.