SOLVED Need help setting up Cron to delete jpg files older than 1 day

glpglp

Registered
Mar 5, 2018
3
1
1
United States
cPanel Access Level
Website Owner
Greetings:

Need to delete .jpg files that are accumulating in a specific folder. The files are not needed and are taking up GB's of space if not deleted regularly. Plan to run the script once daily at 3:00AM and delete files older than 1 day.

Here is the script which I think will do the trick. Can someone confirm or advise with corrections?

find /home/mydomain/public_html/top_folder/folder/*.jpg -mtime +1 -exec rm {} \;

Thanks to anyone helping!
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,880
2,261
463
Hello,

The command you referenced looks correct, however you may want to move the files to another location first to verify it's working as expected. EX:

Code:
find /path/to/files/ -type f -name '*.jpg' -mtime +1 -exec mv {} /path/to/archive/ \;
Once you confirmed it's detecting the correct files, you can update the cron job to remove the files instead of moving them.

Thank you.
 

glpglp

Registered
Mar 5, 2018
3
1
1
United States
cPanel Access Level
Website Owner
Thank you ... one more question ... will either of these scripts accomplish the same thing to remove the .jpg files from the folder?

find /path/to/files/*.jpg -mtime +1 -exec rm {} \;

find /path/to/files/ -type f -name '*.jpg' -mtime +1 -exec rm {} \

If yes, is there an advantage to using one script over the other?
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,880
2,261
463
Hello,

Both of those bash commands are using the "find" utility to find a list of files matching the search term, and then remove those files with the "rm" command. The difference with the second command is that it's searching for files that end with ".jpg", whereas the first command is searching for files or directories that end in ".jpg". Note that websites such as StackOverflow are generally a better resource for assistance with custom bash commands. EX:

find - exec rm vs -delete

Thank you.
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,880
2,261
463
Hello,

I'm glad to see it's working well. I've marked this thread as solved.

Thanks!