Skip to content

Instantly share code, notes, and snippets.

@ostechnix
Last active July 11, 2024 11:45
Show Gist options
  • Save ostechnix/5f325dba6cc185da2463c7f9135abcdb to your computer and use it in GitHub Desktop.
Save ostechnix/5f325dba6cc185da2463c7f9135abcdb to your computer and use it in GitHub Desktop.
DelOldFile - A Bash Script to Find and Remove all Log Files older than 30 days
#!/usr/bin/env bash
# ------------------------------------------------------------------
# Script Name: deloldfile.sh
# Description: Remove all Log Files older than 30 days
# Website: https://gist.github.com/ostechnix
# Version: 1.0
# Usage: sudo ./deloldfile.sh
# ------------------------------------------------------------------
echo "This script will remove all log files older than 30 days from the /var/log directory of your server."
echo "This operation cannot be undone. Are you sure you want to proceed?"
echo "Enter 'y' to continue, 'n' to cancel, or 'c' to cancel and exit the script:"
read -r response
case $response in
[yY])
echo "Removing old log files..."
sudo find /var/log -type f -mtime +30 -delete
;;
[nN])
echo "Operation cancelled. No log files will be removed."
;;
[cC])
echo "Exiting the script."
exit 0
;;
*)
echo "Invalid input. Please enter 'y', 'n', or 'c'."
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment