Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm new to scripting and regular jobs on OS X, so appreciate the help in advance.

I'm looking to have a tmp directory in my homedir, as in /Users/username/tmp which I can use as my "downloads" directory.

My goal is to have files/directories in this directory delete after say 10 days after the atime (last access).

So I am looking for a script and also the method to run it regularly on OS X Lion using ZSH as the shell.


share|improve this question

Write a script that deletes files in a folder if their atime is earlier than ten days ago, then have it run daily (I don't know what the best mechanism for that is on OSX; on Linux I'd use anacron, or failing that run it hourly with cron).

Sample script (takes the directory, eg ~/downloads, as first argument):


setopt extended_glob


[[ -d $targetpath ]] || (echo "Directory does not exist" 1>&2 && exit 1)

rm ${path}/**(.a+10) # ** will match recursively, and the bit in brackets is an
# extended glob qualifier, matching ordinary files with an atime more than ten days ago.
unsetopt extended_glob
exit 0

There's probably all sorts of things wrong with that, and if you habitually use extended_glob you don't need it turned on and later off in the script, naturally.

share|improve this answer
My understanding of setopt was that the option is set only in the current shell (and isn't saved anywhere), so you don't need to unsetopt extended_glob there (but unless you have setopt it in your .zshenv, you will need to setopt it in the script). – Blacklight Shining Oct 22 '12 at 5:48
@BlacklightShining Ah. Good to know that failing to unsetopt it won't cause problems. Thankee. – Darael Nov 22 '12 at 0:16
Usually setopts go in ~/.zshrc, which is not read by zsh before executing scripts. Thus, you should always setopt options you need at the beginning of each script, like you did here. (Again, you don't need to unsetopt them, though.) – Blacklight Shining Nov 22 '12 at 0:45
Yes, I gathered that it was definitely necessary to setopt them. – Darael Dec 11 '12 at 19:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .