where +t is the time criteria to search the files. For example, I took a lot of screenshots on my Gnome shell desktop and it resulted in tons of redundant images in my Pictures folder. I used the following command to delete old images in the folder: This will find all files in the Pictures folder that are more than 1 day old and (force) remove them all. More options
- You can change the path to point to any folder. Make sure you use absolute path.
- You can change the time interval. -mtime refers to number of days while -mmin refers to number of minutes.
- You can set the time criteria. A “+5” value means more than 5 days/minutes while a “-5” value means 5 days/minutes or less. You can also use a combination like “+5 -10” to denote an interval that is more than 5 days/minutes and less than 10 days/minutes.
- The “exec” command will work for command like “cp”, “mv”, “rm”, “rmdir” etc. Other than deleting old files, you can get it to perform other tasks as well, such as moving a file out of Dropbox folder.
Automating the process
There are several ways that you can automate the process.
1. Run on startup
Copy the following code and paste them onto a text editor. Remember to change the file path, time interval, time criteria to suit your needs. Save the file as “find-and-delete” in your Home folder. Open the File Manager app and browse to the Home folder. Right click on the “find-and-delete” file and select Properties. Go to the Permissions tab and check the box “Allow executing file as program”.
Next, open up the Startup Application and add the the “find-and-delete” file to the list.
2. Cron
If you need the script to run on a regular interval, you can set a cron job. Open a terminal and type the following: If it prompts you to select an editor, enter “2” (for nano). The structure for cron setup is To run the script at every hour, enter at the end of the crontab file, on a new line.
Save (Ctrl + o) and exit (Ctrl + x) the crontab.
Gnome Schedule
If using crontab from the terminal scares you out, you can use Gnome-schedule to schedule the task.
That’s it. The methods above are tested on Ubuntu, but it should work on all Linux distro. Image credit: Delete by Big Stock Photo.