Skip to content

Instantly share code, notes, and snippets.

@jeabraham
Created February 2, 2021 19:01
Show Gist options
  • Save jeabraham/cc2bc055713455f497e79b7defc3141b to your computer and use it in GitHub Desktop.
Save jeabraham/cc2bc055713455f497e79b7defc3141b to your computer and use it in GitHub Desktop.
Shell script to compress directories more than 10 weeks old, using AFSC tool
# MacOS has on-the-fly decompression, but not on-the-fly compression.
# AFSC tool https://github.com/RJVB/afsctool.git is handy
# for applying compression, but if you're actively working
# on something, you'll be writing new files, meaning stuff won't stay.
# compressed. Even saving a new
# version of a file from an application usually means a brand
# new (uncompressed) file.
# Also, directories containing compressed files like jpg images
# or movies won't benefit from file system level compression.
# What I do is run this script on occasion in a folder that
# I know contains a lot of subfolders of potentially compressible
# files. It compresses the entire subfolder if it's older than 6 million seconds (
# about 10 weeks)
# I use brew to install AFSCTool into /usr/local/bin, and schedule
# this shell script to run as a LaunchDaemon once a week in the higher
# level directory. For example, you could run it in your Documents folder
# once a week.
export PATH=/usr/local/bin:$PATH
now="$(date +%s)"
find . -maxdepth 1 -mindepth 1 -type d -print0 |
while IFS= read -r -d '' line; do
dirdate="$(stat -f %m $line)"
# echo $now
# echo $dirdate
let diff="${now} - ${dirdate}"
echo "Seconds since directory $line modified is $diff"
if [ "$diff" -gt 6000000 ]; then
echo "Compressing $line"
afsctool -c -j1 -6 "$line"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment