Skip to content

Instantly share code, notes, and snippets.

@cander

cander/slow-rm.sh

Last active Mar 1, 2019
Embed
What would you like to do?
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