System Administration
cancel
Showing results for 
Search instead for 
Did you mean: 

"non recursive find" or "how to get the age of a file"

SOLVED
Go to solution
Alexandre W. Agostini
Occasional Visitor

"non recursive find" or "how to get the age of a file"

Hi people!

Does anyone knows how to convince a find command not to descend the filesystem hierarchy and search only the current directory?

The reason for that are: I need to remove a certain file just if it's more than 7 days old. The problem is that it sits on top of a very large directory structure and I can't afford to wait the find traverse the whole place before finally look around and see the file. :)

Thanks already!
9 REPLIES
Uday_S_Ankolekar
Honored Contributor

Re: "non recursive find" or "how to get the age of a file"

cd to that directory then

find . -name "yourfile"

or

find /yourdir -name "your file"

-USA..
Good Luck..
Elena Leontieva
Esteemed Contributor

Re: "non recursive find" or "how to get the age of a file"

find . -maxdepth 0
will operate only on the current directory.
Rodney Hills
Honored Contributor
Solution

Re: "non recursive find" or "how to get the age of a file"

Try-

find . -path "./*" -prune -mtime +7 -print

The "-path" can limit the file path and "-prune" will prevent find from descending subfolders.

If you wanted to do one level down of directories-

find . -path "./*/*" -prune -mtime +7 -print

HTH

-- Rod Hills
There be dragons...
Alexandre W. Agostini
Occasional Visitor

Re: "non recursive find" or "how to get the age of a file"


That was my first attempt, but when the directory where I am have several others sub-directories each one having a lot of files the find will search those sub-directories also and the search takes much more time. How could I tell find to skip all sub-directories and search just the current one?
Rodney Hills
Honored Contributor

Re: "non recursive find" or "how to get the age of a file"

That's what "prune" does. It prunes the branches of the tree.

-- Rod Hills
There be dragons...
Alexandre W. Agostini
Occasional Visitor

Re: "non recursive find" or "how to get the age of a file"


Bingo!

That's perfect!

I was really messing up with the -prune syntax.

Thanks!
Michael Steele_2
Honored Contributor

Re: "non recursive find" or "how to get the age of a file"

Regarding "...remove a certain file just if it's more than 7 days old..."

I believe find uses a zero based counter where 0 = 24 hours. So for seven days use 6.

-ctime +6

GNU find is superior and uses 'maxdepth'.

find /dir/sub_dir -maxdepth 0 -ctime +6 -exec rm {} \;

-atime - read or view but don't write to. (* access *)
-mtime - write to. (* modify *)
-command line level only

So toy around with atime and ctime.

Here's GNU find:

http://hpux.cs.utah.edu/hppd/hpux/Gnu/findutils-4.1.5/

Support Fatherhood - Stop Family Law
rachel_11
Advisor

Re: "non recursive find" or "how to get the age of a file"

by combining ls & find you can run:
#!/bin/ksh
for i in `ls *fname*`
do
if [ -f $i ]
then
find $i -atime +7
fi
done
rexy
Occasional Visitor

Re: "non recursive find" or "how to get the age of a file"

Thanks that works for me R.