Skip to content

Instantly share code, notes, and snippets.

@marcroberts
Created October 30, 2025 16:01
Show Gist options
  • Select an option

  • Save marcroberts/f4153efa3284521fd7dc54e88e2ae46c to your computer and use it in GitHub Desktop.

Select an option

Save marcroberts/f4153efa3284521fd7dc54e88e2ae46c to your computer and use it in GitHub Desktop.
Script to delete files older than x days
#!/bin/bash
# Script to delete files older than X days from a specified folder
# Configuration
FOLDER_PATH="/path/to/your/folder"
DAYS_OLD=30
# Validate folder exists
if [ ! -d "$FOLDER_PATH" ]; then
echo "Error: Folder '$FOLDER_PATH' does not exist"
exit 1
fi
# Find and delete files older than specified days
echo "Searching for files older than $DAYS_OLD days in: $FOLDER_PATH"
# Dry run - show what would be deleted (remove -delete to preview)
find "$FOLDER_PATH" -type f -mtime +$DAYS_OLD -print
# Uncomment the line below to actually delete the files
# find "$FOLDER_PATH" -type f -mtime +$DAYS_OLD -delete
echo "Done!"
# Alternative with confirmation prompt:
# read -p "Delete these files? (y/n) " -n 1 -r
# echo
# if [[ $REPLY =~ ^[Yy]$ ]]; then
# find "$FOLDER_PATH" -type f -mtime +$DAYS_OLD -delete
# echo "Files deleted"
# fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment