The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Check error after delete files. icemania Shell Programming and Scripting 2 07-24-2007 08:22 PM
File System Check using shell script !!! csaha Shell Programming and Scripting 7 01-03-2006 01:36 AM
Script to check for a file, check for 2hrs. then quit mmarsh UNIX for Dummies Questions & Answers 2 09-16-2005 11:46 AM
Tidying up temp files on exit of script Bab00shka UNIX for Dummies Questions & Answers 2 09-11-2002 03:13 AM
What are core files and how can I delete them when am performing system maintenance?? IMPORTANT UNIX for Dummies Questions & Answers 6 04-04-2002 12:39 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 08-13-2008
Registered User
 

Join Date: Aug 2008
Posts: 2
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
Reply With Quote
Forum Sponsor
  #2  
Old 08-13-2008
Bughunter Extraordinaire
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,235
OK, lets go through it step by step:

Quote:
Originally Posted by Bwood1377 View Post
Code:
#!/bin/bash 
#This script will automatically delete temporary files
rm -rf /tmp/* 
rm -rf/var/tmp/*
fsck -A
exit
Code:
#! /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.

Code:
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:

Code:
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.)

Code:
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:

Code:
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
Reply With Quote
  #3  
Old 08-13-2008
Registered User
 

Join Date: Aug 2008
Posts: 2
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.
Reply With Quote
  #4  
Old 08-14-2008
broli's Avatar
Registered User
 

Join Date: Dec 2007
Location: Argentina
Posts: 198
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)
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:45 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0