Hello again,
Ok, let's give this a go then 🙂
Personally, for editing scripts I like to use the command line 'vi' editor, but for starting with that may scare you off so lets use the regular TextEditor application for the time being.
Fire up TextEditor and open a new document. Change the type of the document to be a plain text file. This can be done via the Format menu -> Make Plain Text option (if it says Make Rich Text, then the file is already in plain text mode).
In the editor, copy and paste the following,
but remove the line numbers I've added:
1 #!/bin/bash
2
3 logFile=/tmp/SafariCleanerLog.txt
4
5 echo "-----------------" >> $logFile
6 echo `date` >> $logFile
7 echo "" >> $logFile
8 echo "Deleting Safari cache files in: $HOME/Library/Caches/Safari/" >> $logFile 2>&1
9 echo "" >> $logFile
10
11 rm -rf $HOME/Library/Caches/Safari/* >> $logFile 2>&1
Some lines may appear to wrap around in the webpage, so use the line numbers as your guide to know if something is supposed to fit on one line or not.
This is what it does:
- The 1st line indicates what particular shell application to use to execute this script (/bin/bash in this case)
- The 3rd creates a variable that I'm using to store the path to a log file for what this script is doing
- Lines 5 to 9 append some logging information to the logfile we've decided to report to
- Line 11 does the actual deletion of the Safari cache files. rm is short for remove, aka delete, and the -rf means
recursively (i.e. all subdirectories in the Safari cache folder) and
forceably (i.e. don't askif you are sure for each and every file). The cryptic 2>&1 at the very end is to make any error messages that rm might generate appear in our logfile. If you try out the commands to see how they work, please please please be very careful with rm, once a file has been rm'ed its gone - it doesn't go to the Trashcan
Note that if you want to see more about what each of these commands do, you can use the man pages either from the Terminal itself or
here. ('echo' is a built-in command to bash, so you would need to read the bash manpage for information on that)
Ok, so now we have a script. Save that on your Desktop with a name that makes sense for you (e.g. SafariCacheCleaner.sh - sh is the standard extension used to indicate something is a script, it's not required)
Now we need to execute a few Terminal commands to make this work when a student logs out. Fire up the Terminal from /Applications/Utilities/Terminal.app
The first thing we need is somewhere to place the script that is out of bounds to regular users, students, etc. OS X already has an area for scripts in /Library/Scripts/, so you may want to create your own area under this folder. For example, I created a 'Custom' folder via the following:
sudo mkdir /Library/Scripts/Custom
You may want to replace Custom with something else, like SchoolAdmin for instance. sudo is a command that gives the executor temporary administration rights. When you press return after typing the above command, you will be asked for a password - enter the password for your current user account and press return (note that you will not see anything appear in the Terminal while you are typing, this is a security feature).
Next up, we need to copy the script to this location and give it the correct permissions to be executed:
sudo cp ~/Desktop/SafariCacheCleaner.sh /Library/Scripts/Custom/
sudo chmod 755 /Library/Scripts/Custom/SafariCacheCleaner.sh
In this case, sudo will probably not prompt you for the password as it trusts access for a few minutes. If you leave things for, say, 10 minutes, it would ask you for your password again.
cp is the command for copying files/folders.
chmod is used to change permissions on a file. In this case, 7 means the owner of the file can write/read/execute the file, the first 5 means anyone in the same unix group as the owner can read/execute the file and the second 5 means everyone else can read/execute it.
Lastly, we need to tell OS X that we want this script to run everytime a user logs out. This can be done with the following command (this is one long line that the browser may wrap):
sudo defaults write com.apple.loginwindow LogoutHook /Library/Scripts/Custom/SafariCacheReset.sh
Hopefully, that should be it. You can test it out by logging out of your account and logging back in. The /Users/YourUsername/Library/Caches/Safari/ should be sparkling clean now.
I've tried to be a bit verbose to explain what's going on, so it's not as complex as it may look at first.
If you have any questions at all, feel free to ask - it's better to be sure of what you are doing when it comes to the Terminal sometimes.
