Operating System - HP-UX
1748182 Members
3395 Online
108759 Solutions
New Discussion

Re: Finding and deleting sub-directories by date

 
TheJuiceman
Super Advisor

Finding and deleting sub-directories by date

I am needing to do a find command and look for all instances of a directory called DIR.  I then want to delete any sub-directories in the found DIR directories older than a month.  Some of the DIR directories may not have any sub-directories in them. 

 

How can I construct a find/rm script to successfully find these older subdirectories and remove them without trying to delete the parent (ie. "rm -r ." or "rm -r ..").  Some of the parent DIR directories may also be older than a month, but I do not want to delete the DIR directory. 

 

There does not seem to be a maxdepth and mindepth equivalent available.

 

Is there a "clean" way to do this without getting the occasional error or having to cd into the parent DIR to prevent accidental deletion?  Thanks

10 REPLIES 10
Steven Schweda
Honored Contributor

Re: Finding and deleting sub-directories by date

> [...] I then want to delete any sub-directories in the found DIR
> directories older than a month. [...]

   Do you want to delete only old subdirectories in DIR itself
("DIR/fred"), or all old subdirectories anywhere under DIR ("DIR/fred",
"DIR/a/fred", "DIR/a/b/fred", ...)?

> How can I construct a find/rm script to successfully find these older
> subdirectories and remove them without trying to delete the parent
> (ie. "rm -r ." or "rm -r ..").

   I'm confused.  When you identify an old subdirectory, then you should
be able to delete that subdirectory ("rm -r path/to/subdir").  Where's
the risk of deleting its parent?

> There does not seem to be a maxdepth and mindepth equivalent
> available.

   I assume that you're running some version of HP-UX, and that its
"find" lacks some of the features of GNU "find".  If you really want the
features of GNU "find", then why not install (and use) GNU "find"?

 

> Is there a "clean" way to do this without getting the occasional error
> or having to cd into the parent DIR to prevent accidental deletion?

   What kind of "occasional error" do you expect (or see)?  What's the
problem with doing a "cd path/to/DIR" (if it might be helpful)?

   I might do this in two pieces.  First, a "find" command to identify
all the "DIR" directories.  Perhaps something like:

      find dir -type d -name DIR -exec script2.sh {} \;

And then have script2.sh look for old subdirectories (in its "$1"
directory), and do whatever you want done with them (probably using
another "find [...] -exec [...]" command.

   It's difficult to debug a script (or set of scripts) which exists
only in your head.  Write something, and _then_ complain about how it
works.  While experimenting, I'd replace (or prefix) all "rm" commands
with "echo" commands, so that you can see what the script(s) would do,
with less risk of permanent damage.

Dennis Handly
Acclaimed Contributor

Re: Finding and deleting sub-directories by date

>sub-directories in the found DIR directories older than a month.

 

You don't care how new the files in that directory are?

Or are the files written only once and that's enough to set the modify time on the directory?

 

>Some of the parent DIR directories may also be older than a month, but I do not want to delete DIR. 

 

This is where a clever use of -prune will help.

 

start with:

find $DIR -type d -mtime +60

 

And you have to be careful if multiple levels of subdirectories since the removing will change the date on the parent.  -depth may help here.

Did you want to remove all subdirectories even if newer than the intermediate directories?

TheJuiceman
Super Advisor

Re: Finding and deleting sub-directories by date

Hello everyone,

 

Thank you for your reply.  Let me see if I can clear up the water just a bit....

 

Various directories exist on the system for archiving information.  Though they all have the same name, they may reside at different layers  within each filesystem.  These all have the same name (I used DIR before).  So on the system, we have something like this....

 

/alpha/subdir/subdir/DIR

/bravo/subdir/DIR

/charlie/subdir/subdir/subdir/subdir/DIR

/charlie/subdir/subdir/DIR

 

Underneath each DIR, archive information is written.  These are created as subdirectories under DIR, so you may have something like this....

 

/alpha/subdir/subdir/DIR/archive-directory-from-today

/alpha/subdir/subdir/DIR/archive-directory-from-yesterday

/alpha/subdir/subdir/DIR/archive-directory-from-a-day-last-week

/bravo/subdir/DIR/archive-directory-from-a-day-last-month

...etc

 

Although all of the DIR directories have a consistent name, the subdirectories under them can be named various things.  The goal is to delete all of the subdirectories under each DIR that are older than a certain number of days.

 

The current method is to find all the instances of DIR and create a temp file.  Then it is using a while/do/done loop to go through the list of directories in the temp file, cd into them, and then do a "find . -mtime +95 -print -exec rm -r + " for example.

 

What I am needing to do is purge the subdirectories under the DIR directories without deleting the DIR directories.  What I am finding is that the rm statement will attempt to delete DIR if it meets the parameters for the -mtime.  If I cd into the DIR directory prior to the rm statement, it will not delete the DIR directory (since it is in it), but I will get a rm error saying it could not delete it  (rm:  cannot remove .. or . ) 

 

This is an old script that was written a long time ago that needs to be cleaned up.  My goal is I would like to make it not have to cd into the directory and get the occassional rm error back.

 

Thank you, everyone, for your help.

Steven Schweda
Honored Contributor

Re: Finding and deleting sub-directories by date

> [...] "find . -mtime +95 -print -exec rm -r + "

> If I cd into the DIR directory prior to the rm statement, it will not
> delete the DIR directory (since it is in it), but I will get a rm
> error saying it could not delete it  (rm:  cannot remove .. or . )

   If "." is the biggest problem, then consider:

mba$  find . -exec echo {} \;
.
./test2
./test2/fred

mba$  find . '!' -name . -exec echo {} \;
./test2
./test2/fred

   (At least "find" doesn't return "..", too.)

Steven Schweda
Honored Contributor

Re: Finding and deleting sub-directories by date

> The current method is to find all the instances of DIR and create a
> temp file.  Then it is using a while/do/done loop to go through the
> list of directories in the temp file, [...]

   I can't see your script, so I know othing, but I'd probably try to
avoid such a temporary file.  Instead, I'd look for a way to feed the
results from the DIR search into the second phase.  That could be done
using multiple scripts (as suggested above) or a plain-old pipeline in a
single script.

   Writing a bunch of data to a file only to read them back out again is
often not the best method.

TheJuiceman
Super Advisor

Re: Finding and deleting sub-directories by date

find / -type d -name DIR > /tmp/DIR.tmp

 

while read TMP

do

cd $TMP

find . -mtime +95 -print rm -r {} \;

done < /tmp/DIR.tmp

 

 

Steven Schweda
Honored Contributor

Re: Finding and deleting sub-directories by date

> find / -type d -name DIR > /tmp/DIR.tmp
>
> while read TMP
> do
> [...]
> done < /tmp/DIR.tmp

find / -type d -name DIR | \
(
    while read TMP ; do
    [...]
    done
)

 

   Sub-shell parentheses can be very helpful.

TheJuiceman
Super Advisor

Re: Finding and deleting sub-directories by date

Hi Steven,

 

I appreciate your help.  That does make it cleaner as it eliminates the tmp file.

 

However, the original problem remains.  If I plug in the find command to do a rm -r, and the DIR directory is older than the -mtime, it will delete the DIR directory as well.  I just tried this out and it did exactly that.

 

find / -type d -name DIR |\

(

   while read TMP; do

         find $TMP -depth -mtime +95 -print -exec rm -r +

   done

)

 

This will find the DIR directories and delete everything older than 95 days.  However, if the DIR directory ITSELF is older than 95 days, it gets deleted as well.

 

Is there a way to prevent this from happening?  Thank you again!!!

Steven Schweda
Honored Contributor

Re: Finding and deleting sub-directories by date

> Is there a way to prevent this from happening?  Thank you again!!!

   I'll try again.

> [...] "find . -mtime +95 -print -exec rm -r + "

> If I cd into the DIR directory prior to the rm statement, it will not
> delete the DIR directory (since it is in it), but I will get a rm
> error saying it could not delete it  (rm:  cannot remove .. or . )

   If "." is the biggest problem, then consider:

mba$  find . -exec echo {} \;
.
./test2
./test2/fred

mba$  find . '!' -name . -exec echo {} \;
./test2
./test2/fred

 

   Does adding "'!' -name ." not work?