Operating System - HP-UX
1832973 Members
2179 Online
110048 Solutions
New Discussion

Re: Removing files within a script.

 
SOLVED
Go to solution
fg_1
Trusted Contributor

Removing files within a script.

Hello all.

I am trying to figure out within a particular script that I am writing how to remove files that are written to 2 different log files. The files are found using the following find statements:

find / -type f -name core -exec ls > $BASEDIR/$CORELOG {} \;

find /var/adm/sa -name "sa[0-3]*" -mtime +10 -exec ls > $BASEDIR/$SARLOG {} \;

As you can see there are 2 log files here, $CORELOG and $SARLOG. So what I would like to do is write a WHILE READ statement after each of the find commands that takes each logfile and reads it's contents, then removes each of the files listed.

BTW: I know that I can accomplish this by simply adding the following to the end of each find command: -exec rm -f {} \;

But I am trying to get an idea how the WHILE READ commands would work with this.

Thank you all.

6 REPLIES 6
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Removing files within a script.

If I understand your question then it's rather simple and you don't need a -exec argument.

Something like this:
find /var/adm/sa -name 'sa[0-3]*' -mtime +10 | while read FNAME
do
echo "Filename: ${FNAME}"
# if you want to read the file contents put a 2nd loop here
cat ${FNAME} | while read X
do
echo "${X}"
done
rm ${FNAME}
done

Note that I also replaced your double quotes with single quotes.



If it ain't broke, I can fix that.
Robin Wakefield
Honored Contributor

Re: Removing files within a script.

Hi Frank,

I would try something like this:

find / -type f -name core | tee $BASEDIR/$CORELOG | xargs rm

If you simply want to remove the files contained in the logfiles after the find command:

rm `cat $BASEDIR/$CORELOG`

or if you want to use while read:

cat $BASEDIR/$CORELOG | while read file ; do
rm $file
done

Rgds, Robin
James R. Ferguson
Acclaimed Contributor

Re: Removing files within a script.

Hi Frank:

Instead of this:

cat $BASEDIR/$CORELOG | while read file
do
rm $file
done

...use:

while read file
do
rm $file
done < $BASEDIR/$CORELOG

...you eliminate the 'cat' process...

Regards!

...JRF...


...JRF...
Leif Halvarsson_2
Honored Contributor

Re: Removing files within a script.

Hi
Sure there is many ways to do this.

Below is a example how to redirect input inside a script.

# create a list of files

find ...... >$BASEDIR/$SARLOG
# Redirect input from the file.
exec <$BASEDIR/$SARLOG
while read a
do
# DO SOMETHING
done
# Redirect input from stdin.
exec <&1
rm $BASEDIR/$SARLOG

Jordan Bean
Honored Contributor

Re: Removing files within a script.

`while read` may actually be slower. Why not group the finds into a single pipe to xargs?

( find / -type f -name core; find /var/adm/sa -name 'sa[0-3]*' -mtime +10; ) | xargs rm -f

fg_1
Trusted Contributor

Re: Removing files within a script.

All of the answers here were right on in their own ways. I ended up using JRF's idea's and it works great.

Thanks to all of you, this is truly the greatest users group/forum I have ever been a part of.

Fg.