Please help me setup a Cronjob to delete files older than 7days old

Alfaman

Member
Jan 6, 2012
17
2
53
cPanel Access Level
Root Administrator
Sorry for the noob question.

Please can anyone help me setup a cronjob to delete files older than 7days in cetain directorys for instance: -

/home/mydomain/public_html/top_folder/folder < files older than 7days in folder
/home/mydomain/public_html/top_folder/folder1 < files older than 7days in folder1
/home/mydomain/public_html/top_folder/folder2 < files older than 7days in folder2
/home/mydomain/public_html/top_folder/folder3 < files older than 7days in folder3
/home/mydomain/public_html/top_folder/folder4 < files older than 7days in folder4
/home/mydomain/public_html/top_folder/folder5 < files older than 7days in folder5

Hope the above makes sense.

Thank you in advance for all help given.
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,910
2,216
463
Hello :)

We are typically hesitant to provide users with scripts that invoke the "rm" command because users can sometimes enter it incorrectly and delete required files. My advice would be to use a search engine such as Google to determine a specific command to configure for this cron job. The search term "cron job delete files after days" appears to yield useful results.

Thank you.
 

Alfaman

Member
Jan 6, 2012
17
2
53
cPanel Access Level
Root Administrator
Hello :)

We are typically hesitant to provide users with scripts that invoke the "rm" command because users can sometimes enter it incorrectly and delete required files. My advice would be to use a search engine such as Google to determine a specific command to configure for this cron job. The search term "cron job delete files after days" appears to yield useful results.

Thank you.
I understand where you're coming from, does this look correct?

find /home/mydomain/public_html/top_folder/folder -mtime +7 -exec rm {}\;
find /home/mydomain/public_html/top_folder/folder1 -mtime +7 -exec rm {}\;
find /home/mydomain/public_html/top_folder/folder2 -mtime +7 -exec rm {}\;
find /home/mydomain/public_html/top_folder/folder3 -mtime +7 -exec rm {}\;
find /home/mydomain/public_html/top_folder/folder4 -mtime +7 -exec rm {}\;
find /home/mydomain/public_html/top_folder/folder5 -mtime +7 -exec rm {}\;

What time would the cron run these jobs?
 

Alfaman

Member
Jan 6, 2012
17
2
53
cPanel Access Level
Root Administrator
Actually should it be as follows?

0 0 * * * find /home/mydomain/public_html/top_folder/folder -mtime +7 -exec rm {}\;
0 0 * * * find /home/mydomain/public_html/top_folder/folder1 -mtime +7 -exec rm {}\;
0 0 * * * find /home/mydomain/public_html/top_folder/folder2 -mtime +7 -exec rm {}\;
0 0 * * * find /home/mydomain/public_html/top_folder/folder3 -mtime +7 -exec rm {}\;
0 0 * * * find /home/mydomain/public_html/top_folder/folder4 -mtime +7 -exec rm {}\;
0 0 * * * find /home/mydomain/public_html/top_folder/folder5 -mtime +7 -exec rm {}\;
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,910
2,216
463
That command looks mostly correct. However, there should be a space between the "{}" and "\" characters. EX:

Code:
0 0 * * * find /home/mydomain/public_html/top_folder/folder -mtime +7 -exec rm {} \;
Instead of:

Code:
0 0 * * * find /home/mydomain/public_html/top_folder/folder -mtime +7 -exec rm {}\;
Thank you.
 

cPanelTristan

Quality Assurance Analyst
Staff member
Oct 2, 2010
7,607
40
248
somewhere over the rainbow
cPanel Access Level
Root Administrator
Rather than deleting these files, I would really suggest moving them to archive instead. You could use a similar command but with mv to move them to an archive folder. At least that way, you move all the files to the same archive folder and, should you decide to remove the files at some point, you can then clear them out of that archive folder. It's up to you, but moving them to archive is preferable to deleting them entirely using a cron. You'll probably end up having someone want a file that ends up deleted, then you'll have no way to recover it.
 

Alfaman

Member
Jan 6, 2012
17
2
53
cPanel Access Level
Root Administrator
Thank you for your help, does this look correct to move to backup drive?

Code:
0 0 * * * find /home/mydomain/public_html/top_folder/folder -mtime +7 -exec mv {} /backup/oldfiles/ \;
 
Last edited:

cPanelTristan

Quality Assurance Analyst
Staff member
Oct 2, 2010
7,607
40
248
somewhere over the rainbow
cPanel Access Level
Root Administrator
Hello,

This command should work:

Code:
find /home/mydomain/public_html/top_folder/{folder,folder1,folder2,folder3,folder4,folder5} -type f -mtime +7 -exec mv {} /backup/oldfiles \;
The above will work for all the folders and move the contents into /backup/oldfiles directory. Of note, if any file names are the same, there's going to be an issue. I tested the above successfully on my machine on one of my accounts. If you add the -v flag after mv, it would also provide verbose output on the files moved. If you add that into the cron and have it email to you, it might well provide that as a report with it (not sure, didn't test that portion).

Thanks!
 

Alfaman

Member
Jan 6, 2012
17
2
53
cPanel Access Level
Root Administrator
Thank you Tristran.

I'm going to add this to the cron, it the -v in the correct place?

Code:
0 0 * * * find /home/mydomain/public_html/top_folder/{folder,folder1,folder2,folder3,folder4,folder5} -type f -mtime +7 -exec mv -v {} /backup/oldfiles \;
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,910
2,216
463
Yes, you could add it as a cron job for the "root" user. Or, you could add it as a cron job under the account username, whichever you prefer.

Thank you.