A bash version of keep_releases known from Capistrano ruby scripts Mar 31, 2012
Today I needed a clean up feature in my bash script similar to what’s known from Capistrano when using the “keep_releases” argument.
I wasn’t able to find a simple version so I created it myself. Maybe others find it useful too so here goes
releases_path=/data/sites/yoursite.com/releases # change this
keep_releases=5
versions=`ls -xt $releases_path`
releases=(${versions// / })
# check available number of versions in releases directory
releases_count=${#releases[@]}
if [ $releases_count -le $keep_releases ]
then
echo 'no old releases to clean up'
else
echo keeping $keep_releases of $releases_count deployed releases
releases=(${releases[@]:0:0} ${releases[@]:($keep_releases)})
for release in "${releases[@]}"
do
path=$releases_path$release
`rm -rf $path`
done
fi
I’ve created a github gist too. Feel free to modify it if you have a better solution.