Skip to content

Instantly share code, notes, and snippets.

@nicman23
Created January 5, 2021 15:30
Show Gist options
  • Save nicman23/5fabd882c4c60e5659156955bdad7b14 to your computer and use it in GitHub Desktop.
Save nicman23/5fabd882c4c60e5659156955bdad7b14 to your computer and use it in GitHub Desktop.
defrag.sh
#! /bin/bash -e
if [ -z "$1" ]; then
echo 'usage: [OPTIONS] <file>|<dir>'
echo 'same as btrfs filesystem defrag (look at its --help)'
exit
fi
if [ ! "$(id -u)" -eq 0 ]; then
echo 'This probably needs root, if not edit' "$0"
echo 'Remove line 10: sed -e '\''10d'\'' -i' "$0"
exit 2
fi
path=${*:${#*}}
opts=${*:1:$((${#*} -1))}
t=$(find "${path}" -xdev -type f | wc -l)
[ $t -eq 0 ] && exit
echo Found $t files;
btrfs -v filesystem defrag $opts "${path}" 2>&1 | pv -i2 -betlap -s $t > /dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment