Script to Delete temp files and check file system

Hi all, new to the threads as well as Unix/Linux. I need to create a script that will delete any temporary files as well as check the files on the system for errors or corruption. This is what I have so far and I'm sure that I'm missing things or have the wrong commands. I'm not sure where to go next or how to incorporate the two. Any help would be much appreciated. Thanks in advance.

#!/bin/bash
#This script will automatically delete temporary files
rm -rf /tmp/*
rm -rf/var/tmp/*
fsck -A
exit

OK, lets go through it step by step:

#! /bin/bash

You haven't told us which system you are working on. If it is Linux, then this line is ok, as bash is Linux' default shell. Otherwise (if you are working on AIX, HP-UX, SunOS, ...) it might be a bad idea, even if you use bash as a command shell. When writing scripts it is better to use the systems default shell. When writing scripts assume as little as possible and that a specific shell other than the default shell is installed is such an assumption.

rm -rf /tmp/* 
rm -rf/var/tmp/*

These two lines delete everything in the two directories - directories, links, files, whatever. And the things get deleted regardless of being in use or not. Ask yourself if this is really what you want to do. It might be more sensible to delete just the files and only the ones which haven't been accessed for - say - 48 hours. Is this possible? Yes, it is: use the "find" command for that:

find /tmp -type f -atime +2 -exec rm -f {} \;

(The second command is constructed analogous to that, consult the manpage for "find" to get acquainted with one of the most powerful Unix utilities there are.)

fsck -A

I do not think that running fsck is really necessary. Most Unix/Linux filesystems are pretty robust and need fsck-ing only under unusual circumstances (loss of power or the like). In your place i would skip that. What would be a good idea though would be a "df" to check if the FS is full or near full. This is a much more common problem. you might want to set the exit code accordingly:

if [ $(df -k /tmp|<some-filter>) -le <some-value> ] ; then
     exit 1
else
     exit 0
fi

<some-filter> is to be replaced by some text-filter, because the output of "df -k /tmp" is not a single number. Use sed/awk/grep/etc. to filter the number of free kbytes out of the complete output. Alas i have no Unix machine at hand writing this so yo will have to work out the details yourself. As all this is meant as a thought-provoking impulse and not a full-featured solution i think this is tolerable.

I hope this helps.

bakunin

To answer your first question it's a Linux system. Thanks for your help, you're description made it a lot easier to understand what I was really trying to do. I really appreciate it, bakunin. :b:

if its linux, there is alerady some scripts that delete what CAN be deleted from /tmp at boot time, (or when the system is going down)