Write a one-liner that finds the 10 largest files under a directory tree.
A partition is filling up and you need to find the biggest offenders — actual files, not directories — anywhere under /var.
Constraints:
- rank the 10 largest regular files in the whole subtree (recurse)
- print each file's size next to its path
- do not be fooled by directory sizes — you want individual files
# complete a one-liner that prints the 10 largest files under /var
find /var -type f ...
Write the one-liner.
Recurse for regular files and rank by size. Use find /var -type f printing each file's byte size and path, piped to sort -rn | head -10 — files only, largest first. A quick alternative is du -ah /var | sort -rh | head, though du also lists directories. ls cannot recurse a subtree.
- ✗Expecting
ls -Sto recurse and rank a whole subtree's files - ✗Using
df(free space on a mount) to rank individual files by size - ✗Sorting human-readable sizes with
sort -ninstead ofsort -rh
- →If
duanddfdisagree on used space, what should you suspect? - →How would you restrict the search to only files larger than 100 MB?
Solution
Files only, ranked by size
find /var -type f -printf '%s\t%p\n' | sort -rn | head -10
-printf '%s\t%p\n' prints the byte size and the path; sort -rn orders descending and head -10 keeps the top 10. For human-readable units:
du -ah /var | sort -rh | head -10
du -ah lists both files and directories, so a big directory can crowd files out of the top — use the find form when you want files only.
⚠️ If the du total is far below what df reports used, look for a deleted-but-open file still held by a process: lsof +L1 (space frees when the descriptor closes).