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