Old Files Cleaner is a simple yet effective shell script that automatically deletes old files from a specified directory. It helps keep your file system tidy by removing files older than a certain number of days. You can also tell it to ignore specific folders and file types, and it even has a "dry-run"ng anything. This script is perfect for setting up as a cron job to clean up old mode so you can test it without actually deleti files regularly.
Old Files Cleaner includes the following features:
- 🗑️ Automated File Deletion: Deletes files older than a specified number of days.
- 🎯 Target Directory: You can specify which directory to clean up.
- 🚫 Exclude Directories: Option to prevent the script from deleting files in certain subdirectories.
- 🗄️ Exclude File Patterns: Option to prevent the script from deleting files matching specific patterns (like file extensions).
- 🧪 Dry-Run Mode: Test the script and see what would be deleted without actually deleting anything. This is useful for testing and making sure it works as expected!
- ⏰ Cron Job Ready: Designed to be easily set up as a cron job for automatic, scheduled cleanups.
This script is a simple bash script, so there's no real "installation" needed. Just follow these steps:
- Download the script: Save the script content to a file named
auto-deleter.sh. You can usewgetorcurlif you are downloading it from somewhere, or simply copy and paste the code into a new file using a text editor. - Make it executable: Open your terminal and navigate to the directory where you saved
auto-deleter.sh. Then, make the script executable by running:
chmod +x auto-deleter.shThat's it! The script is now "installed" and ready to use.
You run the script directly from your terminal. Here's how to use it and the options you can configure:
Basic Usage:
./auto-deleter.shThis will run the script with default settings (if any are hardcoded in the script - check the script itself for defaults). To be effective, you'll likely want to use the options below.
Options:
-
--dry-run: Run the script in dry-run mode. This will show you what files would be deleted without actually deleting them. Very useful for testing!./auto-deleter.sh --dry-run
-
--target-dir <directory>: Specify the directory you want to clean up. Replace<directory>with the actual path to the directory. If you don't specify this, the script might have a default directory set internally (check the script)../auto-deleter.sh --target-dir /path/to/your/target/directory
-
--exclude-dir <directory>: Specify a directory to exclude from deletion. You can use this option multiple times to exclude more than one directory. Replace<directory>with the directory name you want to exclude (relative to the target directory or absolute path)../auto-deleter.sh --target-dir /path/to/target --exclude-dir important_folder ./auto-deleter.sh --target-dir /path/to/target --exclude-dir folder1 --exclude-dir folder2
-
--exclude-file <pattern>: Specify a file pattern to exclude from deletion. This uses standard file pattern matching (like*.txtfor all text files). You can use this option multiple times to exclude multiple patterns../auto-deleter.sh --target-dir /path/to/target --exclude-file "*.log" ./auto-deleter.sh --target-dir /path/to/target --exclude-file "*.txt" --exclude-file "*.csv"
Example Usage with Options:
To do a dry-run cleanup of the /home/user/downloads directory, excluding any folder named archive and any files ending in .temp, you would run:
./auto-deleter.sh --dry-run --target-dir /home/user/downloads --exclude-dir archive --exclude-file "*.temp"Setting up as a Cron Job (Automatic Scheduling):
This script is designed to be run automatically on a schedule using cron. Here's how to set it up to run daily at midnight:
-
Open your crontab: In your terminal, type:
crontab -e
This will open your crontab file in a text editor. If it's your first time, you might be asked to choose an editor.
-
Add the cron job: Add the following line to the crontab file. Make sure to replace
/path/to/auto-deleter.shwith the actual path to where you saved yourauto-deleter.shscript.0 0 * * * /path/to/auto-deleter.sh --target-dir /path/to/your/target/directory --exclude-dir important_folder0 0 * * *: This part schedules the script to run at midnight every day./path/to/auto-deleter.sh: Replace this with the full path to yourauto-deleter.shscript. For example, if you saved it in your home directory, it might be/home/yourusername/auto-deleter.sh.--target-dir /path/to/your/target/directory --exclude-dir important_folder: Add any options you want to use every time the script runs automatically. Remember to customize these options!
-
Save and exit: Save the crontab file and exit the editor. Cron will automatically pick up the changes.
Important Notes for Cron Jobs:
- Full Paths: When using cron, using full paths to your script and any directories is best. Cron environments are often minimal and might not have the same environment variables set as your interactive shell.
- Logging: For cron jobs, especially those that delete files, it's a good idea to add logging to your script to record what it does. You can redirect output to a log file within the script using redirection like
>> /path/to/logfile.log 2>&1.
Before setting up a cron job or running the script on important data, always use the --dry-run option first! This will let you see exactly which files the script intends to delete without actually deleting anything. Check the output carefully to make sure it's behaving as you expect.
- Kolja Nolte [email protected]
This project is licensed under the MIT License. See the LICENSE file for details. (You can add a LICENSE file to your repository with the MIT License text if you wish).