Please whitelist cPanel in your adblocker so that you’re able to see our version release promotions, thanks!

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.

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

Discussion in 'General Discussion' started by glpglp, Mar 5, 2018.

  1. glpglp

    glpglp Registered

    Joined:
    Mar 5, 2018
    Messages:
    3
    Likes Received:
    1
    Trophy Points:
    1
    Location:
    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!
     
  2. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    43,215
    Likes Received:
    1,759
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    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.
     
  3. glpglp

    glpglp Registered

    Joined:
    Mar 5, 2018
    Messages:
    3
    Likes Received:
    1
    Trophy Points:
    1
    Location:
    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?
     
  4. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    43,215
    Likes Received:
    1,759
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    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.
     
  5. glpglp

    glpglp Registered

    Joined:
    Mar 5, 2018
    Messages:
    3
    Likes Received:
    1
    Trophy Points:
    1
    Location:
    United States
    cPanel Access Level:
    Website Owner
    Solved. Everything worked well. Thank you for the extra help and the recommendation to the other site.
     
    cPanelMichael likes this.
  6. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    43,215
    Likes Received:
    1,759
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Hello,

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

    Thanks!
     
Loading...

Share This Page