Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A script to do tmp and log directory clean up UNIX
I was looking at Peter Kappesser's hint Free drive space by deleting miscellaneous excess files, but it didn't delete everything I wanted.

Read the rest of the hint to see what I came up with...

#! /bin/csh -f
#
#  tmp & log directory clean up
#
#  Settings:
#     xeq   -  0: for testing
#              1: to execute
#     old   -  how many days back are files to be deleted
#
#  Notes for "find":
#  1. -maxdepth 1 -  only looks at the current directory
#  2. -type f     -  restrict to files only
#
set xeq = 1
set old = "+7"
#
#  delete old files ending with ".gz" in /private/var/log
cd /private/var/log
echo "Checking: /private/var/log"
if ($xeq) then
  /usr/bin/find . -maxdepth 1 -type f -name "*.gz" -mtime $old -exec rm {} ;
else
  /usr/bin/find . -maxdepth 1 -type f -name "*.gz" -mtime $old -exec ls -ld {} ;
endif
#
#  delete old files from select directories
set dir_list = (/private/var/tmp /private/var/spool/cups 
                /private/var/spool/cups/tmp 
                /Library/Logs/AppleFileService 
                /Library/Logs/DirectoryService)
foreach d ($dir_list)
   echo "Checking: $d"
   cd $d
   if ($xeq) then
      /usr/bin/find . -maxdepth 1 -type f -mtime $old -exec rm {} ;
   else
      /usr/bin/find . -maxdepth 1 -type f -mtime $old -exec ls -ld {} ;
   endif
end
I've added a call to this script to /etc/weekly.local. Does anyone have suggestions for other directories that should be cleaned out on a regular basis?

    •    
  • Currently 2.50 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[12,747 views]  

A script to do tmp and log directory clean up | 4 comments | Create New Account
Click here to return to the 'A script to do tmp and log directory clean up' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A script to do tmp and log directory clean up
Authored by: jaclyn on Sep 23, '04 04:42:58PM

Couple of things I had to change to get this to run without error ...

All of the finds with execs had to be changed. I had to add a backslash before the ; to avoid a find: -exec: no terminating ";" error. For example:


 /usr/bin/find . -maxdepth 1 -type f -name "*.gz" -mtime $old -exec rm {} \;

Also ran into a too many ('s error until I added continuation backslashes to the dir_list line:


set dir_list = (/private/var/tmp /private/var/spool/cups  \
                /private/var/spool/cups/tmp   \
                /Library/Logs/AppleFileService   \
                /Library/Logs/DirectoryService)

Otherwise I think it's working. I'm running it in test mode tonight to make sure....



[ Reply to This | # ]
A script to do tmp and log directory clean up
Authored by: szedula on Sep 23, '04 04:53:48PM

Sorry about that I just didn't notice the missing "\" when I posted the hint. They were in the original script.



[ Reply to This | # ]
A script to do tmp and log directory clean up
Authored by: joshewah on Sep 23, '04 10:05:59PM

There are been a few scripts I have seen posted like this that go and remove files. I was thinking maybe it would be a better idea to use the trash command instead of rm for security issues. You could even add a one like osascript to open the trash or something so that the user can confirm before deleting.



[ Reply to This | # ]
oops
Authored by: joshewah on Sep 23, '04 10:09:14PM

I didn't notice that you set this as a weekly thing. If you are ok with the way it works I guess go at it. I was thinking it was to be run whenever space is low etc.

Guess there isn't a way to have things double checked but you could output a list of what is deleted or something to be mailed to an account on the machine for example.



[ Reply to This | # ]