Operating System - HP-UX
1830938 Members
1968 Online
110017 Solutions
New Discussion

Re: HP-UNIX 11.0 - script

 
SOLVED
Go to solution
Gerard_10
Occasional Advisor

HP-UNIX 11.0 - script

Hi,

I am wondering could anyone help me on my first script which has to delete files which have certain information in them??
12 REPLIES 12
John Poff
Honored Contributor

Re: HP-UNIX 11.0 - script

Hi,

You've come to the right place. Tell us about the files and what kind of information they have that you would be looking for.

JP
James Odak
Valued Contributor

Re: HP-UNIX 11.0 - script

assuming you mean all files in a directory

for x in `grep -l "string" /path/* `
do
echo "removeing file $x" > logfile
rm $x
done

make sure the string is very specific as you do not want to delete files by mistake
also add anything you want between the do and done
such as moving the files instead of deleteing them ect.
Ian Dennison_1
Honored Contributor

Re: HP-UNIX 11.0 - script

cd /directory_to_search

for i in $(grep -l "TEXT" [FILE TEMPLATE])
do
rm $i
done

I recommend you try this with an 'echo $i' first instead of an 'rm $i'.

Share and Enjoy! Ian
Building a dumber user
Rodney Hills
Honored Contributor

Re: HP-UNIX 11.0 - script

Their are several "scripting" languages (ksh, csh, awk, perl, python, etc) available within any unix, including HP-UX.

For example, to delete files under a folder that are older than 7 days, you would-

find /the/folder -mtime +7 -exec rm {} \;

If you wanted to delete specific files that contain a certain text you would-

cd /the/folder
grep -l "yourtext" * | xargs rm

You must be careful when removing files though. It is easy to remove files you didn't mean to remove.

I like to run my scripts with any remove commands replaced with "echo". That way I can test the command and see the list of files it WOULD delete before I actually delete them.

HTH

-- Rod Hills
There be dragons...
Bill McNAMARA_1
Honored Contributor

Re: HP-UNIX 11.0 - script

I recommend you change the rm to mv and put it into a directory that you can examine later!

You never know the text hello might be found in a binary file!

Later,
Bill
It works for me (tm)
Rodney Hills
Honored Contributor

Re: HP-UNIX 11.0 - script

I just thought of a little anecdote I would like to share. Many years ago we had a workstation with a CAD application that tended to create a lot of log files. So a nightly cron job was created to delete anything older than 14 days. The commands went something like this-

cd /the/cadapp/log
find . -mtime +14 -exec rm -f {} \;

Well, for many months this job ran quietly doing its task without much notice.

Then, one day, all of a sudden hundreds of files started disappearing!

It didn't take long to determine that that small cleanup job was the culprit.

It turned out that the folder "/the/cadapp/log" did not exist any more. The "cd" failed but the "find" didn't and went on it merry way starting at "/" and proceeding to delete everything over 14 days old.

Moral of the Story-
In a script, never assume that each statement was successful.

The correct job should have been-
if cd /the/cadapp/log ; then
find . -mtime +14 -exec rm -f {} \;
else
echo "/the/cadapp/log is missing" | mail me@here.com
fi

HTH

-- Rod Hills
There be dragons...
James Odak
Valued Contributor

Re: HP-UNIX 11.0 - script

to rodney

that is why i fully path everything i script
i would of done this

find /the/cadapp/log -mtime +14 -exec rm -f {} \;

Jim
Gerard_10
Occasional Advisor

Re: HP-UNIX 11.0 - script

Hi,

Thanks for the replys here is my script, however it is deleting all files beginning with tpsmppmux, and not those files with the specific line detailed.

Any ideas?

#!/bin/sh
cd /maint/debug
for x in 'grep -l "[ipm_disconnect call failed.]" tpsmppmux*'
do
rm $x
done

the following is outputted when i run the script -

./delete_tpsmpp.sh
rm: grep non-existent
rm: -l non-existent
rm: "[ipm_disconnect non-existent
rm: call non-existent
rm: failed.]" non-existent



Rodney Hills
Honored Contributor
Solution

Re: HP-UNIX 11.0 - script

Those look like forward single quotes (') around grep. They should be back quotes (`)

-- Rod Hills
There be dragons...
Jose Mosquera
Honored Contributor

Re: HP-UNIX 11.0 - script

Hi,

First locate files that contain a specific string:
find -type f -exec grep -l '' \;

So, you can assign this find result to a variable to recycle it and then remove these files, i.e:

FILES_TO_REMOVE=`find -type f -exec grep -l '' \;`
for FILE in $FILES_TO_REMOVE
do
rm $FILE
done

Be careful with **rm** command, pls try with other inoffensive command (i.e: cat, ls, etc) to check that obtains the expected results, once confirmed it uses the **rm** command.

Rgds.
john korterman
Honored Contributor

Re: HP-UNIX 11.0 - script

Hi Gerard,
as Rodney Hill pointed out, you have to back-qoute the grep for the text string. However, you also have to get rid of the brackets; that is if you want to find the files containing the full string - the bracket mean "any character mentioned", i.e. if a file contains just a single of the chars mentionend in the brackets, it will be included (probably not what you want).
You could make your script like this:

#!/bin/sh
cd /maint/debug
for x in `grep -l "ipm_disconnect call failed." tpsmppmux*`
do
#rm $x
ls -l $x
done

As Jose pointed out: please try first with something less offensive than the rm command.

regards,
John K.
it would be nice if you always got a second chance
john korterman
Honored Contributor

Re: HP-UNIX 11.0 - script

Hi again,
I'm sorry - In my previos mail I could not imagine that the brackets could be part of the text string to search for. It just happens that the brackets are special characters to the shell, having the regular expression function in connection with certain commands as e.g. grep, as described in the previous mail. If the brackets are to be taken literally, you need to put a backslash in front of each of them, e.g.:

"\[ipm_disconnect call failed.\]"


regards,
John K.
it would be nice if you always got a second chance