Skip to content

Instantly share code, notes, and snippets.

@cander
Last active March 1, 2019 23:12
Show Gist options
  • Save cander/bb939506c83fd36a42529bad5e81c998 to your computer and use it in GitHub Desktop.
Save cander/bb939506c83fd36a42529bad5e81c998 to your computer and use it in GitHub Desktop.
Slowly remove file files in a directory
#!/bin/bash
#
# Slowly remove files from a directory
# Usage: slow-rm.sh [-dry-run] dir
#
# Removes files older than MIN_AGE_DAYS one at a time syncing and
# sleeping after every BATCH_SIZE files.
# A BATCH_SIZE of 300 will take at least 56 minutes to remove 1M files
BATCH_SIZE=300
MIN_AGE_DAYS=3
if [ $1 == '-dry-run' ] ; then
RM_CMD="echo rm"
shift
else
RM_CMD="rm"
fi
cd $1
echo "Removing files from $PWD with $RM_CMD"
count=0
find . -type f -mtime +$MIN_AGE_DAYS |
while read path
do
$RM_CMD $path
let "count += 1"
if [[ $(( count % $BATCH_SIZE )) -eq 0 ]] ; then
echo -n '.'
sync
sleep 1
fi
done
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment