The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

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

Discussion in 'General Discussion' started by Alfaman, May 1, 2012.

  1. Alfaman

    Alfaman Member

    Joined:
    Jan 6, 2012
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    1
    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.
     
  2. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,811
    Likes Received:
    671
    Trophy Points:
    113
    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.
     
  3. Alfaman

    Alfaman Member

    Joined:
    Jan 6, 2012
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    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?
     
  4. Alfaman

    Alfaman Member

    Joined:
    Jan 6, 2012
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    1
    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 {}\;
     
  5. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,811
    Likes Received:
    671
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
    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.
     
  6. cPanelTristan

    cPanelTristan Quality Assurance Analyst
    Staff Member

    Joined:
    Oct 2, 2010
    Messages:
    7,623
    Likes Received:
    21
    Trophy Points:
    38
    Location:
    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.
     
  7. Alfaman

    Alfaman Member

    Joined:
    Jan 6, 2012
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    1
    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/ \;
     
    #7 Alfaman, May 2, 2012
    Last edited: May 3, 2012
  8. cPanelTristan

    cPanelTristan Quality Assurance Analyst
    Staff Member

    Joined:
    Oct 2, 2010
    Messages:
    7,623
    Likes Received:
    21
    Trophy Points:
    38
    Location:
    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!
     
  9. Alfaman

    Alfaman Member

    Joined:
    Jan 6, 2012
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    1
    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 \;
     
  10. cPanelTristan

    cPanelTristan Quality Assurance Analyst
    Staff Member

    Joined:
    Oct 2, 2010
    Messages:
    7,623
    Likes Received:
    21
    Trophy Points:
    38
    Location:
    somewhere over the rainbow
    cPanel Access Level:
    Root Administrator
    Yep, the -v flag is in the right place after the mv command.
     
  11. Alfaman

    Alfaman Member

    Joined:
    Jan 6, 2012
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    One last question, assuming the cron job should go in /var/spool/cron/root ?
     
  12. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,811
    Likes Received:
    671
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
    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.
     
  13. Alfaman

    Alfaman Member

    Joined:
    Jan 6, 2012
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    Sorry, one last question. Once the new cron file is edited, does anything need to be re-started?
     
  14. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,811
    Likes Received:
    671
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
  15. Alfaman

    Alfaman Member

    Joined:
    Jan 6, 2012
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    Thank you for your help guys.
     
Loading...

Share This Page