1834255 Members
2554 Online
110066 Solutions
New Discussion

find command...

 
SOLVED
Go to solution
Hazem Mahmoud_3
Respected Contributor

find command...

I'm having a little trouble getting find to do what I want it to. Here is the deal:
I have a directory that has a bunch of directories. I want to remove all directories inside that directory that are older than 90 days, using the timestamp of that directory. Example:
# ll /maindir
drwxrwsr-x 4 speclive live 8192 Aug 17 2003 dir1
drwxrwsr-x 4 spxlive live 8192 May 7 09:32 dir2
drwxrwsr-x 4 spxlive live 8192 May 3 05:33 dir3
drwxrwsr-x 2 spxlive live 96 Jan 24 14:59 dir4

Therefore I would want dir1 and dir4 deleted, with all the contents under them.
Also, I would first want to list which directories I selected, then remove them (these can be in two different find commands). Here is what I have so far, but it's working right:
find /maindir -name * +mtime +90 -exec ll {} \;
find /maindir -name * +mtime +90 -exec rm {} \;

I've also tried playing with -prune, but it doesn't do what I want.
10 points to the person who can help me solve this! Thanks!

-Hazem
14 REPLIES 14
Pete Randall
Outstanding Contributor

Re: find command...

Hazem
I think you're close but you need to specify the directory option:

find /maindir -type d -name * +mtime +90 -exec ll {} \;

find /maindir -type d -name * +mtime +90 -exec rm {} \;


Pete

Pete
Fred Ruffet
Honored Contributor

Re: find command...

You should add the "-type d" option to your find to select only directories. You could also remove "-name *" for 2 reasons :

1. * is evaluated by your shell when launching so it will only find dirs that are in your start dir ".". You should put
-name "*" instead so that * is evaluated each time

2. -name "*" is what is done by default if you don't put -name.

So your command could be :
find /maindir -type d +mtime +90 -exec ll {} \;

regards,

Fred
--

"Reality is just a point of view." (P. K. D.)
Tom Dineen_2
Advisor

Re: find command...


yes, the "-type d" is what you want, but
to list the acutal directories found do:

use: "-exec ls -ld {} \;"

Hazem Mahmoud_3
Respected Contributor

Re: find command...

I tried it with type -d and here is the message I got:
find: missing conjunction

I also tried it without the -name option.

-Hazem
Fred Ruffet
Honored Contributor

Re: find command...

Another point :

using "ll {}\;" will list the content of each directory found. you should use -d option to see the directory itself.

So it will look :
find /maindir -type d +mtime +90 -exec ll -d {} \;

Regards,

Fred
--

"Reality is just a point of view." (P. K. D.)
Fred Ruffet
Honored Contributor

Re: find command...

I didn't saw the +mtime... use -mtime instead.
--

"Reality is just a point of view." (P. K. D.)
Hazem Mahmoud_3
Respected Contributor

Re: find command...

Ok, we're getting closer, but not there yet. Here is what I used:
find /maindir -type d -mtime +90 -exec ll -d {} \;

Good catch on the -mtime Fred, that was one of my problems. Also, it did not like it when I specified -name *, so another good catch on that one Fred.
However, it is giving me all directories under /maindir, including the sub-directores under the directories in /maindir. So it is still recursively going down the directory structure. It is however giving me the directores older than 90 days that are directly under /maindir.
Any ideas how to stop it from going further down the directory structure?

-Hazem
Fred Ruffet
Honored Contributor

Re: find command...

Another answer again (I'm a bit tired) :

about the concept this time. It's not because your dir has a modification date older than 90 days that all of its subdirs will be older than 90 days.

Once you'll run the find with ll, take the answers, and do the same find with "-mtime -90" on them...
--

"Reality is just a point of view." (P. K. D.)
Fred Ruffet
Honored Contributor
Solution

Re: find command...

for your recursive problem, it's time to use -prune. Each matching dir will be skipped.

Fred
--

"Reality is just a point of view." (P. K. D.)
Hazem Mahmoud_3
Respected Contributor

Re: find command...

When I use -prune it does not pick up anything.

-Hazem
Fred Ruffet
Honored Contributor

Re: find command...

I have this :

#find /etc -type d -mtime +90 -prune -exec ls -ld {} \;
dr-xr--r-- 4 bin bin 96 Nov 15 2000 /etc/ftpd
dr-xr-xr-x 11 bin bin 8192 Jan 14 07:41 /etc/vx
dr-xr-xr-x 2 bin bin 8192 Jan 14 07:17 /etc/hpC2400
dr-xr-xr-x 2 bin bin 96 Nov 15 2000 /etc/local
dr-xr-xr-x 2 bin bin 96 Jan 14 07:59 /etc/default
dr-xr-xr-x 2 bin bin 96 Nov 15 2000 /etc/hparray
drwxr-xr-x 4 root sys 96 Nov 15 2000 /etc/vhelp
drwxr-xr-x 3 bin bin 8192 Nov 15 2000 /etc/ppp
dr-xr-xr-x 3 bin bin 8192 Jan 14 17:49 /etc/sam
drwxr-xr-x 12 root bin 8192 Jan 14 07:57 /etc/opt
drwxr-xr-x 2 bin bin 96 Nov 15 2000 /etc/gss
drwxr-xr-x 2 bin bin 96 Nov 15 2000 /etc/acct
drwxr-xr-x 3 bin bin 96 Nov 15 2000 /etc/asx
drwxr-xr-x 8 lp bin 8192 Nov 15 2000 /etc/lp
dr-xr-xr-x 2 bin bin 8192 Nov 15 2000 /etc/uucp
drwxr-xr-x 8 root sys 8192 Jan 14 07:57 /etc/X11
drwxr-xr-x 3 bin bin 96 Nov 15 2000 /etc/vue
drwxr-xr-x 5 bin bin 96 Nov 15 2000 /etc/net
drwxr-xr-x 4 bin bin 96 Jan 14 07:53 /etc/dt
dr-xr-xr-x 2 bin bin 96 Jan 14 07:17 /etc/switch
dr-xr-xr-x 2 bin bin 8192 Jan 14 07:17 /etc/eisa
drwxr-xr-x 3 root root 96 Jan 14 07:23 /etc/.java
dr-xr-xr-x 3 bin bin 96 Jan 14 07:41 /etc/cmcluster
drwxr-xr-x 2 root sys 96 Jan 14 07:54 /etc/ximian

Difference with or whitout -prune :

#find /etc -type d -mtime +90 -prune -exec ls -ld {} \; | wc -l
24
#find /etc -type d -mtime +90 -exec ls -ld {} \; | wc -l
187

Fred

--

"Reality is just a point of view." (P. K. D.)
Hazem Mahmoud_3
Respected Contributor

Re: find command...

That did it Fred!!! I had prune running before the -mtime and it seems like it did not like that (I'm not sure why). So I had it run after the -mtime and it worked great! Thanks for all your help troubleshooting this!!!

-Hazem
Geoff Wild
Honored Contributor

Re: find command...

Fred is correct:

# find /etc -type d -mtime +90 -exec ls -d {} \; |wc
175 175 4468
# find /etc -type d -mtime +90 -prune -exec ls -d {} \; |wc
46 46 680

Now - as far as rm, it won't delete directory's if they have contents - unless you do a rm -rf and that is dangerous!

I would be more inclined to find all dirs over 90 days, list their contents, then delete the files under them - just to be safe....

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Hazem Mahmoud_3
Respected Contributor

Re: find command...

Thanks for the information Geoff, I'll definitely take that into consideration.

Congratulations on your new hat Fred!!! I hope your first hat feels good!:)

-Hazem