Need cron job to delete files every month first week Saturday.

Posted on 2011-10-17
Last Modified: 2013-12-16
Hi experts,

Below is the my corn job Not working :-(. Its deleting file ever Saturday.

0 1 1-7 1-12 6 rm /srv/www/upload_doc/*

My requirement is Every moth first week of Saturday only the files need to be deleted. In above cron stanza what i m missing. pleas help.


Question by:sivakumar_experts
    LVL 3

    Assisted Solution

    This usual solution is something like, first write a wrapper script that will run somecommand only if the day of the month is 7 or less:

    #! /usr/bin/ksh
    day=$(date +%d)
    if ((day <= 7)) ; then
       exec somecommand
    exit 1

    Open in new window

    Then run the wrapper every Saturday: "0 0 * * 1 wrapper"
    LVL 68

    Accepted Solution

    Or all in one:

    0 1 * * 6 [ $(date "+\%d") -le 7 ] &&  rm /srv/www/upload_doc/*

    Note the backslash ("\") in front of the percent ("%") sign! It's very important under cron.

    If you'd like to test on the command line however, you must remove it, since outside cron it will be considered an error.


    Author Closing Comment

    As always wmp rocksssssssss

    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
    It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
    Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
    Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now