1843569 Members
3416 Online
110219 Solutions
New Discussion

Re: Shell Scripts

 
SOLVED
Go to solution
Rajkumar_3
Regular Advisor

Shell Scripts

Hai All,

I created a directory called /apps ..In this directory so many files will be created daily..

So i need to clean up only those files which starts with the letter "X_" in the /apps

Now i want to write a shell script using environment variable...

For Eg:
-------

1)#x=/apps

2)#vi del_file.sh

If $x then

else

endif;

3)So finally this script will be called using the CRONJOB.So it will automatically calls the DEL_FILE.sh script to delete the files..

Condition:
----------
a) every file has to be deleted for every 3 days starting with the prefix " X_ ",

b) And the status has to be written to syslog file automatically using the LOGGER Command...

If i declare an environment variable like... #x=/apps
That variable " x " has should be used in the shell script to satisfy the conditions..

Can any one can provide me a scipts ,,,Its urgent please...

Thanks & Regards
Rajkumar
Oracle DBA
20 REPLIES 20
Robin Wakefield
Honored Contributor

Re: Shell Scripts

Hi Rajkumar,

Something like this should do. This accepts either an argument or, if set and exported, the variable $X.

==============================================
#!/bin/ksh

DIR=${X:-$1}

if [ -z "$DIR" ] ; then
logger "$0 : X not set, and no argument given"
exit 1
fi

if [ ! -d "$DIR" ] ; then
logger "$0 : $DIR not found"
exit 1
fi

COUNT=0

find $DIR -name "X_*" -mtime +3 | while read FILE ; do
rm $FILE && logger "$0 : $FILE deleted"
COUNT=$(expr $COUNT + 1)
done

if [ "$COUNT" -gt 0 ] ; then
logger "$0 : $COUNT files removed"
else
logger "$0 : No matching files found"
fi

exit 0
=============================================

Just create this script, chmod it, and put it into cron to run, say, at 3:00 every morning, e.g.:

0 3 * * * /usr/local/bin/del_file.sh /apps

Rgds, Robin
Varghese Mathew
Trusted Contributor

Re: Shell Scripts

Hi Rajkumar,

check out this..

find /apps -name X_* >>/tmp/filesX
a=`cat /tmp/fileX`
for a in $a
do
/usr/bin/rm $a
done

The file list which the scipts deletes remains in /tmp/fileX

Hope this helps.
Cheers !!!
Mathew
Cheers !!!
Rajkumar_3
Regular Advisor

Re: Shell Scripts

Hai Robin,

Thank you for your reply...

That means what i understood is if i declare the Environment variable X=/apps and exported it..
and then if mention this script name in the CRONJOB it will automattically it will delete the files starting with the name X_* in the /apps directory ???

Is my understanding is correct ????

Thanks & Regards
Rajkumar
Oracle DBA
Rajkumar_3
Regular Advisor

Re: Shell Scripts

Hai Matchew,

Thank you for your reply...

I think yours script will run because you have hardcoded the directory name in the script where the files has to be deleted..But what i want is The script has to collect the directory name from the Environment variable and delete the files accordingly...

Suppose the user may change the directory name then your script dosent work...

If the user wants to change the directory he will change it in the Environment variable not in the script....

So we have to give that compatibility..

Thanks & Regards
Rajkumar
Oracle DBA
Robin Wakefield
Honored Contributor

Re: Shell Scripts

Hi,

You'll have to either put the directory on the command line in cron, as shown in my previous reply, or source an external file and edit this accordingly.

So near the top of the script add something like:

. ~/.dirfile

and in this file you'll have:

X=/apps

Rgds, Robin.
Sridhar Bhaskarla
Honored Contributor

Re: Shell Scripts

Rajkumar,

I think you want to have a generic script that takes the directory (ex /apps) and cleanout the files that startwith X_.

#clean_it /apps

#!/usr/bin/ksh

if [ $# -ne 1 ]
then
echo "Usage:$0 directory"
exit 1
fi

DIR=$1

if [ !-d $DIR ]
logger "$0: No such directory"
exit 1
else
logger "$0 : `date` : started deletion"
fi

printdelete()
{
logger "$0: $1 is deleted"
}

find $DIR -name "X_*" -mtime +3 >> /tmp/list$$
NUM=`wc -l /tmp/list$$|awk '{print $1}'`
for FILE in `cat /tmp/list$$`
do
rm $FILE
printdelete $FILE
done
logger "$0: `date` : End deletion of $NUM files"

-Sri


You may be disappointed if you fail, but you are doomed if you don't try
Rajkumar_3
Regular Advisor

Re: Shell Scripts

Hai Robin,

Thank you very much.

That means i have add this " . ~/.dirfile " at the beginning of the file..

If i have to immeditely test the script whether its working or not??? How can I please.. ??

Thanks & Regards
Rajkumar
Oracle DBA
Rajkumar_3
Regular Advisor

Re: Shell Scripts

Hai Sridhar,

Can you please explain me about your script step by step??

Thanks & Regards
Rajkumar
Oracle DBA
Robin Wakefield
Honored Contributor

Re: Shell Scripts

Hi,

You could run it with an at job:

at now
your_script


Create yourself another window in which you tail the syslog:

tail -f /var/adm/syslog/syslog.log

You may want to substitute "echo" for "rm" while you are testing it, just in case.

Rgds, Robin.
Rajkumar_3
Regular Advisor

Re: Shell Scripts

Hai Robin

I created the 2 files(X_file1.dat, X_file2.dat) in the /apps directory..

I have set the environment variable X=/apps and i exported it #export X

After that i run the script its displaying an error in the syslog file saying that no matching files found..

What could be the problem???

Thanks & Regards
Rajkumar
Oracle DBA
Santosh Nair_1
Honored Contributor

Re: Shell Scripts

Rather than setting an environment variable and sourcing the profile, it would be better to run Robin's script passing the directory as one of the parameters. (Remember that cron only has the minimal environment variable setting which is why you'd have to source the profile in order to read your variable.)

So the better way to do it would be to set up the cron job as follows:

0 3 * * * /usr/local/bin/del_file.sh /apps

And of course you can test it out using at, i.e.

at now < /usr/local/bin/del_file.sh

Hope this helps.

-Santosh
Life is what's happening while you're busy making other plans
Robin Wakefield
Honored Contributor
Solution

Re: Shell Scripts

Hi,

It means it didn't find any files older than 3 days. I'm not sure if that's what you're after, but if you remove the "-mtime +3", you should see it removing them this time.

Rgds, Robin.
Sridhar Bhaskarla
Honored Contributor

Re: Shell Scripts

Raj kumar,

The first if function checks if there is any argument passed to the script (should be like clean_it /apps) otherwise will give out syntax error.

Next the argument ($1) is assigned to the variable DIR (here it is /apps)

Next if condition checks if this directory is existing or not. If not, printouts the error otherwise will start logging the deletion.

printdelete is a function that just logs into the syslog taking an argument. For ex., printdelete X_121212 will log "clean_it : X_121212 deleted"

Find command gets a list of all files taht start with X_ that haven't been modified since 3days. We take the number of such files using wc and assign it to NUM.

For loop deletes all the files in the list calling the function printdelete for each file.

At the end, we log again the completion with date and the number of files.

YOu just need to add one more step at the end

rm /tmp/list$$

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Rajkumar_3
Regular Advisor

Re: Shell Scripts

Hai Robin,

Instead of removing the -mtime +s in the script is there any thing that we can create files using
" touch " command using exactly 3 days before date to delete the files now??

Thanks & Regards
Rajkumar
Oracle DBA
Rajkumar_3
Regular Advisor

Re: Shell Scripts

Hai Robin,

I am able to run your script successfully..I eliminated the -mtime +3 and also created the files in the /apps directory using the touch command..With this command i created 10 files on 12th date.. Now Its working fine..

Thank you very much Robin...

Thank you very much all of you for helping me...

Thanking you

Regards
Rajkumar
Oracle DBA
Rajkumar_3
Regular Advisor

Re: Shell Scripts

Hai all,

I have posted a old query with the new requirement please review..

Thanking you

Rajkumar
Oracle DBA
John Flanagan
Regular Advisor

Re: Shell Scripts

I see a lot of people using a loop to delete files. Would it not be easier to pipe the output to 'xargs rm'

find /app -name x_* |xargs rm
Rajkumar_3
Regular Advisor

Re: Shell Scripts

Hai

here we are using " -mtime +3 " to delete the file for every 3 days..
suppose If i want to delete the files "OLDER THAN 3 DAYS"

what could be the option ???

Thanks & Regards
Rajkumar
Oracle DBA
Robin Wakefield
Honored Contributor

Re: Shell Scripts

Hi,

"-mtime +3" DOES mean delete if older than 3 days!! Are you saying that you want cron to run your script every 3 days?

Rgds, Robin.
Rajkumar_3
Regular Advisor

Re: Shell Scripts

Yes Robin,

You are correct.It is for every 3 days..I have another scripts which has to be read from the oracle tables...I will post a new query soon..

Thank you all for helping me..

Thanks
Rajkumar
Oracle DBA