Script to Delete temp files and check file system


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to Delete temp files and check file system
# 1  
Old 08-13-2008
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
# 2  
Old 08-13-2008
OK, lets go through it step by step:

Quote:
Originally Posted by Bwood1377
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
# 3  
Old 08-13-2008
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. Smilie
# 4  
Old 08-14-2008
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)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to check a file and delete old files

Hello, I needed help with a shell script where in it checks if a file exists under a directory and also checks the age of the file and delete it if it is older than 3 weeks. thanks (10 Replies)
Discussion started by: hasn318
10 Replies

2. Shell Programming and Scripting

Backup shell script created temp files .

Hi, I've a script which creates a temp flat file for storing all business dates received on a single day from diff control files sent by source system on that day. e.g on 12th april I receive txns for business day 8,9,10,11 april. I capture this business day and append to a flat file from... (1 Reply)
Discussion started by: manojg9
1 Replies

3. Shell Programming and Scripting

need a shell script to extract the files from source file and check whether those files existonserve

Hi, I am new to shell scripting.Please help me on this.I am using solaris 10 OS and shell i am using is # echo $0 -sh My requirement is i have source file say makefile.I need to extract files with extensions (.c |.cxx |.h |.hxx |.sc) from the makefile.after doing so i need to check whether... (13 Replies)
Discussion started by: muraliinfy04
13 Replies

4. Shell Programming and Scripting

Help with a shell script to check /var file system

deleting (0 Replies)
Discussion started by: fretagi
0 Replies

5. Shell Programming and Scripting

Script to temp create files more than inode limit

HI, I am from testing background. I have a scenario of a file generation, through cronjob, on a defined path. After I fill the data as 100 % utilized, my application is generating an empty file on the defined path. # df -kh Filesystem Size Used Avail Use% Mounted on... (3 Replies)
Discussion started by: atulbassi83
3 Replies

6. Shell Programming and Scripting

how to delete files at booting of system (system startup)

hi all I have a problem how to write a shell script which delete files/folder form directory whenever system boot and copy last updated folder/file in the specified directory.pse help me ASAP. i write a script which copy files in directory.I want when system boot up using script it check whether... (1 Reply)
Discussion started by: shubhig15
1 Replies

7. Shell Programming and Scripting

Script to check file system size

Dears, the output of this command df -h | tr -s ' ' | cut -f5 -d' ' is capacity 24% 0% 0% 0% 0% 1% 0% 24% 24% 0% 93% 1% (4 Replies)
Discussion started by: xxmasrawy
4 Replies

8. Shell Programming and Scripting

perl script to check if empty files are created and delete them and run a shell script

I have a local linux machine in which the files are dumped by a remote ubuntu server. If the process in remote server has any problem then empty files are created in local machine. Is there any way using perl script to check if the empty files are being created and delete them and then run a shell... (2 Replies)
Discussion started by: hussa1n
2 Replies

9. Shell Programming and Scripting

File System Check using shell script !!!

Hi Guys I m posing this new thread as I didnt get satisfactory result after running search here. My requirement is very simple (using shell script) I need to findout what all file systems are mounted -> Then I need check the health of that file system - by disk usage & by doing a simple... (8 Replies)
Discussion started by: csaha
8 Replies

10. UNIX for Dummies Questions & Answers

Tidying up temp files on exit of script

Hi I believe there is a method to remove all temporary files when a KSH script terminates (either expectedly or unexpectedly). I think is some sort of subroutine you can create that runs when the script exits. Can anyone help me with this please? Many thanks Helen :confused: (2 Replies)
Discussion started by: Bab00shka
2 Replies
Login or Register to Ask a Question