Searching for files over 30 days old in current directory

May be a simple question for experts here....

I need to get the list of files older than 30 days in the current folder. I tried "find", but it searches recursively in all the sub directories.

Can I restrict the recursive search and extract the files only from current directory ?

to find file in DIR
use
find $DIR -type f -atime +30 -print
hope it will suffice

See if this works:

find . -mtime +30 \( ! -name . -prune \)

Both the above scripts are digging down the sub directories. Any other altrenative ?

Try....

find $PWD -type f -mtime +30 -print|grep -Ev "$PWD/.*/"

Thanks a lot Ygor, this one is working.

Again thanks everybody.

Hmm... the -prune option is supposed to be what prevents going into subdirectories! :confused: At least, it works on ksh... what shell are you using? Ygor had a great work-around idea, but I'd think it would be faster if there's a ton of subdirectories to use -prune, rather than grabbing all the files in all the subdirectories, then eliminating everything except the current directory.

You almost had it right, oombera...

find . \( ! -name . -prune \) -mtime +30

Its great. Perderabo's solution works. But can somebody explain how the order of options is changing the output here ??

i didnt know about the prune option.

it seems to be an order of presidence useing Perderabo version.

find the files. then only select the ones that are 30days old.

vs. find anything that is 30 days old then (this part is negated?) find files in the current directory.

find does stuff in order.

With
find . -mtime +30 \( ! -name . -prune \)
a candidate entry in . must first pass the "-mtime +30" test. Only then do we see if we want to prune it or not.

hi for this command
find $PWD -type f -mtime +30 -print|grep -Ev "$PWD/.*/"

can i know the usage for E in grep -Ev
this produce error when i use it.
grep: illegal option -- E
Usage: grep -hblcnsviw pattern file . . .

You must have a very old grep. -E is required by posix. It allows extended regular expressions. You might have an egrep program that can do extended regular expressions.

Maybe I'm misunderstanding, but seems to me if all you want to do is prevent recursing into subdirectories you could also do this with

find -maxdepth 1

I think it should be '-maxdepth 0' then it will work,

how ever man pages definetly say that -purne option will not allow find to search in subdirectories of current directory...

cheers,

maxdepth 0 has maximum of one line of output -- TITS. (Try It To See)

maxdepth -1 outputs the contents of one directories, without recursing into subdirs.

At least, seems that way to me :wink:

A use that I have found for the prune option is to find everything in a directory but skip version controlling dirs and their contents, eg folders named .csv or .svn.

find . -type d -name .svn -prune -false -o -type f

Most recent HP-UX find man page: HP-UX find(1)
Most recent Solaris find man page: Solaris find(1)
The official Posix standard for find: IEEE Std 1003.1, 2004 Edition find

Please notice what these documents have to say regarding the -maxdepth option: not one word. Solutions involving -maxdepth are not portable at all.

Well, I can find only two flavors of -maxdepth and neither supports the view that output is limited to a single line. When I tried it...

$ touch one two three
$ find one two three -maxdepth 0 -type f
one
two
three
$ find .
.
./one
./two
./three
$ 

Thanks for pointing that out.

I hadn't really thought about portability, but I am going to try be more "portably" correct.

In terms of portability, I am assuming that the posix definition should be fairly portable, so I will pay the most attention to that.

Try this :

#!/bin/sh

i="/pat/to/dir/you/wanna/check/"
msg () {
for i
do echo "clearing: $i" >&2
done
}

fatal () { msg "$@"; exit 1; }

if [ ! -d "$i" ]
then msg "no directory: $i"
elif [ ! -r "$i" -o ! -w "$i" ]
then msg "access denied (no rw- access): $i"
else
# search & destroy
find "$i" ! -type d -mtime +30d -print | xargs rm -f > /dev/null 2>&1

[ -d "$i" ] || continue
(cd $i || continue
for d in *
do
[ -d "$d" ] || continue
find . -depth -type d -mtime +30d -print |
xargs rmdir > /dev/null 2>&1
done
)
fi
exit 0

This works for me to delete files 30 days old from directory $i in depth