Eric's Wonderful Things RSS

Archive

Jun
1st
Sun
permalink

Automatic Delete in OS X

If you’re like me, your downloads directory tends to fill up very quickly. I wanted to automate this so that I didn’t have manually delete old files in OS X 10.5.

The goal is to have any files that are older than 2 days get moved to the trash.

After searching for a GUI utility to achieve this, I determined that that command line was going to be the only way to go. I’m a bit of a terminal weenie, but I decided buck up. Here are the step by step instructions how I did it.

We will be using trash, which is similar to rm, except that rather than rather than straight up deleting the files (which is what rm does by default in OS X), it moves the files to Trash.


  1. We will be using the CSH for our shell (since thats how I found most of my instructions), so the first thing we need to do is change this.
    Launch Terminal
    Go to Terminal -> Preferences -> Startup
    Change the default login shell to say:
    /bin/csh
    OS X Terminal Screenshot
    Now relaunch Terminal

  2. Install trash
    Go here, copy the text, and save it in your /bin directory as “trash”. This will give you the trash command in your shell, however, you can use it in place of rm. O’reilly shows you how here.

  3. Create a bin directory in your user directory, if you don’t already have one. (I didn’t)
    Open up terminal and type:
    mkdir ~/bin

  4. Now lets create a shell script to delete the files. First we will make the script list the files to test first.
    Paste the following text:

    #!/bin/sh
    echo "Delete Downloads"
    find /Users/ehensley/Downloads -mtime +2 -type f -exec ls -la {} \;


    Obviously, you will need to change the path to reflect the directory you want to clean. You can also change the +2 to reflect how many days you want to go back. Hit ctrl + x and save.

  5. Lets chmod the script so that it can be run
    sudo chmod 755 delete.sh

  6. Now we will test the script to see if it gets hits the files that you want.
    In the terminal type:
    ~/bin/delete.sh
    You should see a list of files to be deleted, assuming you have some in the directory. You can tweak the find query to fine tune what you want to delete. Assuming this looks good, we now need to change the query to actually delete the files now.

  7. We will now replace
    find /Users/ehensley/Downloads -mtime +2 -type f -exec ls -la {} \;
    TO
    find /Users/ehensley/Downloads -mtime +2 -type f -exec trash {} \;Type:
    sudo pico ~/bin/delete.sh
    Then replace ls -la with trash
    Hit ctrl + x and save your changes.

  8. Now we will schedule our shell script to run using crontab, but first, lets make a backup just to be safe.
    In the terminal type:
    sudo cp /private/etc/crontab /private/etc/crontab.bak

  9. Alright, since the default editor is vi, and I suck at vi, we will change the default text editor to be pico
    In the terminal type:
    setenv EDITOR pico

  10. The final step is to schedule the shell script to run at a set interval. Now the formating for this is a little confusing so I would recommend reading up on crontab here for a background. I should also note, for the next step, DO NOT copy the text, because the formating will get messed up. You will need to hit tab after every entry.
    In the terminal type:
    crontab -e
    Now following this formatting, type the following, hitting tab after each entry.
    45 10 * * * sh ~/bin/delete.sh



    Once you are done, put a return at the end.

    Now hit ctrl + x and save it with the default name, which should be something like /tmp/crontab.LHvBILBjWO

    This format should launch my job every day at 10:45am and 10:45pm.

  11. Thats it! Once you exit out it should say:
    crontab: installing new crontab
    You can check to make sure that your cronjob is scheduled by typing
    crontab -l



Sources:

http://www.oreillynet.com/pub/h/334
http://www.macdevcenter.com/pub/a/mac/2001/12/14/terminal_one.html?page=2
http://www.macdevcenter.com/pub/a/mac/2002/07/02/terminal_5.html?page=2
http://www.linuxquestions.org/questions/linux-general-1/shell-script-to-remove-old-files-based-on-date-7368
http://www.macosxhints.com/article.php?story=2001020700163714