Find command for lookup up a directory tree
Is there any equivalent command, or options for, GNU find
that will search up the directory tree? I'd like to look backwards through the tree for files with a given name. For example, say I'm in /usr/local/share/bin and I want to search look for a file called foo
. Ideally I'd like the command to look for the file in the following order:
- /usr/local/share/bin/foo
- /usr/local/share/foo
- /usr/local/foo
- /usr/foo
- /foo
I know that I can write something like this as a shell function, but I was hoping there would be a command as rich as gnu find
that I could leverage.
find
bumped to the homepage by Community♦ 13 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
migrated from serverfault.com May 2 '14 at 10:56
This question came from our site for system and network administrators.
|
show 2 more comments
Is there any equivalent command, or options for, GNU find
that will search up the directory tree? I'd like to look backwards through the tree for files with a given name. For example, say I'm in /usr/local/share/bin and I want to search look for a file called foo
. Ideally I'd like the command to look for the file in the following order:
- /usr/local/share/bin/foo
- /usr/local/share/foo
- /usr/local/foo
- /usr/foo
- /foo
I know that I can write something like this as a shell function, but I was hoping there would be a command as rich as gnu find
that I could leverage.
find
bumped to the homepage by Community♦ 13 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
migrated from serverfault.com May 2 '14 at 10:56
This question came from our site for system and network administrators.
2
Have you tried find with the-depth
option?-depth Process each directory's contents before the directory itself.
.
– Zoredache
Apr 29 '14 at 5:54
As far as I can tell, the-depth
switch just changes thefind
's mode of operation in terms of which directories it looks at first. It doesn't appear to makefind
look upwards through the directory hierarchy.
– Bryan Kyle
Apr 29 '14 at 16:25
What exactly do you mean by upwards? When doing a tree search you either do depth first, or breadth first. A depth first search should return results like you describe above.
– Zoredache
Apr 29 '14 at 16:36
find
looks downwards through the directory structure, towards the leaves of the file system. I want to look upwards, meaning towards the root of the file system. See the list in my original question. Notice that it starts with a longer path (deeper in the file system) and moves towards towards short paths (shallower in the file system).
– Bryan Kyle
Apr 30 '14 at 15:38
And how do you think a command could actually acomplish that? The is no magical way for a command to know the deepest folder. Are you going to pass the starting folder on the command line, or?
– Zoredache
Apr 30 '14 at 15:48
|
show 2 more comments
Is there any equivalent command, or options for, GNU find
that will search up the directory tree? I'd like to look backwards through the tree for files with a given name. For example, say I'm in /usr/local/share/bin and I want to search look for a file called foo
. Ideally I'd like the command to look for the file in the following order:
- /usr/local/share/bin/foo
- /usr/local/share/foo
- /usr/local/foo
- /usr/foo
- /foo
I know that I can write something like this as a shell function, but I was hoping there would be a command as rich as gnu find
that I could leverage.
find
Is there any equivalent command, or options for, GNU find
that will search up the directory tree? I'd like to look backwards through the tree for files with a given name. For example, say I'm in /usr/local/share/bin and I want to search look for a file called foo
. Ideally I'd like the command to look for the file in the following order:
- /usr/local/share/bin/foo
- /usr/local/share/foo
- /usr/local/foo
- /usr/foo
- /foo
I know that I can write something like this as a shell function, but I was hoping there would be a command as rich as gnu find
that I could leverage.
find
find
asked Apr 29 '14 at 4:22
Bryan Kyle
bumped to the homepage by Community♦ 13 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 13 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
migrated from serverfault.com May 2 '14 at 10:56
This question came from our site for system and network administrators.
migrated from serverfault.com May 2 '14 at 10:56
This question came from our site for system and network administrators.
2
Have you tried find with the-depth
option?-depth Process each directory's contents before the directory itself.
.
– Zoredache
Apr 29 '14 at 5:54
As far as I can tell, the-depth
switch just changes thefind
's mode of operation in terms of which directories it looks at first. It doesn't appear to makefind
look upwards through the directory hierarchy.
– Bryan Kyle
Apr 29 '14 at 16:25
What exactly do you mean by upwards? When doing a tree search you either do depth first, or breadth first. A depth first search should return results like you describe above.
– Zoredache
Apr 29 '14 at 16:36
find
looks downwards through the directory structure, towards the leaves of the file system. I want to look upwards, meaning towards the root of the file system. See the list in my original question. Notice that it starts with a longer path (deeper in the file system) and moves towards towards short paths (shallower in the file system).
– Bryan Kyle
Apr 30 '14 at 15:38
And how do you think a command could actually acomplish that? The is no magical way for a command to know the deepest folder. Are you going to pass the starting folder on the command line, or?
– Zoredache
Apr 30 '14 at 15:48
|
show 2 more comments
2
Have you tried find with the-depth
option?-depth Process each directory's contents before the directory itself.
.
– Zoredache
Apr 29 '14 at 5:54
As far as I can tell, the-depth
switch just changes thefind
's mode of operation in terms of which directories it looks at first. It doesn't appear to makefind
look upwards through the directory hierarchy.
– Bryan Kyle
Apr 29 '14 at 16:25
What exactly do you mean by upwards? When doing a tree search you either do depth first, or breadth first. A depth first search should return results like you describe above.
– Zoredache
Apr 29 '14 at 16:36
find
looks downwards through the directory structure, towards the leaves of the file system. I want to look upwards, meaning towards the root of the file system. See the list in my original question. Notice that it starts with a longer path (deeper in the file system) and moves towards towards short paths (shallower in the file system).
– Bryan Kyle
Apr 30 '14 at 15:38
And how do you think a command could actually acomplish that? The is no magical way for a command to know the deepest folder. Are you going to pass the starting folder on the command line, or?
– Zoredache
Apr 30 '14 at 15:48
2
2
Have you tried find with the
-depth
option? -depth Process each directory's contents before the directory itself.
.– Zoredache
Apr 29 '14 at 5:54
Have you tried find with the
-depth
option? -depth Process each directory's contents before the directory itself.
.– Zoredache
Apr 29 '14 at 5:54
As far as I can tell, the
-depth
switch just changes the find
's mode of operation in terms of which directories it looks at first. It doesn't appear to make find
look upwards through the directory hierarchy.– Bryan Kyle
Apr 29 '14 at 16:25
As far as I can tell, the
-depth
switch just changes the find
's mode of operation in terms of which directories it looks at first. It doesn't appear to make find
look upwards through the directory hierarchy.– Bryan Kyle
Apr 29 '14 at 16:25
What exactly do you mean by upwards? When doing a tree search you either do depth first, or breadth first. A depth first search should return results like you describe above.
– Zoredache
Apr 29 '14 at 16:36
What exactly do you mean by upwards? When doing a tree search you either do depth first, or breadth first. A depth first search should return results like you describe above.
– Zoredache
Apr 29 '14 at 16:36
find
looks downwards through the directory structure, towards the leaves of the file system. I want to look upwards, meaning towards the root of the file system. See the list in my original question. Notice that it starts with a longer path (deeper in the file system) and moves towards towards short paths (shallower in the file system).– Bryan Kyle
Apr 30 '14 at 15:38
find
looks downwards through the directory structure, towards the leaves of the file system. I want to look upwards, meaning towards the root of the file system. See the list in my original question. Notice that it starts with a longer path (deeper in the file system) and moves towards towards short paths (shallower in the file system).– Bryan Kyle
Apr 30 '14 at 15:38
And how do you think a command could actually acomplish that? The is no magical way for a command to know the deepest folder. Are you going to pass the starting folder on the command line, or?
– Zoredache
Apr 30 '14 at 15:48
And how do you think a command could actually acomplish that? The is no magical way for a command to know the deepest folder. Are you going to pass the starting folder on the command line, or?
– Zoredache
Apr 30 '14 at 15:48
|
show 2 more comments
2 Answers
2
active
oldest
votes
find_backwards () {
test $# -ne 1 && return 2
if [ -z "$find_backwards_recurse" ]; then
pushd . &>/dev/null
find_backwards_recurse=yes
fi
if [ -f "$1" ]; then
echo "${PWD}/${1}"
popd &>/dev/null
find_backwards_recurse=
return 0
else
if [ / != "$PWD" ]; then
cd ..
find_backwards "$1"
else
popd &>/dev/null
find_backwards_recurse=
return 1
fi
fi
}
start cmd:> pwd
/home/hl/tmp/tmp
start cmd:> find_backwards foo
/home/hl/tmp/foo
add a comment |
I don't know any command who would do that.
One possible script implementation.
rfind:
#!/bin/bash
d=$1
[[ -f "$d/$2" ]] && echo "$d/$2"
until [[ ${d%%*/*} ]] || [[ -z "$d" ]]
do
d=${d%/*}
[[ -f "$d/$2" ]] && echo "$d/$2"
done
Usage rfind path filename
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f127537%2ffind-command-for-lookup-up-a-directory-tree%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
find_backwards () {
test $# -ne 1 && return 2
if [ -z "$find_backwards_recurse" ]; then
pushd . &>/dev/null
find_backwards_recurse=yes
fi
if [ -f "$1" ]; then
echo "${PWD}/${1}"
popd &>/dev/null
find_backwards_recurse=
return 0
else
if [ / != "$PWD" ]; then
cd ..
find_backwards "$1"
else
popd &>/dev/null
find_backwards_recurse=
return 1
fi
fi
}
start cmd:> pwd
/home/hl/tmp/tmp
start cmd:> find_backwards foo
/home/hl/tmp/foo
add a comment |
find_backwards () {
test $# -ne 1 && return 2
if [ -z "$find_backwards_recurse" ]; then
pushd . &>/dev/null
find_backwards_recurse=yes
fi
if [ -f "$1" ]; then
echo "${PWD}/${1}"
popd &>/dev/null
find_backwards_recurse=
return 0
else
if [ / != "$PWD" ]; then
cd ..
find_backwards "$1"
else
popd &>/dev/null
find_backwards_recurse=
return 1
fi
fi
}
start cmd:> pwd
/home/hl/tmp/tmp
start cmd:> find_backwards foo
/home/hl/tmp/foo
add a comment |
find_backwards () {
test $# -ne 1 && return 2
if [ -z "$find_backwards_recurse" ]; then
pushd . &>/dev/null
find_backwards_recurse=yes
fi
if [ -f "$1" ]; then
echo "${PWD}/${1}"
popd &>/dev/null
find_backwards_recurse=
return 0
else
if [ / != "$PWD" ]; then
cd ..
find_backwards "$1"
else
popd &>/dev/null
find_backwards_recurse=
return 1
fi
fi
}
start cmd:> pwd
/home/hl/tmp/tmp
start cmd:> find_backwards foo
/home/hl/tmp/foo
find_backwards () {
test $# -ne 1 && return 2
if [ -z "$find_backwards_recurse" ]; then
pushd . &>/dev/null
find_backwards_recurse=yes
fi
if [ -f "$1" ]; then
echo "${PWD}/${1}"
popd &>/dev/null
find_backwards_recurse=
return 0
else
if [ / != "$PWD" ]; then
cd ..
find_backwards "$1"
else
popd &>/dev/null
find_backwards_recurse=
return 1
fi
fi
}
start cmd:> pwd
/home/hl/tmp/tmp
start cmd:> find_backwards foo
/home/hl/tmp/foo
answered May 2 '14 at 11:15
Hauke LagingHauke Laging
56.5k1285135
56.5k1285135
add a comment |
add a comment |
I don't know any command who would do that.
One possible script implementation.
rfind:
#!/bin/bash
d=$1
[[ -f "$d/$2" ]] && echo "$d/$2"
until [[ ${d%%*/*} ]] || [[ -z "$d" ]]
do
d=${d%/*}
[[ -f "$d/$2" ]] && echo "$d/$2"
done
Usage rfind path filename
add a comment |
I don't know any command who would do that.
One possible script implementation.
rfind:
#!/bin/bash
d=$1
[[ -f "$d/$2" ]] && echo "$d/$2"
until [[ ${d%%*/*} ]] || [[ -z "$d" ]]
do
d=${d%/*}
[[ -f "$d/$2" ]] && echo "$d/$2"
done
Usage rfind path filename
add a comment |
I don't know any command who would do that.
One possible script implementation.
rfind:
#!/bin/bash
d=$1
[[ -f "$d/$2" ]] && echo "$d/$2"
until [[ ${d%%*/*} ]] || [[ -z "$d" ]]
do
d=${d%/*}
[[ -f "$d/$2" ]] && echo "$d/$2"
done
Usage rfind path filename
I don't know any command who would do that.
One possible script implementation.
rfind:
#!/bin/bash
d=$1
[[ -f "$d/$2" ]] && echo "$d/$2"
until [[ ${d%%*/*} ]] || [[ -z "$d" ]]
do
d=${d%/*}
[[ -f "$d/$2" ]] && echo "$d/$2"
done
Usage rfind path filename
edited May 2 '14 at 12:53
answered May 2 '14 at 12:30
EmmanuelEmmanuel
3,03411120
3,03411120
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f127537%2ffind-command-for-lookup-up-a-directory-tree%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Have you tried find with the
-depth
option?-depth Process each directory's contents before the directory itself.
.– Zoredache
Apr 29 '14 at 5:54
As far as I can tell, the
-depth
switch just changes thefind
's mode of operation in terms of which directories it looks at first. It doesn't appear to makefind
look upwards through the directory hierarchy.– Bryan Kyle
Apr 29 '14 at 16:25
What exactly do you mean by upwards? When doing a tree search you either do depth first, or breadth first. A depth first search should return results like you describe above.
– Zoredache
Apr 29 '14 at 16:36
find
looks downwards through the directory structure, towards the leaves of the file system. I want to look upwards, meaning towards the root of the file system. See the list in my original question. Notice that it starts with a longer path (deeper in the file system) and moves towards towards short paths (shallower in the file system).– Bryan Kyle
Apr 30 '14 at 15:38
And how do you think a command could actually acomplish that? The is no magical way for a command to know the deepest folder. Are you going to pass the starting folder on the command line, or?
– Zoredache
Apr 30 '14 at 15:48