This utility provides a configurable way to clean up old files in specified directories on Debian systems. The clean-old-files script checks directories for files older than a configurable number of hours and removes them, excluding specified files and patterns. All actions, warnings, and errors are logged to the local syslog and outputted to STDERR.
- Features
- Requirements
- Installation
- Configuration
- Usage
- Viewing Logs
- Customising the Schedule
- Uninstallation
- Author
- License
- Disclaimer
- Configurable Directories and Patterns: Specify which directories to clean and which patterns to exclude.
- Age Threshold: Set the age threshold in hours for files to be processed.
- Dry Run Mode: List files without deleting them by default.
- Syslog Logging: Logs all actions to the local syslog.
- Systemd Integration: Includes service and timer unit files for periodic execution.
- Debian-based operating system.
- Zsh shell installed (
zsh). - Systemd (for service and timer units).
To install the clean-old-files utility using the install.sh script, you can run the following command:
sudo bash -c "$(curl -fsSL https://gist.githubusercontent.com/troykelly/353f99a8206d49e6a6aae5f6bb4e35b5/raw/install.sh)"Important Notes:
-
Running as Root: The script installs files to system directories and requires root privileges. It is invoked with
sudoto ensure it has the necessary permissions. -
Security Consideration: Running scripts directly from the internet with elevated privileges can be risky. Ensure you trust the source before executing such commands.
-
Alternative: If you prefer to inspect the script before running it, you can download it first:
curl -fsSL https://gist.githubusercontent.com/troykelly/353f99a8206d49e6a6aae5f6bb4e35b5/raw/install.sh -o install.sh sudo bash install.sh
-
Checks for Required Commands: Ensures that
curlandsystemctlare available. -
Downloads Files:
- Downloads
clean-old-filesscript to/usr/local/sbin/and sets it as executable. - Downloads the environment file to
/etc/. - Downloads the systemd service and timer files to
/etc/systemd/system/.
- Downloads
-
Sets Permissions and Ownership:
- Sets appropriate permissions and ownership for each file.
-
Reloads systemd Daemon:
- Reloads the systemd manager configuration to recognize the new unit files.
-
Enables and Starts the Timer:
- Enables the
clean-old-files.timerto start at boot. - Starts the timer immediately.
- Enables the
-
Logs Progress:
- Outputs informative messages during the installation process.
-
Configuration:
- Edit
/etc/clean-old-files.envto adjust configuration variables to your requirements.
- Edit
Save the clean-old-files script to /usr/local/sbin/ and make it executable:
sudo cp clean-old-files /usr/local/sbin/
sudo chmod +x /usr/local/sbin/clean-old-filesCreate the /etc/clean-old-files.env file with the default configuration:
sudo touch /etc/clean-old-files.env
sudo chmod 644 /etc/clean-old-files.env
sudo chown root:root /etc/clean-old-files.envCopy the service and timer files to /etc/systemd/system/:
sudo cp clean-old-files.service /etc/systemd/system/
sudo cp clean-old-files.timer /etc/systemd/system/Reload the systemd daemon to recognise the new unit files, enable the timer to start at boot, and start the timer:
sudo systemctl daemon-reload
sudo systemctl enable clean-old-files.timer
sudo systemctl start clean-old-files.timerModify the configuration variables in /etc/clean-old-files.env to suit your requirements:
# Age threshold in hours. Files older than this will be processed.
FILE_CLEANUP_HOURS_THRESHOLD=24
# Space-separated list of directories to clean.
FILE_CLEANUP_DIRECTORIES="/path/to/directory1 /path/to/directory2"
# Space-separated list of patterns to exclude from deletion.
FILE_CLEANUP_EXCLUDE_PATTERNS="*.log *.tmp"
# Set to a truthy value to enable deletion. Otherwise, the script will only list files.
FILE_CLEANUP_DOIT=0Truthy Values for FILE_CLEANUP_DOIT: 1, yes, true, on (case-insensitive).
-
Set the Age Threshold to 48 Hours:
FILE_CLEANUP_HOURS_THRESHOLD=48 -
Specify Directories:
FILE_CLEANUP_DIRECTORIES="/home/user/downloads /var/tmp"
-
Exclude Specific Patterns:
FILE_CLEANUP_EXCLUDE_PATTERNS="*.conf important_file.txt"
-
Enable Deletion:
FILE_CLEANUP_DOIT=1
Before enabling deletion, it's recommended to run the script manually to see which files would be affected.
Run the Script Manually:
sudo /usr/local/sbin/clean-old-filesCheck the Logs:
sudo journalctl -t clean-old-filesOnce satisfied with the files that would be processed, set FILE_CLEANUP_DOIT to a truthy value in /etc/clean-old-files.env to enable deletion.
Enable Deletion in Environment File:
FILE_CLEANUP_DOIT=yesRun the Script Manually or Wait for Timer:
sudo /usr/local/sbin/clean-old-filesAll log messages are sent to the local syslog with the identifier clean-old-files. You can view the logs using:
sudo journalctl -t clean-old-filesTo adjust how often the script runs, edit the OnCalendar value in clean-old-files.timer.
Examples:
-
Every Day at Midnight:
OnCalendar=*-*-* 00:00:00 -
Every 15 Minutes:
OnCalendar=*:0/15
After making changes, reload the daemon and restart the timer:
sudo systemctl daemon-reload
sudo systemctl restart clean-old-files.timerFor more scheduling options, refer to the systemd.time manual.
To remove the service, timer, and configuration:
sudo systemctl stop clean-old-files.timer
sudo systemctl disable clean-old-files.timer
sudo rm /etc/systemd/system/clean-old-files.service
sudo rm /etc/systemd/system/clean-old-files.timer
sudo rm /usr/local/sbin/clean-old-files
sudo rm /etc/clean-old-files.env
sudo systemctl daemon-reload- Name: Troy Kelly
- Username: @troykelly
- Contact: troy@team.production.city
Ensure you thoroughly test the script in a safe environment before deploying it on production systems. Adjust file paths and configurations according to your specific setup. The author is not responsible for any loss or damage caused by the use of this script.