Link to home
Start Free TrialLog in
Avatar of 16582
16582

asked on

Creating a script to delete multiple files in multiple dirs.

Currently I have an HP-UX 11i (11.23).  I'm running the command below to remove files / directories older than today, the problem I'm having is creating a script to execute in multiple directories.

root@bhux1#  find /ds1/in_dcm -type f -mtime +1 -exe rm -f {} \;

The command runs great, the only issue I have is, I must change the directory in the command each time it runs.  Is there  (1) any way do give it a block of directories to remove? (i.e. /ds1/in_dcm is shown but I have ds1 - ds28 and not all are online all the time -- in other words ds1 -ds7 may be online ds8 - ds12 offline and the rest online.)

What I would like to do is have a command or script to move thru all of the directories without having to manually change.  That way I can add it to the crontab for a weekly run.

Thanks
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image


You can wildcard the path name

find /ds*/in_dcm .....

That's pretty dangerous to put into a crontab file.  You might just put 1 line per path.


Kent
Avatar of 16582
16582

ASKER

I'm not really a big fan of using the wildcard option.....  I would prefer to have a script that I can run manually but, add a line to run that script weekly in the crontab.

Sure...  Then just put the desired commands in a script file.

find /ds1/in_dcm -type f -mtime +1 -exe rm -f {} \;
find /ds2/in_dcm -type f -mtime +1 -exe rm -f {} \;
find /ds3/in_dcm -type f -mtime +1 -exe rm -f {} \;
find /ds4/in_dcm -type f -mtime +1 -exe rm -f {} \;
...
find /ds28/in_dcm -type f -mtime +1 -exe rm -f {} \;

You might want to call them so that you can ignore any returned error values.

Kent

Avatar of 16582

ASKER

Call them?  I'm not sure I understand...  Small system...  Not a UNIX guru....  not great at scripting (dangerous but not a complete waste).

Well, it depends a little bit on how you want to handle things.

If the path isn't mounted, you probably want to ignore the error.  What about when the call to rm(1) fails?

find returns 0 if it processes every file that is returned.  If no files are returned (path not found) it shouldn't return an error.

I'd start by putting all of the find commands in a script file and registering it with cron to run at a time of your choosing.  If it fails, deal with it then.


Kent
Avatar of 16582

ASKER

Yes, I agree but to avoid that, how can I get the script to continue on, ignoring the /ds directories that aren't online?  As the cron is running under root, it would send a slew of messages to root mail with the errors that aren't necessarily errors.
ASKER CERTIFIED SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 16582

ASKER

Thanks Kent