Directory Cleanup Bash Script for Removing Unnecessary Files

marius

Administrator
Staff member
Introduction:
This bash script cleans up specified directories by deleting temporary and log files older than a defined threshold. It helps maintain disk space and improves system performance.

Bash:
#!/bin/bash

# Directory to clean up
TARGET_DIR="/path/to/cleanup"

# Remove .tmp and .log files older than 7 days
find $TARGET_DIR -type f \( -name "*.tmp" -o -name "*.log" \) -mtime +7 -exec rm {} \;

echo "Cleanup complete in $TARGET_DIR."

Instructions:
Execute the following commands to run the cleanup script:
Code:
chmod +x dir_cleanup.sh
./dir_cleanup.sh

Example Output:
Code:
Cleanup complete in /path/to/cleanup.
 
Back
Top