Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7701547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:00:48+00:00 2026-05-31T23:00:48+00:00

I want to make a cron job that checks if a folder exists, and

  • 0

I want to make a cron job that checks if a folder exists, and it if does to delete all the contents of that folder. For example, I know that the following will delete the contents of my folder in using cron:

0 * * * * cd home/docs/reports/;rm -r *

However, I realized that if the folder is removed (or the wrong file path is given) instead of the contents of that folder being deleted, cd fails and all files are deleted on my operating system. To prevent this from happening (again) I want to check for the existence of the folder first, and then to delete the contents. I want to do something like the following, but I’m not sure how to use a bash script with cron.

if [ -d "home/docs/reports/" ]; then
    cd home/docs/reports/;rm -r *
fi

I’m new to bash and cron (in case it is not obvious).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T23:00:49+00:00Added an answer on May 31, 2026 at 11:00 pm

    I think cron uses /bin/sh to execute commands. sh is typically a subset of bash, and you’re not doing anything bash-specific.

    Execute the rm command only if the cd command succeeds:

    0 * * * * cd home/docs/reports/ && rm -r *
    

    NOTE Please wait a few minutes while I test this. If this note is gone, I’ve tried it and it works.

    Yes, it works. (Note that testing whether the directory exists is less reliable; it’s possible that the directory exists but you can’t cd into it, or it might cease to exist between the test and the cd command.)

    But actually you don’t need to use a compound command like that:

     0 * * * * rm -r home/docs/reports/*
    

    Still the && trick, and the corresponding || operator to execute a second command only if the first one fails, can be very useful for more complicated operations.

    (Did you mean /home/docs rather than home/docs? The latter will be interpreted relative to your home directory.)

    Though this worked ok when I tried it, use it at your own risk. Any time you combine rm -r with wildcards, there’s a risk. If possible, test in a directory you’re sure you don’t care about. And you might consider using rm -rf if you want to be as sure as possible that everything is deleted. Finally, keep in mind that the * wildcard doesn’t match files or directories whose names start with ..

    #include <stddisclaimer.h>
    

    EDIT :

    The comments have given me a better understanding of what you’re trying to do. These are files that users are going to download shortly after they’re created (right?), so you don’t want to delete anything less than, say, 5 minutes old.

    Assuming you have GNU findutils, you can do something like this:

    0 * * * *  find /home/docs/reports/* -cmin +5 -delete 2>/dev/null
    

    Using the -delete option to find means you’re deleting files and/or directories one at a time, not deleting entire subtrees; the main difference is that an old directory with a new file in it will not be deleted. Applying -delete to a non-empty directory will fail with an error message.

    Read the GNU find documentation (info find) for more information on the -cmin and -delete options. Note that -ctime operates on the time of the last status change of the file, not its creation time (Unix doesn’t record file creation times). For your situation, it’s likely to be the same.

    (If you omit the /* on the path, it will delete the reports directory itself.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would need a cron job that filters all the rows that have (travellingPath.endDate>now)
I want to run a cron job every minute that will launch a script.
I have link for example domain.com/de/controler/action?param=value and I want make actionlink to keep same
i need to make a cron job to run a java program every 40
I want to make a news portal(php) with minimum mysql force. :create a cron,
I want make a UI that is semi transparent in WPF VS2008, so I
I have this cron job to empty a temp folder I have once a
I want to schedule a cron job on Google App Engine to view my
I have 50+ scripts in folder cron-scripts , I only want exec them with
I want to make a simple script just to test the time that the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.