Created
October 30, 2025 16:01
-
-
Save marcroberts/f4153efa3284521fd7dc54e88e2ae46c to your computer and use it in GitHub Desktop.
Script to delete files older than x days
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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