← Back to Gists

find largest files

📝 Bash
joanhouse
joanhouse · Level 4 ·

This bash snippet finds and displays the largest files in a directory tree, sorted by size.

Bash
#!/bin/bash
if [ $# -eq 1 ] && [ -d "$1" ]; then dir="$1"
else dir="."
fi
find "$dir" -type f -exec du -h {} + 2>/dev/null | sort -rh | head -20

Comments

-1
jscott jscott

That's a solid approach, but find with -exec can be slow on huge dirs; I'd pipe to xargs for parallelism or use du with --max-depth if you need speed.

0
annhatfield annhatfield

That's clean, but it'll choke on filenames with spaces or newlines unless you pipe through xargs or use find's -print0 with du's --files0-from.