Skip to content

Instantly share code, notes, and snippets.

@kylemanna
Created January 8, 2015 20:43
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kylemanna/b5cac22b6164b14aa967 to your computer and use it in GitHub Desktop.
Save kylemanna/b5cac22b6164b14aa967 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Search $1 path (default to /) for files with over $2 (defaults to 100) fragments and attempts to defragment them.
#
# Author: Kyle Manna
#
root=${1:-/}
min=${2:-100}
find $root -xdev -type f -print0 | xargs -0 filefrag | sed -e "s/^\(.*\): \(.*\) extents found/\2 \1/" | awk "{ if (\$1 > $min) print \$0; }" | while read line; do
file=${line#* }
before=${line%% *}
btrfs filesystem defrag "$file"
after=$(filefrag "$file" | sed -e "s/^\(.*\): \([0-9]\+\) extents\? found/\2/")
echo "$file: $before -> $after"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment