From: Gyruss on
Dear all,

Is it possible to use find to pattern match on directory names, and then do
something to all of the files that lie within the directories that are
found?

What I want to do is find all directories called "logs" or "data" in a
heirarchy, then delete every file older than 7 days in "logs" and every file
older than 30 days in "data".

Cheers!


From: Janis Papanagnou on
Gyruss wrote:
> Dear all,
>
> Is it possible to use find to pattern match on directory names, and then do
> something to all of the files that lie within the directories that are
> found?

Yes, that's possible. On what system are you working?

> What I want to do is find all directories called "logs" or "data" in a
> heirarchy, then delete every file older than 7 days in "logs" and every file
> older than 30 days in "data".

Have a look at the 'find' tool ('man find' for all options).

To find directories...

find <startdirs...> -type d

To use shell pattern matching...

find ... -name "quoted expression"

Or (GNU only, I think) standard regexps...

find ... -regex "..."

To find newer file than a reference file (use 'touch' to create a ref file)...

find -newer <reffile>

Or if you know the specific modification times use option -mtime

To perform some action see the 'xargs' command, or the 'find -exec' option.
You may write a loop and perform any action...

find ... | while read -r dir; do anyting_with "$dir"; done


Janis
From: Stephane Chazelas on
On Tue, 15 Aug 2006 22:44:00 +1000, Gyruss wrote:
> Dear all,
>
> Is it possible to use find to pattern match on directory names, and then do
> something to all of the files that lie within the directories that are
> found?
>
> What I want to do is find all directories called "logs" or "data" in a
> heirarchy, then delete every file older than 7 days in "logs" and every file
> older than 30 days in "data".
[...]

You can nest several ones:

find . \
-name logs -type d -exec sh -c '
find "$1" -type f -mtime +7 -exec rm -f \{\} +' {} {} \; \
-o \
-name data -type d -exec sh -c '
find "$1" -type f -mtime +30 -exec rm -f \{\} +' {} {} \;

I use sh in order to be able to write "\{\}" so that the first
find doesn't expand it.

--
Stephane