This simple Python script helps you clean up files in a specified folder that have been created more than a certain threshold time ago. It can be used to automatically remove old files from a directory.
- Python (>= 3.6) should be installed on your system.
-
Download the
deletingFiles.py
script to your desired location. -
Open the script in a text editor and set the
folder_path
variable to the path of the folder you want to clean. -
Set the
threshold
variable to the desired time limit in seconds. By default, it is set to 30 minutes (30 * 60 seconds). -
Save the changes to the script.
-
You can manually run the script by executing it:
python deletingFiles.py
To automate the process and run the script as a cron job, follow these steps:
-
Open your terminal and edit your crontab file:
crontab -e
-
Add a new line to schedule the script. The following is an example of running the script every 30 minutes:
*/30 * * * * /usr/bin/python3 /path/to/deletingFiles.py
Replace
/usr/bin/python3
with the path to your Python interpreter (you can find it using which python3) and/path/to/clean_files.py
with the actual path to the script. Save and exit the crontab editor. -
Now, your script will run automatically at the specified schedule, cleaning up old files in the specified folder.