Bash File Rotate And Delete Script

I came across an interesting task where I had to rotate several files in a folder - here is a quick bash script that can go into a specified directory and delete files older than 15 days.

  1. #!/bin/sh
  2. #---------------------------------------------------
  3. # FILE ROTATOR SCRIPT
  4. #
  5. # The purpose of this script is to rotate and delete files
  6. # greater than 14 days.
  7. #---------------------------------------------------
  8.  
  9. # Vars
  10. FILEDIR="/var/log"
  11. AGE="15"
  12.  
  13. # Diagnostics
  14. echo "Rotate starting..."
  15. echo "Directory to search: $FILEDIR"
  16. echo "File age to check $AGE"
  17. echo " "
  18.  
  19. # Grep is used to ignore files with .
  20. FILES_OLD=`find $FILEDIR -type f -mtime +$AGE -print |grep -v '^\.\/\.'`
  21. echo "Files older than $AGE:"
  22. echo $FILES_OLD
  23. echo " "
  24.  
  25. # Deletes old images, notes and hashes.
  26. echo "Deleting old files..."
  27. #find $FILEDIR -mtime +$AGE -exec rm -f {} \;
  28. echo "Rotate complete..."

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.