Find command for lookup up a directory tree












2















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:




  1. /usr/local/share/bin/foo

  2. /usr/local/share/foo

  3. /usr/local/foo

  4. /usr/foo

  5. /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.










share|improve this question














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 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











  • 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















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:




  1. /usr/local/share/bin/foo

  2. /usr/local/share/foo

  3. /usr/local/foo

  4. /usr/foo

  5. /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.










share|improve this question














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 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











  • 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








2








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:




  1. /usr/local/share/bin/foo

  2. /usr/local/share/foo

  3. /usr/local/foo

  4. /usr/foo

  5. /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.










share|improve this question














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:




  1. /usr/local/share/bin/foo

  2. /usr/local/share/foo

  3. /usr/local/foo

  4. /usr/foo

  5. /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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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











  • 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





    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











  • 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










2 Answers
2






active

oldest

votes


















0














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





share|improve this answer































    0














    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






    share|improve this answer

























      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
      });


      }
      });














      draft saved

      draft discarded


















      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









      0














      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





      share|improve this answer




























        0














        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





        share|improve this answer


























          0












          0








          0







          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





          share|improve this answer













          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






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered May 2 '14 at 11:15









          Hauke LagingHauke Laging

          56.5k1285135




          56.5k1285135

























              0














              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






              share|improve this answer






























                0














                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






                share|improve this answer




























                  0












                  0








                  0







                  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






                  share|improve this answer















                  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







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited May 2 '14 at 12:53

























                  answered May 2 '14 at 12:30









                  EmmanuelEmmanuel

                  3,03411120




                  3,03411120






























                      draft saved

                      draft discarded




















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      Accessing regular linux commands in Huawei's Dopra Linux

                      Can't connect RFCOMM socket: Host is down

                      Kernel panic - not syncing: Fatal Exception in Interrupt