About | Buy | Forum | Industry Watch | Learning Curve | Products | Search | Twitter | Xnews
Home » Learning Curve

A Leopard Cleanup Script

Or two. A first look at why 10.5 deserves special attention.


Buy It

Try It

If there's one thing that makes serious users despondent it's seeing people buying into the 'system maintenance utility' scheme - purchasing software that does nothing but wrap Unix commands you already have on your hard drive - commands you've already paid for.

As if before these utilities came along those poor engineers at Bell Labs and Berkeley had no way to clean and optimise their systems, poor things.

CLIX was placed in the middle to ameliorate that.

[Bob Rudis of The Apple Blog has a recent article on how you use CLIX for things like this - and a lot else too. Ed.]

10.5 Leopard represents a step forward - but it also represents new challenges to those interested in keeping their machines clean lean and mean. New caches are strewn all over the place; these caches can make ordinary operations run faster and smoother; but there can be times when you simply want them gone - when the system doesn't take adequate care of itself.

/private/var/folders

A new location for system and user caches is found at /private/var/folders. Leopard 10.5 systems should have two subdirectories at this point; and their names are installation dependent - they'll vary from machine to machine. Two levels further down the hierarchy are represented by one two-character directory and below that a second directory with a name 27 characters or more in length. An attempt seems to have been made to thwart entrance from the command line but of course nothing's impossible - the names namely contain a lot of 'weird' characters that often conflict with Unix shell operations.

The first subdirectory under /private/var/folders is for user caches; the second for system caches. The commands that follow won't touch the system caches (for obvious reasons).

The three user cache subdirectories with actual contents are called '-Caches-', '-Tmp-', and 'TemporaryFiles'. The system caches have the same layout but with six separate 'trees' for the same number system accounts. The system accounts are for _atsserver, _installer, _mdnsresponder, nobody, root, and _securityagent.

What Builds Up

The contents of the user '-Cache-' hive can grow to monstrous proportions. Safari caches can grow to several hundred MB. Other caches will appear and you'll have no idea how they got there. Leopard seems to love to cache stuff all over the place. And if you're particular on watching your disk free space this can throw you for a loop. When that happens: look first inside /private/var/folders.

Apple's Preview leaves heaps of junk in '-Tmp-': a directory created and left behind for every file you open.

~/Library/Caches

Already from the Tiger era ~/Library/Caches could display fantastic behaviour: position yourself there so you're watching what happens and then just open and close 'System Preferences'. You should see two files immediately appear - eating up a good 5-6 MB disk space. And all you did was open and close the program.

$ ls -ilOR com.apple.pref*
4302965 -rw-r--r--   1 - 1695765 Aug 10 09:13 com.apple.preferencepanes.cache
4302966 -rw-r--r--   1 -   79872 Aug 10 09:13 com.apple.preferencepanes.searchindexcache

You might feel they're good to keep; you can hope they won't mutate with time; you might on the other hand try removing them and running 'System Preferences' again to see things still work OK. [Which they definitely do.] So you might decide you can live with these two files gone as well. [Good decision.]

~/Library/Caches/Software Update

Be sure to check out '~/Library/Caches/Software Update' as well - this type of cache resembles what Safari traditionally stores and can grow to several MB with no apparent benefit to you.

Remember: your system can live without caches. But caches can often speed up computer operations - including your login. Leopard is blazing fast at logins - removing too many caches in the '/private/var/folders' hive can slow logins down. The logins will still work OK - they'll just be slower: the system will want to recreate these caches before turning the controls over to you.

Remember: not all caching systems are well thought out. Software can have a tremendous propensity for literally littering your system with totally useless junk. And if you want a clean lean and mean machine you'll have to take care of this yourself.

グラフィックトライブへ

Dali Rău of Graphic Tribe runs 4-5 Leopard boxes in tandem. [He needs this power for graphics rendering - click here and click here. Ed.] Some of the software used by Graphic Tribe really gets ambitious with caching and Leopard helps them along fabulously. It is therefore important to clean up the hardware on a regular basis if they're to be used for this type of crunching again.

What follow are some of the cleanup scripts used by Graphic Tribe. They're used mostly in conjunction with rebooting - some run before the reboot, some run afterwards. As always: 'YMMV'. They're yours to try - but only 'as is'.

beforeReboot.sh

This is the most important part of the cleanup. It's about getting a box ready to use (abuse) again. This script will automatically reboot your computer when the cleaning's done - so be sure to close down all other operations before you begin.

The script assumes the presence of xabatch to remove unwanted extended attributes. Comment out the appropriate lines if you don't have it.

[Note: this script will not run 'as is': line breaks have been introduced for readability. Click on the code itself to download a functional copy. Ed.]

#!/bin/bash

# modify the paths to match the location of your own script and xabatch file
# protect this script and the xabatch file (this can be done at the end of the
# script as well, right before the reboot command)

chown 0:80 ~/Library/Scripts/Unix/beforeReboot.sh
chmod 0550 ~/Library/Scripts/Unix/beforeReboot.sh
chown 0:80 ~/Library/Scripts/Unix/apple.XAs.xabatch
chmod 0440 ~/Library/Scripts/Unix/apple.XAs.xabatch

# this can be omitted if you don't need verbose feedback
DATE=`date '+%Y.%m.%d at %H:%M:%S.'`; say 'Cleansing started on'; say $DATE

# all 'say' lines can be removed if you don't need verbose feedback
# or you could just use the silent way if you get tired of the gimmick
# add or remove items as you see fit

say 'Step 1 of 7: Removing caches and logs...'

rm -rf /Library/Caches/* /System/Library/Caches/* /Library/Logs/* ~/Library/Caches/* ~/Library/Logs/* ↩
/private/var/log/*.log.* /private/var/log/*.log.*.bz2 /private/var/log/cups/* /private/var/log/fax/* ↩
/private/var/log/ppp/* /private/var/log/sa/* /private/var/log/samba/*

# do '/private/var/folders/*/*/-Caches-;ls -aFGhilORSTvW' and look for 'Cache.db';
# if the file size bothers you, add it to the following items
# Safari is a must, it grows really big in no time

rm -rf /private/var/folders/*/*/-Caches-/com.apple.ATS/* ↩
/private/var/folders/*/*/-Caches-/com.apple.Dictionary/* ↩
/private/var/folders/*/*/-Caches-/com.apple.dashboard.client/* ↩
/private/var/folders/*/*/-Caches-/com.apple.mail/* ↩
/private/var/folders/*/*/-Caches-/com.apple.Safari/* ↩
/private/var/folders/*/*/-Caches-/com.apple.Xcode.501/*

# these will be rebuilt immediately after reboot but it's probably a good idea to keep them on a leash
rm -rf /private/var/folders/*/*/-Caches-/com.apple.desktoppicture.* ↩
/private/var/folders/*/*/-Caches-/com.apple.dock.iconcache

say 'Step 1 completed. 6 to go.'
say 'Step 2 of 7: Emptying some logs inside /private/var/log/...'

# do '/private/var/logs; ls -aFGhilORSTvW' to see which items you have;
# add or remove items as needed

cp /dev/null /private/var/log/alf.log
cp /dev/null /private/var/log/apache2/error_log
cp /dev/null /private/var/log/apache2/access_log
cp /dev/null /private/var/log/appfirewall.log
cp /dev/null /private/var/log/asl.db
cp /dev/null /private/var/log/daily.out
cp /dev/null /private/var/log/fsck_hfs.log
cp /dev/null /private/var/log/hdiejectd.log
cp /dev/null /private/var/log/install.log
cp /dev/null /private/var/log/ipfw.log
cp /dev/null /private/var/log/mb.log
cp /dev/null /private/var/log/monthly.out
cp /dev/null /private/var/log/secure.log
cp /dev/null /private/var/log/system.log
cp /dev/null /private/var/log/weekly.out
cp /dev/null /private/var/log/windowserver.log
cp /dev/null /private/var/log/windowserver_last.log

say 'Step 2 completed. 5 to go.'
say 'Step 3 of 7: Emptying some junk inside the home directory...'

# do 'cd ~; ls -aFGhilOSTvW' and see what's in there;
# if you use Gimp you'll have things like '.recently-used.xbel' that should be kept on a leash

cp /dev/null ~/.bash_history; rm -rf ~/.thumbnails/*

say 'Step 3 completed. 4 to go.'
say 'Step 4 of 7: Removing useless localizations and designable.nibs...'

find /Applications ~/Applications /Library ~/Library ↩
\( -iname '*.lproj' -and \! \( -iname 'en*' \) -and \! ↩
\( -path '/Library/Receipts/*' -or -path '~/Library/Receipts/*' \) \) ↩
-print0 | xargs -0 rm -frv {}

# look at the example below for ways to keep other localizations you need to keep

# find /Applications ~/Applications /Library ~/Library ↩
# \( -iname '*.lproj' -and \! \( -iname 'en*' -or -iname 'ja*' \) -and \! ↩
# \( -path '/Library/Receipts/*' -or -path '~/Library/Receipts/*' \) \) -print0 | xargs -0 rm -frv {}

# if you never repair permissions, you don't need to exclude /Library/Receipts and ~/Library/Receipts
# if you don't need verbose feedback, you can omit the -v argument for rm

find /Applications ~/Applications /Library ~/Library \( -name 'designable.nib' -and \! ↩
\( -path '/Library/Receipts/*' -or -path '~/Library/Receipts/*' \) \) -type f -print0 | ↩
xargs -0 rm -frv {}

say 'Step 4 completed. 3 to go.'
say 'Step 5 of 7: Removing .DS_Store and .localized files...'

# change '~' to '/' if you want a more thorough (but time consuming) sweep

find ~ \( -name '.DS_Store' -or -name '.localized' \) -print0 | xargs -0 rm -frv {}

say 'Step 5 completed. 2 to go.'
say 'Step 6 of 7: Removing Desktop DB and Desktop DF...'

rm -f /Desktop\ DB /Desktop\ DF

say 'Step 6 completed. 1 to go.'
say 'Step 7 of 7: Removing extended attributes in the home directory...'

# modify the paths to match the location of the xabatch script (usually in ACP/etc) and xabatch file

# more info about xabatch at:
# http://rixstep.com/1/20071224,00.shtml
# http://rixstep.com/2/20080105,00.shtml

find ~ -print0 | xargs -0 ~/Applications/ACP/etc/xabatch ~/Library/Scripts/Unix/apple.XAs.xabatch {}

say 'Step 7 completed.'
DATE=`date '+%Y.%m.%d %H:%M:%S.'`; say 'Done. Cleansing completed on '; say $DATE

# Preparing to reboot

say 'Preparing to reboot. Fasten your seat belt and put your seat in an upright position.'
say 'The captain has turned on the no smoking sign.'

# if you prefer to protect the script and the xabatch file at the end,
# put the first 4 lines of the script here

reboot

afterReboot.sh

This is a much easier procedure. It checks the integrity of preference files and runs the periodicals.

#!/bin/bash

# modify the paths to match the location of your own script
# protect this script (this can be done at the end of the script)
chown 0:80 ~/Library/Scripts/Unix/afterReboot.sh
chmod 0550 ~/Library/Scripts/Unix/afterReboot.sh

# this can be omitted if you don't need verbose feedback
echo -n 'Post-reboot maintenance started on '
date '+%Y.%m.%d at %H:%M:%S.'

echo 'Step 1 of 2: Checking syntax of preference lists...'
plutil -s /Library/Preferences/*.plist ~/Library/Preferences/*.plist
echo 'Step 1 completed. 2 to go.'

# this can be omitted if your computer never sleeps during the night
echo 'Step 2 of 2: Running periodic scripts...'
periodic daily weekly monthly
echo 'Step 2 completed.'

# this can be omitted if you don't need verbose feedback
echo -n 'Done. Post-reboot maintenance completed on '
date '+%Y.%m.%d %H:%M:%S.'

# if you prefer to protect this script at the end, put the first 2 lines of this script here

Scripts & sudo

Both of the above scripts need to be invoked with sudo to run correctly.

Caution is always advised when invoking sudo on scripts. Their contents have to be adequately protected. Your system has an option to allow SUID bits on scripts but it's normally turned off for the same reason. There can be occasions such as the above scripts when using sudo can't easily be avoided; just be careful.

And Now with CLIX!

Of course you can reduce all of this to a few mouse clicks. That's the point! And it's free too. That's another good point!

You already have the associated CLIX file if you clicked on the snippets and got the download; otherwise click here now.

See Also
Download: A Leopard Cleanup Script
Learning Curve: 無駄な膨張と闘う (Bloat Warfare)

About | Buy | Forum | Industry Watch | Learning Curve | Products | Search | Twitter | Xnews
Copyright © Rixstep. All rights reserved.