Operating System - HP-UX
1752866 Members
4679 Online
108791 Solutions
New Discussion юеВ

Re: Shell scripts help - find deletes the base dir.

 
Gulam Mohiuddin
Regular Advisor

Shell scripts help - find deletes the base dir.

My following scripts works but also removes the base directory ├в log_output├в which I don't want to remove. How to rectify this script so it should not delete the log_output directory.

#!/usr/bin/sh
nod=60
dir1=/psoft/prcs/log_output

find $dir1 -mtime +$nod -name "_PSDSTSRVLOG" -prune -o -name "_PSPRCSRVLOG" -prune -o -name "_TUXLOG" -prune -o -print | tee /psoft/log_fstrn8.txt | xargs rm -rf


Thanks,

Gulam.
Everyday Learning.
9 REPLIES 9
Patrick Wallek
Honored Contributor

Re: Shell scripts help - find deletes the base dir.

Are you just wanting to delete regular files? If so, just add a '-type f' to your find to search only files.
Gulam Mohiuddin
Regular Advisor

Re: Shell scripts help - find deletes the base dir.

No, there are lots of other subdir which I would also like to remove.

Thanks,

Gulam.
Everyday Learning.
inventsekar_1
Respected Contributor

Re: Shell scripts help - find deletes the base dir.

i dont know how to help you, but i think, why dont you create that directory at the end of the script?
Be Tomorrow, Today.
Joseph C. Denman
Honored Contributor

Re: Shell scripts help - find deletes the base dir.

Use a /. after the $dir1

find $dir1/. -mtime ..........


you will get an error stating rm: cannot remove .. or ., but all below $dir will be gone.

...jcd...
If I had only read the instructions first??
Peter Nikitka
Honored Contributor

Re: Shell scripts help - find deletes the base dir.

Hi Gulam,

you can explicitly skip it:

...
find $dir1 -mtime +$nod -name "_PSDSTSRVLOG" -prune -o -name "_PSPRCSRVLOG" -prune -o -name "_TUXLOG" -prune -o -print | tee /psoft/log_fstrn8.txt |
fgrep -vx $dir1 | xargs rm -rf

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Gulam Mohiuddin
Regular Advisor

Re: Shell scripts help - find deletes the base dir.

Hi,

If I changed it to $dir/. then it deltes all files/subdir under log_output just keepling following two pruned subdir

total 108
drwxr-x--- 2 fstrn8 psoft 39936 Jun 7 05:22 _PSDSTSRVLOG
drwxr-x--- 2 fstrn8 psoft 15360 Jun 7 12:34 _PSPRCSRVLOG


Thanks,

Gulam.
Everyday Learning.
OldSchool
Honored Contributor

Re: Shell scripts help - find deletes the base dir.

... or, you can

cd $dir1

find . -mtime +$nod -name "_PSDSTSRVLOG" -prune -o -name "_PSPRCSRVLOG" -prune -o -name "_TUXLOG" -prune -o -print | tee /psoft/log_fstrn8.txt | xargs rm -rf
Bill Hassell
Honored Contributor

Re: Shell scripts help - find deletes the base dir.

Or the simplest:

find ${dir1}/* ...rest of options...

${dir1}/* is replaced with a list of all the items in $dir1 but not the directory itself.


Bill Hassell, sysadmin
Peter Godron
Honored Contributor

Re: Shell scripts help - find deletes the base dir.

Gulam,
do a 'touch $dir1' before you start.