All pastes #341633 Raw Edit

Something

public text v1 · immutable
·published 2007-02-05 19:35 UTC
rendered paste body
#!/bin/sh

TMP="/tmp"

MAXAGE=$(( 31 + ( `sed 's/[^0-9].*$//g' /proc/uptime` / 90000 ) ))

TIME=`date +"%Y/%m/%d at %T -->"`

echo "Cleaning /tmp ..."

echo "Remove files from $TMP older then $MAXAGE days"
find "$TMP/" -maxdepth 1 -mindepth 1 -not -type d -mtime +$MAXAGE | while read FILE
do
  ( rm -f "$FILE" &> /dev/null ) && \
  ( echo "$TIME   Delete file: $FILE" | tee -a "$TMP/trash.log" ) || \
  ( echo "$TIME   ERROR: Delete file: $FILE" )
done

echo "Remove all dirs with only $MAXAGE days old contents from $TMP"
find "$TMP/" -mindepth 1 -maxdepth 1 -type d -mtime +$MAXAGE | while read DIR
do
  NEWEST=`( find "$DIR" -mindepth 1 -mtime -$MAXAGE | while read F ; do find "$F" -printf "%TY%Tm%Td%TH%TM\\n" ; done ) | sort | tail -n 1 `
  if test "$NEWEST"
  then
    ( touch -r "$NEWEST" "$DIR" ) && \
    ( echo "$TIME   Touch $NEWEST of dir: $DIR") || \
    ( echo "$TIME   ERROR: Touch $NEWEST of dir: $DIR" )
  else
    ( rm -rf "$DIR" &> /dev/null ) && \
    ( echo "$TIME   Delete dir: $DIR" | tee -a "$TMP/trash.log" ) || \
    ( echo "$TIME   ERROR: Delete dir: $DIR" )
  fi
done


# vim:ts=4