Operating System - HP-UX
1832161 Members
10837 Online
110038 Solutions
New Discussion

How to do this find command?

 
SOLVED
Go to solution
Angie_1
Regular Advisor

How to do this find command?

I need a find command that will do the following:

-I want it to /test/aoi/inspire/defects/812165000/E02 and look for any directories under here that are more than 30 days old and if it finds them, prompts for a delete.

I can do it for files, not for directories.

Please help soon, I need it today.

Thx..Angie
20 REPLIES 20
Angie_1
Regular Advisor

Re: How to do this find command?

I noticed I needed to clarify more.

There are many many 812* directories. So I need to be able to go down into the E02 and E03..E04.. E05..E06 directory, check under each of those and find if there is a directory more than 30 days old. If it then finds it, prompt for delete.

So I need to go through lots of 812* directories and lower to do the check.

Thx..Angie
Pete Randall
Outstanding Contributor
Solution

Re: How to do this find command?

Hi Angie,

Have you tried something like this:

find /test/aoi/inspire/defects/ -type d -name '812*' -exec 'rm -i' {} \;


Pete

Pete
Paul Sperry
Honored Contributor

Re: How to do this find command?

find test/aoi/inspire/defects/812165000/E02 -type d -ctime 30 |xargs rm
John Poff
Honored Contributor

Re: How to do this find command?

Hi Angie,

Here is one way to try it:

find /test/aoi/inspire/defects/812* -type d -mtime +30 -depth -ok rm -rf {} \;


I would suggest testing it first by replacing the 'rm -rf' with something like an 'ls -ld' to make sure it is returning the correct directories.

JP
Angie_1
Regular Advisor

Re: How to do this find command?

Hey thanks both for replying!

So would either of those find commands go underneath the 812* part, then go underneath each of the directories under there? Example, 812165000 may have E01, E02, E03...E20 and I would need to do the actual check on each directory under each E01, E02, E03... etc.

Please respond.
Angie
Pete Randall
Outstanding Contributor

Re: How to do this find command?

Angie,

Sorry, I left out the mtime part!


Pete

Pete
Angie_1
Regular Advisor

Re: How to do this find command?

Thats ok! I appreciate all the help! I need to figure out which one to try now..thx!

Angie
Brian Crabtree
Honored Contributor

Re: How to do this find command?

Angie,

One thing to consider is that removing a file from a directory will change the modified date of the directory, so the counter will reset. You might want to run the find without trying to remove the directories first, store the information in a file, and then try to remove the directories.

Brian
Paul Sperry
Honored Contributor

Re: How to do this find command?

The find should go underneath the 812* part
Helen French
Honored Contributor

Re: How to do this find command?

Yes, the find command will check all directories and it's subs:

# find dir_path -type d -depth -xdev \( -atime +30 -a -mtime +30 \) -exec rm -ir {} \;

This will check each directories and will prompt for delete if not accessed or modified within last 30 days.
Life is a promise, fulfill it!
Gary Yu
Super Advisor

Re: How to do this find command?

Hi Shiju,

'find' on a directory will change it's "atime" immediately, so I guess the command you gave won't yield any result.

thanks,
Gary
Angie_1
Regular Advisor

Re: How to do this find command?

Why isn't this command working as it looks to me it should (I am trying to use this one as I understand it better than the other suggestions):

find /tmp/811* -type d -mtime +30 -depth -ok ls -ld {} \;
John Poff
Honored Contributor

Re: How to do this find command?

Angie,

What error message are you getting when you try the command? Do you have some 811* directories in /tmp? You might have to enclose the '/tmp/811*' in single quotes.

JP
Shannon Petry
Honored Contributor

Re: How to do this find command?

Not sure why your command works, unless it sees nothing over 30 days old. If I were you, I'd take this a step further and create a cache file to work from.. I.E.

#!/bin/sh
##########
CACHE=/tmp/.doIdelete
if [ -f "${CACHE}" ] ; then
rm -f $CACHE
fi
touch $CACHE
find /test/aoi/inspire/defects -type d -name "812*" -atime +30 -print 1>>$CACHE 2>>/dev/null

# At this point, look at your file.
# If it's empty, then nothing is older
# than 30 days. Change the number to
# ensure that your seeing output
# If you have output, add this

for DIR in `cat ${CACHE}` ; do
echo "Delete $DIR? Y/N"
read INPUT
case $INPUT in
y*|Y*) rm -fr $DIR
echo "$DIR is gone"
;;
n*|N*) echo "Not touching it"
esac
done

This will make a nice loop, and give you a chance to see what is going on. While find is good by itself, most of us want to "Know" that it's working like we want.


Regards,
Shannon
Microsoft. When do you want a virus today?
Shannon Petry
Honored Contributor

Re: How to do this find command?

echo sorry, I hate this little typing box... In the case statement you need to add ";;" in the line below the "n*|N)" line.

The whole case should be...

case $INPUT in
y*|Y*) rm -fr $DIR
echo "$DIR is gone"
;;
n*|N*) echo "Not touching it"
;;
esac




Shannon
Microsoft. When do you want a virus today?
Angie_1
Regular Advisor

Re: How to do this find command?

Yes I have a directory in /tmp to test with.

I don't understand the Case statements very well. I was trying to avoid that and just use the find command.

Can it be done?

Angie
Dario_1
Trusted Contributor

Re: How to do this find command?

Angie

If you are 100% sure you can delete the directory then avoid the part that prompt you for an answer. In that case, just try Pete's recommendation. Otherwise you will have to use a case statement.

Regards,

DR
Dario_1
Trusted Contributor

Re: How to do this find command?

Angie:

I used Pete's answer as an example, Paul and John's will work too. I did not want to leave them behind.

Regards,

DR

NO POINTS PLEASE
Shannon Petry
Honored Contributor

Re: How to do this find command?

The case statement is not as confusing as it looks.

case $VARIABLE in
something) "exec this"
;; #ends something
nextthing) "exec this"
;;
esac

All I did was used the "read" command to get input from the user. I used y*|Y*) so that if anything beginning with "y" or "Y" is typed in, it's accepted to delete. Meaning someone types "yes" instead of just "y" then enter. Yes, you can use wildcards as well as "|" piped strings to look for in the case.

I think if you type it in just how I have it, it should work.

Regards,
Shannon
Microsoft. When do you want a virus today?
Angie_1
Regular Advisor

Re: How to do this find command?

Ok I will relook at that case statement.

Thanks a ton everyone!!!

I owe some points!!! And will assign them.

Thanks,
Angie