-1

How to remove zip files in specific folder in my hosting by cron job?

I want to remove zip files in specific folder every day at 5:00PM

Please help.

2 Answers 2

1

You can add a cronjob which will run at 5 PM daily which will find and remove all .zip files in the specified directory as:

find . -type f -name "*.zip" -exec rm -f {} \;

This will find all the .zip files in the current directory and remove them; this DOES not include directories named *zip

You can put the above command in a shell script as it is and then run it via cron.

To add a cron, simply put the following in the file that opens up after typing crontab -e:

0 17 * * * script.sh

Where script.sh is the script containing that one line command.

4
  • Thank you for reply. This my cron job screen how I can use this command on it ? i.imgur.com/sHnXFt2.png
    – Ali
    Nov 2, 2018 at 19:43
  • From the picture, the Weekday should have either 5 or 17 (consult with your hosting provider to clarify on that) and in the Minute field, it should be 00. In the Command field, you need to put the command that I shared (find . -type f -name "*.zip" -exec rm -f {} \;). Again, I'd recommend to ask your provider for how to set the time via GUI but the command should be the same.
    – sla3k
    Nov 2, 2018 at 19:52
  • Thank you for help I appreciate that my folder path is: public_html/ali/hi How I can write the command ?
    – Ali
    Nov 2, 2018 at 19:55
  • From the GUI, I am not sure but in order to search inside public_html/ali/hi when the command is called/run from something/public_html/ali/hi: find something/public_html/ali/hi -type f -name "*.zip" -exec rm -f {} \;; you can also put in the absolute path (from the terminal, run pwd while you are cd'd into ...ali/hi directory), the output is your absolute path to this directory. In this case, use: find absolute_path_here -type f -name "*.zip" -exec rm -f {} \;
    – sla3k
    Nov 2, 2018 at 20:00
0

change folder_path to your value

  1. crontab -e
  2. $(which find) folder_path -maxdepth 0 -name "*.zip" -delete
  3. save file (if vim :wq)

check cronjob installed: crontab -l

2
  • Thank you for help but unfortunately not work!
    – Ali
    Nov 2, 2018 at 22:12
  • Do you need exact values that you can use in your GUI? Did you change folder_path to the path of your choice? What is a full path of the directory you need to remove files from? Nov 5, 2018 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.