find-old-files.sh 628 B

12345678910111213141516171819202122232425
  1. #!/bin/bash
  2. ## Find files older than given age.
  3. ## Replacement for busybox find lacking mtime argument.
  4. ## This script doesn't traverse directories.
  5. ## danja@k0a1a.net | Superglue project | 2016
  6. set -e
  7. DIR=$1
  8. MAXAGEDAYS=$2
  9. [ ! -z $DIR ] || (echo "usage: $(basename $0) <dir> <days>"; exit 1;)
  10. [ -d $DIR ] || (echo $DIR' dir does not exist'; exit 1;)
  11. [ ! -z $MAXAGEDAYS ] && [ -z "${MAXAGEDAYS##[0-9]*}" ] || (echo 'second parameter must be number of days'; exit 1;)
  12. MAXAGE=$(($(date +%s)-86400*${MAXAGEDAYS}))
  13. for FILE in $DIR/*; do
  14. if [[ $(date -r $FILE +%s) -le $MAXAGE ]]; then
  15. echo $FILE
  16. fi
  17. done