Find files larger than 1 MiB, biggest first
Problem statement
A volume is at 95%. List every regular file under the current directory that is larger than 1 MiB, printing its size in bytes and its path, biggest first. A file of exactly 1 MiB is not 'larger than' and must not be listed. The tree can be recreated with (sparse files, so no real disk is used):
mkdir -p logs datatruncate -s 5M logs/app.logtruncate -s 200K logs/old.log.gztruncate -s 12M data/dump.sqltruncate -s 1M data/seed.sqlExamples
Example 1
Input: Run the command in the tree root.
Output: 12582912 ./data/dump.sql
5242880 ./logs/app.log
Explanation: seed.sql is exactly 1 MiB and old.log.gz is 200 KiB.
Hints
Approach
Optimal
-type f restricts the search to regular files. -size +1M keeps files larger than 1 MiB. -printf '%s %p\n' is a GNU find action that prints the size in bytes (%s) and the path (%p) without starting any extra process per file. sort -rn sorts those lines numerically on the leading size, largest first. On a real server you would usually add -xdev to stay on one filesystem and 2>/dev/null to hide permission errors.
find . -type f -size +1M -printf '%s %p\n' | sort -rnFollow-up questions
- Print sizes in human-readable units. (
numfmt --to=iecordu -h) - A file was deleted but
dfstill shows the space used. Why, and how do you find it? (lsof +L1)
Frequently asked questions
find rounds the file size up to whole units before comparing. With M, a 1.5 MiB file rounds up to 2 and matches, but a 1.0 MiB file is 1 and does not. The rounding surprises people most with -size -1M, which only matches empty files. For exact thresholds, use bytes: -size +1048576c.
No, BSD find has no -printf. Use find . -type f -size +1M -exec stat -f '%z %N' {} + | sort -rn, or install GNU findutils (gfind).