find backward

I’m looking for a way (using standard command) to find a file that is in
a directory up from the search location. The find command would
work fine, but I cannot find an option to get it to scan in the
upward direction in the directory tree. It only goes downward
in deeper level of directory.

  • Mario

Do you want to start it in the next directory up or do you really want to
find a file above you by any means necessary? “find / -name file” will start
the search for file in the root of the filesystem no matter where you are
but I’m sure that’s not what you mean.

Poseidon

Mario Charest <mcharest@clipzinformatic.com> wrote:

I’m looking for a way (using standard command) to find a file that is in
a directory up from the search location. The find command would
work fine, but I cannot find an option to get it to scan in the
upward direction in the directory tree. It only goes downward
in deeper level of directory.

I don’t know of a way to do it with a single command, but it’s pretty
easy with a shell script. Edit “/usr/bin/addvariant” and look for the
line “find the directory containing common.mk”.


Brian Stecher (bstecher@qnx.com) QNX Software Systems, Ltd.
phone: +1 (613) 591-0931 (voice) 175 Terence Matthews Cr.
+1 (613) 591-3579 (fax) Kanata, Ontario, Canada K2M 1W8

“Poseidon” <paul.ryan2@nospam.virgin.net> wrote in message
news:9rrnvh$css$1@nntp.qnx.com

Do you want to start it in the next directory up or do you really want to
find a file above you by any means necessary?

Above the current level.

“find / -name file” will start
the search for file in the root of the filesystem no matter where you are
but I’m sure that’s not what you mean.

No that wouldn’t work cause it may find multiple instance of the file
I’m looking for. I’m very surprise “find” doesn’t have an option
to search backward.

Poseidon

“Brian Stecher” <bstecher@qnx.com> wrote in message
news:9rrqat$e3p$1@nntp.qnx.com

Mario Charest <> mcharest@clipzinformatic.com> > wrote:
I’m looking for a way (using standard command) to find a file that is in
a directory up from the search location. The find command would
work fine, but I cannot find an option to get it to scan in the
upward direction in the directory tree. It only goes downward
in deeper level of directory.

I don’t know of a way to do it with a single command, but it’s pretty
easy with a shell script. Edit “/usr/bin/addvariant” and look for the
line “find the directory containing common.mk”.

That’s exaclty what I want to do. Problem is I want
to run this from a Makefile $(shell). I would really want to avoid the need
for an external script/program. As of now I’ve written a program
that does it but it has to be available in the user’s path.
I want to avoid this requirement.


Brian Stecher (> bstecher@qnx.com> ) QNX Software Systems, Ltd.
phone: +1 (613) 591-0931 (voice) 175 Terence Matthews Cr.
+1 (613) 591-3579 (fax) Kanata, Ontario, Canada K2M
1W8

Perhaps I’m missing something but can’t you just do:
find … [rest of your filters]


Bill Caroselli – 1(530) 510-7292
Q-TPS Consulting
QTPS@EarthLink.net


“Mario Charest” <mcharest@clipzinformatic.com> wrote in message
news:9rrrl4$hbi$1@inn.qnx.com

“Brian Stecher” <> bstecher@qnx.com> > wrote in message
news:9rrqat$e3p$> 1@nntp.qnx.com> …
Mario Charest <> mcharest@clipzinformatic.com> > wrote:
I’m looking for a way (using standard command) to find a file that is
in
a directory up from the search location. The find command would
work fine, but I cannot find an option to get it to scan in the
upward direction in the directory tree. It only goes downward
in deeper level of directory.

I don’t know of a way to do it with a single command, but it’s pretty
easy with a shell script. Edit “/usr/bin/addvariant” and look for the
line “find the directory containing common.mk”.

That’s exaclty what I want to do. Problem is I want
to run this from a Makefile $(shell). I would really want to avoid the
need
for an external script/program. As of now I’ve written a program
that does it but it has to be available in the user’s path.
I want to avoid this requirement.

\

Brian Stecher (> bstecher@qnx.com> ) QNX Software Systems, Ltd.
phone: +1 (613) 591-0931 (voice) 175 Terence Matthews Cr.
+1 (613) 591-3579 (fax) Kanata, Ontario, Canada K2M
1W8

Now I re-read this. Are you saying that you want to look in your parent’s
dir, then your parent’s parent’s dir, etc?

If so, write a program yourself. Readdir() isn’t that hard to use. I’ve
done this in QNX4. (I haven’t tried it in QNX6 yet.)


Bill Caroselli – 1(530) 510-7292
Q-TPS Consulting
QTPS@EarthLink.net


“Bill Caroselli” <qtps@earthlink.net> wrote in message
news:9rrtec$icv$1@inn.qnx.com

Perhaps I’m missing something but can’t you just do:
find … [rest of your filters]


Bill Caroselli – 1(530) 510-7292
Q-TPS Consulting
QTPS@EarthLink.net


“Mario Charest” <> mcharest@clipzinformatic.com> > wrote in message
news:9rrrl4$hbi$> 1@inn.qnx.com> …

“Brian Stecher” <> bstecher@qnx.com> > wrote in message
news:9rrqat$e3p$> 1@nntp.qnx.com> …
Mario Charest <> mcharest@clipzinformatic.com> > wrote:
I’m looking for a way (using standard command) to find a file that
is
in
a directory up from the search location. The find command would
work fine, but I cannot find an option to get it to scan in the
upward direction in the directory tree. It only goes downward
in deeper level of directory.

I don’t know of a way to do it with a single command, but it’s pretty
easy with a shell script. Edit “/usr/bin/addvariant” and look for the
line “find the directory containing common.mk”.

That’s exaclty what I want to do. Problem is I want
to run this from a Makefile $(shell). I would really want to avoid the
need
for an external script/program. As of now I’ve written a program
that does it but it has to be available in the user’s path.
I want to avoid this requirement.

\

Brian Stecher (> bstecher@qnx.com> ) QNX Software Systems, Ltd.
phone: +1 (613) 591-0931 (voice) 175 Terence Matthews Cr.
+1 (613) 591-3579 (fax) Kanata, Ontario, Canada
K2M
1W8
\

Mario Charest <mcharest@clipzinformatic.com> wrote:

That’s exaclty what I want to do. Problem is I want
to run this from a Makefile $(shell). I would really want to avoid the need
for an external script/program. As of now I’ve written a program
that does it but it has to be available in the user’s path.
I want to avoid this requirement.

Using “;” to put multiple commands on one line, you should be able to hack
that together.


Brian Stecher (bstecher@qnx.com) QNX Software Systems, Ltd.
phone: +1 (613) 591-0931 (voice) 175 Terence Matthews Cr.
+1 (613) 591-3579 (fax) Kanata, Ontario, Canada K2M 1W8

“Brian Stecher” <bstecher@qnx.com> wrote in message
news:9rs096$hu5$1@nntp.qnx.com

Mario Charest <> mcharest@clipzinformatic.com> > wrote:
That’s exaclty what I want to do. Problem is I want
to run this from a Makefile $(shell). I would really want to avoid the
need
for an external script/program. As of now I’ve written a program
that does it but it has to be available in the user’s path.
I want to avoid this requirement.

Using “;” to put multiple commands on one line, you should be able to hack
that together.

I’ll give it a try thanks.



Brian Stecher (> bstecher@qnx.com> ) QNX Software Systems, Ltd.
phone: +1 (613) 591-0931 (voice) 175 Terence Matthews Cr.
+1 (613) 591-3579 (fax) Kanata, Ontario, Canada K2M
1W8

“Bill Caroselli” <qtps@earthlink.net> wrote in message
news:9rrtns$im2$1@inn.qnx.com

Now I re-read this. Are you saying that you want to look in your parent’s
dir, then your parent’s parent’s dir, etc?

Exactly.

If so, write a program yourself. Readdir() isn’t that hard to use.

That’s what I done. There was not need for readdir since I know
the file I’m looking for, no need to scan the directory:

I’m basicly doing

stat( “file” );
stat ( “…/file” );
stat( “…/…/file” )

Until it success or it gives up after X level.

done this in QNX4. (I haven’t tried it in QNX6 yet.)

As I said in another post, I want to avoid using a program or a script.
I need to do this within a Makefile.



Bill Caroselli – 1(530) 510-7292
Q-TPS Consulting
QTPS@EarthLink.net


“Bill Caroselli” <> qtps@earthlink.net> > wrote in message
news:9rrtec$icv$> 1@inn.qnx.com> …
Perhaps I’m missing something but can’t you just do:
find … [rest of your filters]


Bill Caroselli – 1(530) 510-7292
Q-TPS Consulting
QTPS@EarthLink.net


“Mario Charest” <> mcharest@clipzinformatic.com> > wrote in message
news:9rrrl4$hbi$> 1@inn.qnx.com> …

“Brian Stecher” <> bstecher@qnx.com> > wrote in message
news:9rrqat$e3p$> 1@nntp.qnx.com> …
Mario Charest <> mcharest@clipzinformatic.com> > wrote:
I’m looking for a way (using standard command) to find a file that
is
in
a directory up from the search location. The find command would
work fine, but I cannot find an option to get it to scan in the
upward direction in the directory tree. It only goes downward
in deeper level of directory.

I don’t know of a way to do it with a single command, but it’s
pretty
easy with a shell script. Edit “/usr/bin/addvariant” and look for
the
line “find the directory containing common.mk”.

That’s exaclty what I want to do. Problem is I want
to run this from a Makefile $(shell). I would really want to avoid
the
need
for an external script/program. As of now I’ve written a program
that does it but it has to be available in the user’s path.
I want to avoid this requirement.

\

Brian Stecher (> bstecher@qnx.com> ) QNX Software Systems,
Ltd.
phone: +1 (613) 591-0931 (voice) 175 Terence Matthews Cr.
+1 (613) 591-3579 (fax) Kanata, Ontario, Canada
K2M
1W8


\

What about a shell script like this? I call it rfind and it just passes the
arguments off to find.

#!/bin/sh

RETVAL=1

if [ $# -gt 1 ] ; then
case $1 in
-*)
FIND_PATH=""
;;
*)
FIND_PATH=$1
shift
;;
esac
fi

while [ pwd != “/” ] ; do
cd …
if find $FIND_PATH -maxdepth 1 $* ; then
RETVAL=0
fi
done

return $RETVAL

cheers,

Kris

Mario Charest <mcharest@clipzinformatic.com> wrote:

“Brian Stecher” <> bstecher@qnx.com> > wrote in message
news:9rs096$hu5$> 1@nntp.qnx.com> …
Mario Charest <> mcharest@clipzinformatic.com> > wrote:
That’s exaclty what I want to do. Problem is I want
to run this from a Makefile $(shell). I would really want to avoid the
need
for an external script/program. As of now I’ve written a program
that does it but it has to be available in the user’s path.
I want to avoid this requirement.

Using “;” to put multiple commands on one line, you should be able to hack
that together.


I’ll give it a try thanks.


Brian Stecher (> bstecher@qnx.com> ) QNX Software Systems, Ltd.
phone: +1 (613) 591-0931 (voice) 175 Terence Matthews Cr.
+1 (613) 591-3579 (fax) Kanata, Ontario, Canada K2M
1W8


Kris Warkentin
kewarken@qnx.com
(613)591-0836 x9368
“Computer science is no more about computers than astronomy is about telescopes”
–E.W.Dijkstra

Oops…not quite right…see the change below:


Kris Eric Warkentin <kewarken@qnx.com> wrote:

What about a shell script like this? I call it rfind and it just passes the
arguments off to find.

#!/bin/sh

RETVAL=1

if [ $# -gt 1 ] ; then
case $1 in
-*)
FIND_PATH=""
;;
*)
FIND_PATH=$1
shift
;;
esac
fi

while [ pwd != “/” ] ; do
cd …
if find $FIND_PATH -maxdepth 1 $* ; then
should be:

if find $FIND_PATH -maxdepth 1 $* | xargs -i echo $PWD/basename {}; then

This isn’t perfect but at least it give a valid path.



RETVAL=0
fi
done

return $RETVAL

cheers,

Kris

Mario Charest <> mcharest@clipzinformatic.com> > wrote:

“Brian Stecher” <> bstecher@qnx.com> > wrote in message
news:9rs096$hu5$> 1@nntp.qnx.com> …
Mario Charest <> mcharest@clipzinformatic.com> > wrote:
That’s exaclty what I want to do. Problem is I want
to run this from a Makefile $(shell). I would really want to avoid the
need
for an external script/program. As of now I’ve written a program
that does it but it has to be available in the user’s path.
I want to avoid this requirement.

Using “;” to put multiple commands on one line, you should be able to hack
that together.


I’ll give it a try thanks.


Brian Stecher (> bstecher@qnx.com> ) QNX Software Systems, Ltd.
phone: +1 (613) 591-0931 (voice) 175 Terence Matthews Cr.
+1 (613) 591-3579 (fax) Kanata, Ontario, Canada K2M
1W8


Kris Warkentin
kewarken@qnx.com
(613)591-0836 x9368
“Computer science is no more about computers than astronomy is about telescopes”
–E.W.Dijkstra


Kris Warkentin
kewarken@qnx.com
(613)591-0836 x9368
“Computer science is no more about computers than astronomy is about telescopes”
–E.W.Dijkstra

Mario,

Here’s a script:

#! /bin/sh
#__USAGE
#%C filename

Look in parent directory and on up for the specified file

file=$1
dir=$PWD
while true; do
[ -f “$dir/$file” ] && echo $dir/$file && exit 0
parent=dirname $dir
[ “$parent” eq “$dir” ] && break
dir=$parent
done
echo “File ‘$file’ not found”
exit 1

Of course you can strip this down considerably and use line-continuation
characters (’’) liberally in order to stuff it into a Makefile.

-Norton

Kris Eric Warkentin <kewarken@qnx.com> wrote:

What about a shell script like this? I call it rfind and it just passes the
arguments off to find.

[Script deleted]

Why didn’t you call it dnif?

Thomas

Mario Charest <> mcharest@clipzinformatic.com> > wrote:

“Brian Stecher” <> bstecher@qnx.com> > wrote in message
news:9rs096$hu5$> 1@nntp.qnx.com> …
Mario Charest <> mcharest@clipzinformatic.com> > wrote:
That’s exaclty what I want to do. Problem is I want
to run this from a Makefile $(shell). I would really want to avoid the
need
for an external script/program. As of now I’ve written a program
that does it but it has to be available in the user’s path.
I want to avoid this requirement.

Using “;” to put multiple commands on one line, you should be able to hack
that together.


I’ll give it a try thanks.


Brian Stecher (> bstecher@qnx.com> ) QNX Software Systems, Ltd.
phone: +1 (613) 591-0931 (voice) 175 Terence Matthews Cr.
+1 (613) 591-3579 (fax) Kanata, Ontario, Canada K2M
1W8


Kris Warkentin
kewarken@qnx.com
(613)591-0836 x9368
“Computer science is no more about computers than astronomy is about telescopes”
–E.W.Dijkstra

Thomas (toe-mah) Fletcher QNX Software Systems
thomasf@qnx.com Core OS Technology Group
(613)-591-0931 http://www.qnx.com/

I believe eq in second test should be ‘=’, that is,

[ “$parent” = “$dir” ] && break

Also, I noted that:

[ -f //.boot ]

returns false whereas:

[ -f /.boot ]

returns true.

Bug in ksh?

Norton Allen wrote:

Mario,

Here’s a script:

#! /bin/sh
#__USAGE
#%C filename

Look in parent directory and on up for the specified file

file=$1
dir=$PWD
while true; do
[ -f “$dir/$file” ] && echo $dir/$file && exit 0
parent=dirname $dir
[ “$parent” eq “$dir” ] && break
dir=$parent
done
echo “File ‘$file’ not found”
exit 1

Of course you can strip this down considerably and use line-continuation
characters (’’) liberally in order to stuff it into a Makefile.

-Norton

I was testing this on QNX4, not RTP.

Dean Douthat wrote:

I believe eq in second test should be ‘=’, that is,

[ “$parent” = “$dir” ] && break

Also, I noted that:

[ -f //.boot ]

returns false whereas:

[ -f /.boot ]

returns true.

Bug in ksh?

Norton Allen wrote:

Mario,

Here’s a script:

#! /bin/sh
#__USAGE
#%C filename

Look in parent directory and on up for the specified file

file=$1
dir=$PWD
while true; do
[ -f “$dir/$file” ] && echo $dir/$file && exit 0
parent=dirname $dir
[ “$parent” eq “$dir” ] && break
dir=$parent
done
echo “File ‘$file’ not found”
exit 1

Of course you can strip this down considerably and use line-continuation
characters (’’) liberally in order to stuff it into a Makefile.

-Norton

<thomasf@qnx.com> wrote in message news:9ruc14$3ar$1@nntp.qnx.com

Kris Eric Warkentin <> kewarken@qnx.com> > wrote:
What about a shell script like this? I call it rfind and it just passes
the
arguments off to find.

[Script deleted]

Why didn’t you call it dnif?

Thomas

Cute! I like it.

“Kris Eric Warkentin” <kewarken@qnx.com> wrote in message
news:9rs7tb$mf7$1@nntp.qnx.com

What about a shell script like this?

I want to do this within a makefile without having to invoke any non system
script/program

Mario Charest wrote:

“Kris Eric Warkentin” <> kewarken@qnx.com> > wrote in message
news:9rs7tb$mf7$> 1@nntp.qnx.com> …
What about a shell script like this?

I want to do this within a makefile without having to invoke any non system
script/program

Preceed first line with ‘@’ and escape all newlines except last one with ‘’.

This is nice too. I had only used find -maxdepth 1 so that you could do
all the funky stuff that find lets you do…you know, mtime, type, etc.

If you’re just doing a simple search for a name, this is a much better
script.

Kris

Norton Allen <allen@huarp.harvard.edu> wrote:

Mario,

Here’s a script:

#! /bin/sh
#__USAGE
#%C filename

Look in parent directory and on up for the specified file

file=$1
dir=$PWD
while true; do
[ -f “$dir/$file” ] && echo $dir/$file && exit 0
parent=dirname $dir
[ “$parent” eq “$dir” ] && break
dir=$parent
done
echo “File ‘$file’ not found”
exit 1

Of course you can strip this down considerably and use line-continuation
characters (’’) liberally in order to stuff it into a Makefile.

-Norton


Kris Warkentin
kewarken@qnx.com
(613)591-0836 x9368
“Computer science is no more about computers than astronomy is about telescopes”
–E.W.Dijkstra