#!/bin/bash set -e days=3 dry_run=false workspace_top=$HOME/workspace maxdepth=1 while [ $# -gt 0 ]; do case $1 in --days) days="$2"; shift ;; --dry_run) dry_run="$2"; shift ;; --workspace_top) workspace_top="$2"; shift ;; --maxdepth) maxdepth="$2"; shift ;; *) echo "ERROR: Wrong option: $1"; exit 1 ;; esac shift done set -x if [ "$days" -lt "0" ]; then echo "ERROR: Refusing to delete workspaces that are $days days old" exit 1 fi echo "Date / size report before (`date`):" ls -ld $workspace_top/* || true df -h $workspace_top || true du -hsc $workspace_top/* || true # Semantics of find's mtime "+N" stands for N+1 days old or older. days_1=$(($days-1)) # Handle dirs with whitespaces by setting $IFS to newline. SAVEIFS=$IFS IFS=$'\n' dirs=($(find $workspace_top/ -maxdepth $maxdepth -type d -mtime +$days_1)) IFS=$SAVEIFS rm_dirs=() for dir in "${dirs[@]}"; do if [ x"$(find "$dir" -mtime -$days | wc -l)" = x"0" ]; then rm_dirs=("${rm_dirs[@]}" "$dir") fi done if [ ${#rm_dirs[@]} != 0 ]; then echo "Removing directories:" ls -ld "${rm_dirs[@]}" du -hs "${rm_dirs[@]}" if ! $dry_run; then # Make sure we can get into all subdirectories. # Glibc tests can leave behind "rw-" directories, the contents of which # can't be cleaned up without "x" permission bit. chmod -R +rwx "${rm_dirs[@]}" rm -rf "${rm_dirs[@]}" else echo "DRY_RUN: NOT REMOVING DIRECTORIES" fi echo "Date / size report after (`date`):" ls -ld $workspace_top/* || true df -h $workspace_top || true du -hsc $workspace_top/* || true fi