Skip to content

Instantly share code, notes, and snippets.

@DavidRogersDev
Created January 18, 2025 04:40
Show Gist options
  • Save DavidRogersDev/ee3cd6026660a0e53f27dfe5701c821b to your computer and use it in GitHub Desktop.
Save DavidRogersDev/ee3cd6026660a0e53f27dfe5701c821b to your computer and use it in GitHub Desktop.
Recursively Visit Directories from a Root Directory and Compress All Git Repos
#!/bin/bash
compressAll() {
declare -a repos
repos=()
for i in "$1"/*;do # for all in the root
if [ -f "$i" ]; then # if a file exists
echo "$i" > /dev/null # send it to null
elif [ -d "$i" ];then # if a directroy exists
isGit="$(ls -la "$i" | awk '{print $9}' | grep -E "[\.]{1}git[\/]?$" | wc -l)" # is it a repo? Looks for .git directory
if [ $isGit -gt 0 ]; then
echo "$(basename -- "$i")" $isGit
repos+=($i)
else
echo "Not a repo: $(basename -- "$i")" $isGit
compressAll "$i" # recurse
fi
fi
done
for rep in ${repos[@]};do
cd $rep
echo "Compressing" $rep
git gc --aggressive
done
}
compressAll $1 # e.g.: ./compressAll.sh .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment