I have a script to delete all folders created before past 2 days. Now, on Monday, it deletes just every folder from last week, which I do not want.

Is there an option to consider only weekdays for this command ? The whole script runs every day. I am using this:

find /my-folder/sub-folder/ -type d -ctime +1 | xargs rm -rf
  • I would recommend running stat on the resulting list of results from find, extracting the modified time and then running date -d <date> +a checking for instances not Sat or Sun. Jan 18 '18 at 16:06
  • 1
    ctime IS NOT creation time
    – roaima
    Jan 18 '18 at 18:54

You could do:

find /my-folder/sub-folder/ -type d -ctime "+$((
  1 + 2 * ($(date +%u) <= 2) ))" -prune -exec echo rm -rf {} +

That is, add 2 days if the current day is either 1 (Monday) or 2 (Tuesday) (assuming you're only running that script on week days).

I've added a -prune as it doesn't make sense to descend into directories that you are removing and replaced xargs which would fail for file names that contain blanks or backslashes or quotes with the standard -exec ... {} + alternative.

Note that the ctime of a directory is not updated when a file within it is modified or when files are added or removed to/from sub-directories.

  • Asking will running this find /dir/path -type d -ctime -6in a cron job every Friday help? Jan 18 '18 at 16:15
  • ctime is not creation time.
    – roaima
    Jan 18 '18 at 18:55

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.