Find all files in directories named fooLimit POSIX find to specific depth?symbolic link to a directory and relative pathRecursive find that does not find hidden files or recurse into hidden dirsHow can I efficiently dereference all symlinks in `find` *output* filenames?Copying Only Directories With FilesIdentify sub-directories that do not contain a specific string in a specific fileFind files in globbed directories excluding some subpathsfind: exclude n different directories and m different files present at any level but include few files from some excluded directoriesHow to use 'find' and 'cpio' to exclude parent directoriesZipping all sub directories

What's the purpose of autocorrelation?

Is there an in-universe reason Harry says this or is this simply a Rowling mistake?

Removing rows containing NA in every column

Exam design: give maximum score per question or not?

Is the name of an interval between two notes unique and absolute?

How does one calculate the distribution of the Matt Colville way of rolling stats?

Why do things cool down?

Minimize taxes now that I earn more

Is there any actual security benefit to restricting foreign IPs?

The relationship of noch nicht and the passive voice

Dear Fellow PSE Users,

Why does Canada require a minimum rate of climb for ultralights of 300 ft/min?

How could artificial intelligence harm us?

Why are two-stroke engines nearly unheard of in aviation?

Manager manipulates my leaves, what's in it for him?

Find all files in directories named foo

Are lay articles good enough to be the main source of information for PhD research?

How do rulers get rich from war?

Should I inform my future product owner that there is a good chance that a team member will leave the company soon?

4h 40m delay caused by aircraft inspection, Norwegian refuses EU 261/2004 compensation because it turned out there was nothing wrong with the aircraft

Can Brexit be undone in an emergency?

I reverse the source code, you negate the input!

Is Zack Morris's 'time stop' ability in "Saved By the Bell" a supernatural ability?

Escape the labyrinth!



Find all files in directories named foo


Limit POSIX find to specific depth?symbolic link to a directory and relative pathRecursive find that does not find hidden files or recurse into hidden dirsHow can I efficiently dereference all symlinks in `find` *output* filenames?Copying Only Directories With FilesIdentify sub-directories that do not contain a specific string in a specific fileFind files in globbed directories excluding some subpathsfind: exclude n different directories and m different files present at any level but include few files from some excluded directoriesHow to use 'find' and 'cpio' to exclude parent directoriesZipping all sub directories






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I would like to find all files which reside in any directory foo. For example, consider the following files:



foo/w
a/foo/x
a/b/foo/y
a/b/c/foo/z
a/b/c/foo/bar/n


I would like to find the files w,x,y,z and get them listed as above, i.e., with their relative paths. My attempt was something like



$ find . -path '**/foo' -type f


which doesn't work. The search should not include file n, i.e., only files whose parent directory is named foo we are interested in.










share|improve this question
































    1















    I would like to find all files which reside in any directory foo. For example, consider the following files:



    foo/w
    a/foo/x
    a/b/foo/y
    a/b/c/foo/z
    a/b/c/foo/bar/n


    I would like to find the files w,x,y,z and get them listed as above, i.e., with their relative paths. My attempt was something like



    $ find . -path '**/foo' -type f


    which doesn't work. The search should not include file n, i.e., only files whose parent directory is named foo we are interested in.










    share|improve this question




























      1












      1








      1








      I would like to find all files which reside in any directory foo. For example, consider the following files:



      foo/w
      a/foo/x
      a/b/foo/y
      a/b/c/foo/z
      a/b/c/foo/bar/n


      I would like to find the files w,x,y,z and get them listed as above, i.e., with their relative paths. My attempt was something like



      $ find . -path '**/foo' -type f


      which doesn't work. The search should not include file n, i.e., only files whose parent directory is named foo we are interested in.










      share|improve this question
















      I would like to find all files which reside in any directory foo. For example, consider the following files:



      foo/w
      a/foo/x
      a/b/foo/y
      a/b/c/foo/z
      a/b/c/foo/bar/n


      I would like to find the files w,x,y,z and get them listed as above, i.e., with their relative paths. My attempt was something like



      $ find . -path '**/foo' -type f


      which doesn't work. The search should not include file n, i.e., only files whose parent directory is named foo we are interested in.







      find directory






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 8 hours ago







      Max Maier

















      asked 9 hours ago









      Max MaierMax Maier

      1183 bronze badges




      1183 bronze badges























          6 Answers
          6






          active

          oldest

          votes


















          3
















          Not using find, but globbing in the zsh shell:



          $ printf '%sn' **/foo/*(^/)
          a/b/c/foo/z
          a/b/foo/y
          a/foo/x
          foo/w


          This uses the ** glob, which matches "recursively" down into directories, to match any directory named foo in the current directory or below, and then *(^/) to match any file in those foo directories. The (^/) at the end of that is a glob qualifier that restricts the pattern from matching directories (use (.) to match regular files only, or (-.) to also include symbolic links to regular files).



          In bash:



          shopt -s globstar nullglob

          for pathname in **/foo/*; do
          if [[ ! -d "$pathname" ]] || [[ -h "$pathname" ]]; then
          printf '%sn' "$pathname"
          fi
          done


          I set the nullglob option in bash to remove the pattern completely in case it does not match anything. The globstar shell option enables the use of ** in bash (this is enabled by default in zsh).



          Since bash does not have the glob qualifiers of zsh, I'm looping over the pathnames that the pattern matches and test each match to make sure it's not a directory before printing it. Change the "! -d || -h" test to a "-f && ! -h" test to instead pick out only regular files, or just a single "-f" test to also print symbolic links to regular files.






          share|improve this answer



























          • Note that [[ -f file ]] matches on regular files and symlinks to regular files. The zsh qualifier equivalent would be (-.).

            – Stéphane Chazelas
            6 hours ago











          • @StéphaneChazelas Thanks! I just added a note about symbolic links (maybe it would be even better to look for non-directories), and I agree that getting a "no matches found" error may be more informative than getting an empty line.

            – Kusalananda
            5 hours ago



















          2
















          I'm on RHEL 7. This works for me:



          find . -path "*/foo/*" ! -path '*/foo/*/*' -type f


          Note the DOT before the -path. (Or substitute your path there, such as /home/$USER)



          The DOT says "Start looking in the current directory"



          the -path says "Look for anything, followed by a sub-directory named foo, followed by anything" except for directories nested under "foo".



          The -type f says give me only the files in a matching directory.

          Looks like



          -path "*/foo/*" ! -path '*/foo/*/*' 


          Doesn't get everything. Not elegant, but it works:



          find . -path "*/foo/*" -type f | awk -F'/' 'if (match$(NF-1),"foo")) print $0' 





          share|improve this answer



























          • That is pretty close to what I'm locking for. I updated my question in order to be more precise. This solution finds any file whose path contains a directory named foo. However, I'm interested only in files whose parent is named foo.

            – Max Maier
            8 hours ago











          • Ah, sorry I missed that. I'll work on an update.

            – Scottie H
            8 hours ago











          • I think @Jeff added that. Thanks for that!

            – Scottie H
            7 hours ago






          • 3





            Note: a/foo/c/foo/z should be found.

            – Kamil Maciorowski
            7 hours ago











          • Just updated the answer to show that.

            – Scottie H
            5 hours ago


















          1
















          One method is to find all files, then use grep to match only the desired results:



          find . -type f | grep '/foo/[^/]*$'





          share|improve this answer
































            1
















            ** has no special meaning in find patterns. -path '*/foo/*' would find all files under a directory called foo, including files in subdirectories. -path '*/foo/*' ! -path '*/foo/*/*' would exclude files like a/foo/b/foo/c. I don't think you can do this with just one invocation of POSIX find.



            With find implementations that support -regex (GNU, BusyBox, FreeBSD, NetBSD), you can use that to ensure that there's a single / after foo.



            find . -regex '.*/foo/[^/]*' -type f


            Alternatively, you can use find to locate the foo directories and a shell to enumerate files in this directory.



            Another potential approach would be to invoke find again, but I can't find a pure POSIX find solution that actually works. With any POSIX find, invoked again for each foo directory:



            find . -name foo -type d -exec find -type d ! -name foo -prune -o -type f ;


            Beware that this mostly works, but not quite, and it's a little fragile. You need -type d -prune to avoid recursing into subdirectories, but with just -type d -prune, find would stop at the foo directory. ! -name foo does not prune foo/foo, so a file like foo/foo/bar will be reported twice. You can't use -exec in the inner find because its would be interpreted by the outer find. If your find has -maxdepth (which is being considered for inclusion in the next version of POSIX), you can make this reliable, but there's still this limitation against -exec:



            find . -name foo -type d -exec find -maxdepth 1 -type f ;


            With any POSIX find and sh:



            find . -name foo -type d -exec sh -c '
            for x in "$0/"* "$0/".*; do
            if [ -f "$x" ] && ! [ -L "$x" ]; then
            …;
            fi;
            done
            ' ;


            Substitute the code you want to run on the file names for .






            share|improve this answer



























            • In BSD find, -regex does BRE unless you use -E. GNU find does emacs RE by default which has +. Here you could use * instead.

              – Stéphane Chazelas
              6 hours ago


















            0
















            The first thing that comes to mind is:



            find $ROOT_PATH -type d -name foo | xargs -n1 -I find -type f






            share|improve this answer










            New contributor



            erikjwaxx is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.





















            • Unfortunately, this also finds the file n, which should be excluded since it lives in bar, not in foo.

              – Kusalananda
              8 hours ago


















            0
















            Tried with Below approach



            find . -type d -iname "*foo* -exec ls -ltr ; 2>/dev/null| awk '!/^d/||/^l/print $0'





            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/4.0/"u003ecc by-sa 4.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%2f542304%2ffind-all-files-in-directories-named-foo%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              6 Answers
              6






              active

              oldest

              votes








              6 Answers
              6






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              3
















              Not using find, but globbing in the zsh shell:



              $ printf '%sn' **/foo/*(^/)
              a/b/c/foo/z
              a/b/foo/y
              a/foo/x
              foo/w


              This uses the ** glob, which matches "recursively" down into directories, to match any directory named foo in the current directory or below, and then *(^/) to match any file in those foo directories. The (^/) at the end of that is a glob qualifier that restricts the pattern from matching directories (use (.) to match regular files only, or (-.) to also include symbolic links to regular files).



              In bash:



              shopt -s globstar nullglob

              for pathname in **/foo/*; do
              if [[ ! -d "$pathname" ]] || [[ -h "$pathname" ]]; then
              printf '%sn' "$pathname"
              fi
              done


              I set the nullglob option in bash to remove the pattern completely in case it does not match anything. The globstar shell option enables the use of ** in bash (this is enabled by default in zsh).



              Since bash does not have the glob qualifiers of zsh, I'm looping over the pathnames that the pattern matches and test each match to make sure it's not a directory before printing it. Change the "! -d || -h" test to a "-f && ! -h" test to instead pick out only regular files, or just a single "-f" test to also print symbolic links to regular files.






              share|improve this answer



























              • Note that [[ -f file ]] matches on regular files and symlinks to regular files. The zsh qualifier equivalent would be (-.).

                – Stéphane Chazelas
                6 hours ago











              • @StéphaneChazelas Thanks! I just added a note about symbolic links (maybe it would be even better to look for non-directories), and I agree that getting a "no matches found" error may be more informative than getting an empty line.

                – Kusalananda
                5 hours ago
















              3
















              Not using find, but globbing in the zsh shell:



              $ printf '%sn' **/foo/*(^/)
              a/b/c/foo/z
              a/b/foo/y
              a/foo/x
              foo/w


              This uses the ** glob, which matches "recursively" down into directories, to match any directory named foo in the current directory or below, and then *(^/) to match any file in those foo directories. The (^/) at the end of that is a glob qualifier that restricts the pattern from matching directories (use (.) to match regular files only, or (-.) to also include symbolic links to regular files).



              In bash:



              shopt -s globstar nullglob

              for pathname in **/foo/*; do
              if [[ ! -d "$pathname" ]] || [[ -h "$pathname" ]]; then
              printf '%sn' "$pathname"
              fi
              done


              I set the nullglob option in bash to remove the pattern completely in case it does not match anything. The globstar shell option enables the use of ** in bash (this is enabled by default in zsh).



              Since bash does not have the glob qualifiers of zsh, I'm looping over the pathnames that the pattern matches and test each match to make sure it's not a directory before printing it. Change the "! -d || -h" test to a "-f && ! -h" test to instead pick out only regular files, or just a single "-f" test to also print symbolic links to regular files.






              share|improve this answer



























              • Note that [[ -f file ]] matches on regular files and symlinks to regular files. The zsh qualifier equivalent would be (-.).

                – Stéphane Chazelas
                6 hours ago











              • @StéphaneChazelas Thanks! I just added a note about symbolic links (maybe it would be even better to look for non-directories), and I agree that getting a "no matches found" error may be more informative than getting an empty line.

                – Kusalananda
                5 hours ago














              3














              3










              3









              Not using find, but globbing in the zsh shell:



              $ printf '%sn' **/foo/*(^/)
              a/b/c/foo/z
              a/b/foo/y
              a/foo/x
              foo/w


              This uses the ** glob, which matches "recursively" down into directories, to match any directory named foo in the current directory or below, and then *(^/) to match any file in those foo directories. The (^/) at the end of that is a glob qualifier that restricts the pattern from matching directories (use (.) to match regular files only, or (-.) to also include symbolic links to regular files).



              In bash:



              shopt -s globstar nullglob

              for pathname in **/foo/*; do
              if [[ ! -d "$pathname" ]] || [[ -h "$pathname" ]]; then
              printf '%sn' "$pathname"
              fi
              done


              I set the nullglob option in bash to remove the pattern completely in case it does not match anything. The globstar shell option enables the use of ** in bash (this is enabled by default in zsh).



              Since bash does not have the glob qualifiers of zsh, I'm looping over the pathnames that the pattern matches and test each match to make sure it's not a directory before printing it. Change the "! -d || -h" test to a "-f && ! -h" test to instead pick out only regular files, or just a single "-f" test to also print symbolic links to regular files.






              share|improve this answer















              Not using find, but globbing in the zsh shell:



              $ printf '%sn' **/foo/*(^/)
              a/b/c/foo/z
              a/b/foo/y
              a/foo/x
              foo/w


              This uses the ** glob, which matches "recursively" down into directories, to match any directory named foo in the current directory or below, and then *(^/) to match any file in those foo directories. The (^/) at the end of that is a glob qualifier that restricts the pattern from matching directories (use (.) to match regular files only, or (-.) to also include symbolic links to regular files).



              In bash:



              shopt -s globstar nullglob

              for pathname in **/foo/*; do
              if [[ ! -d "$pathname" ]] || [[ -h "$pathname" ]]; then
              printf '%sn' "$pathname"
              fi
              done


              I set the nullglob option in bash to remove the pattern completely in case it does not match anything. The globstar shell option enables the use of ** in bash (this is enabled by default in zsh).



              Since bash does not have the glob qualifiers of zsh, I'm looping over the pathnames that the pattern matches and test each match to make sure it's not a directory before printing it. Change the "! -d || -h" test to a "-f && ! -h" test to instead pick out only regular files, or just a single "-f" test to also print symbolic links to regular files.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 5 hours ago

























              answered 8 hours ago









              KusalanandaKusalananda

              165k19 gold badges322 silver badges511 bronze badges




              165k19 gold badges322 silver badges511 bronze badges















              • Note that [[ -f file ]] matches on regular files and symlinks to regular files. The zsh qualifier equivalent would be (-.).

                – Stéphane Chazelas
                6 hours ago











              • @StéphaneChazelas Thanks! I just added a note about symbolic links (maybe it would be even better to look for non-directories), and I agree that getting a "no matches found" error may be more informative than getting an empty line.

                – Kusalananda
                5 hours ago


















              • Note that [[ -f file ]] matches on regular files and symlinks to regular files. The zsh qualifier equivalent would be (-.).

                – Stéphane Chazelas
                6 hours ago











              • @StéphaneChazelas Thanks! I just added a note about symbolic links (maybe it would be even better to look for non-directories), and I agree that getting a "no matches found" error may be more informative than getting an empty line.

                – Kusalananda
                5 hours ago

















              Note that [[ -f file ]] matches on regular files and symlinks to regular files. The zsh qualifier equivalent would be (-.).

              – Stéphane Chazelas
              6 hours ago





              Note that [[ -f file ]] matches on regular files and symlinks to regular files. The zsh qualifier equivalent would be (-.).

              – Stéphane Chazelas
              6 hours ago













              @StéphaneChazelas Thanks! I just added a note about symbolic links (maybe it would be even better to look for non-directories), and I agree that getting a "no matches found" error may be more informative than getting an empty line.

              – Kusalananda
              5 hours ago






              @StéphaneChazelas Thanks! I just added a note about symbolic links (maybe it would be even better to look for non-directories), and I agree that getting a "no matches found" error may be more informative than getting an empty line.

              – Kusalananda
              5 hours ago














              2
















              I'm on RHEL 7. This works for me:



              find . -path "*/foo/*" ! -path '*/foo/*/*' -type f


              Note the DOT before the -path. (Or substitute your path there, such as /home/$USER)



              The DOT says "Start looking in the current directory"



              the -path says "Look for anything, followed by a sub-directory named foo, followed by anything" except for directories nested under "foo".



              The -type f says give me only the files in a matching directory.

              Looks like



              -path "*/foo/*" ! -path '*/foo/*/*' 


              Doesn't get everything. Not elegant, but it works:



              find . -path "*/foo/*" -type f | awk -F'/' 'if (match$(NF-1),"foo")) print $0' 





              share|improve this answer



























              • That is pretty close to what I'm locking for. I updated my question in order to be more precise. This solution finds any file whose path contains a directory named foo. However, I'm interested only in files whose parent is named foo.

                – Max Maier
                8 hours ago











              • Ah, sorry I missed that. I'll work on an update.

                – Scottie H
                8 hours ago











              • I think @Jeff added that. Thanks for that!

                – Scottie H
                7 hours ago






              • 3





                Note: a/foo/c/foo/z should be found.

                – Kamil Maciorowski
                7 hours ago











              • Just updated the answer to show that.

                – Scottie H
                5 hours ago















              2
















              I'm on RHEL 7. This works for me:



              find . -path "*/foo/*" ! -path '*/foo/*/*' -type f


              Note the DOT before the -path. (Or substitute your path there, such as /home/$USER)



              The DOT says "Start looking in the current directory"



              the -path says "Look for anything, followed by a sub-directory named foo, followed by anything" except for directories nested under "foo".



              The -type f says give me only the files in a matching directory.

              Looks like



              -path "*/foo/*" ! -path '*/foo/*/*' 


              Doesn't get everything. Not elegant, but it works:



              find . -path "*/foo/*" -type f | awk -F'/' 'if (match$(NF-1),"foo")) print $0' 





              share|improve this answer



























              • That is pretty close to what I'm locking for. I updated my question in order to be more precise. This solution finds any file whose path contains a directory named foo. However, I'm interested only in files whose parent is named foo.

                – Max Maier
                8 hours ago











              • Ah, sorry I missed that. I'll work on an update.

                – Scottie H
                8 hours ago











              • I think @Jeff added that. Thanks for that!

                – Scottie H
                7 hours ago






              • 3





                Note: a/foo/c/foo/z should be found.

                – Kamil Maciorowski
                7 hours ago











              • Just updated the answer to show that.

                – Scottie H
                5 hours ago













              2














              2










              2









              I'm on RHEL 7. This works for me:



              find . -path "*/foo/*" ! -path '*/foo/*/*' -type f


              Note the DOT before the -path. (Or substitute your path there, such as /home/$USER)



              The DOT says "Start looking in the current directory"



              the -path says "Look for anything, followed by a sub-directory named foo, followed by anything" except for directories nested under "foo".



              The -type f says give me only the files in a matching directory.

              Looks like



              -path "*/foo/*" ! -path '*/foo/*/*' 


              Doesn't get everything. Not elegant, but it works:



              find . -path "*/foo/*" -type f | awk -F'/' 'if (match$(NF-1),"foo")) print $0' 





              share|improve this answer















              I'm on RHEL 7. This works for me:



              find . -path "*/foo/*" ! -path '*/foo/*/*' -type f


              Note the DOT before the -path. (Or substitute your path there, such as /home/$USER)



              The DOT says "Start looking in the current directory"



              the -path says "Look for anything, followed by a sub-directory named foo, followed by anything" except for directories nested under "foo".



              The -type f says give me only the files in a matching directory.

              Looks like



              -path "*/foo/*" ! -path '*/foo/*/*' 


              Doesn't get everything. Not elegant, but it works:



              find . -path "*/foo/*" -type f | awk -F'/' 'if (match$(NF-1),"foo")) print $0' 






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 5 hours ago

























              answered 9 hours ago









              Scottie HScottie H

              1336 bronze badges




              1336 bronze badges















              • That is pretty close to what I'm locking for. I updated my question in order to be more precise. This solution finds any file whose path contains a directory named foo. However, I'm interested only in files whose parent is named foo.

                – Max Maier
                8 hours ago











              • Ah, sorry I missed that. I'll work on an update.

                – Scottie H
                8 hours ago











              • I think @Jeff added that. Thanks for that!

                – Scottie H
                7 hours ago






              • 3





                Note: a/foo/c/foo/z should be found.

                – Kamil Maciorowski
                7 hours ago











              • Just updated the answer to show that.

                – Scottie H
                5 hours ago

















              • That is pretty close to what I'm locking for. I updated my question in order to be more precise. This solution finds any file whose path contains a directory named foo. However, I'm interested only in files whose parent is named foo.

                – Max Maier
                8 hours ago











              • Ah, sorry I missed that. I'll work on an update.

                – Scottie H
                8 hours ago











              • I think @Jeff added that. Thanks for that!

                – Scottie H
                7 hours ago






              • 3





                Note: a/foo/c/foo/z should be found.

                – Kamil Maciorowski
                7 hours ago











              • Just updated the answer to show that.

                – Scottie H
                5 hours ago
















              That is pretty close to what I'm locking for. I updated my question in order to be more precise. This solution finds any file whose path contains a directory named foo. However, I'm interested only in files whose parent is named foo.

              – Max Maier
              8 hours ago





              That is pretty close to what I'm locking for. I updated my question in order to be more precise. This solution finds any file whose path contains a directory named foo. However, I'm interested only in files whose parent is named foo.

              – Max Maier
              8 hours ago













              Ah, sorry I missed that. I'll work on an update.

              – Scottie H
              8 hours ago





              Ah, sorry I missed that. I'll work on an update.

              – Scottie H
              8 hours ago













              I think @Jeff added that. Thanks for that!

              – Scottie H
              7 hours ago





              I think @Jeff added that. Thanks for that!

              – Scottie H
              7 hours ago




              3




              3





              Note: a/foo/c/foo/z should be found.

              – Kamil Maciorowski
              7 hours ago





              Note: a/foo/c/foo/z should be found.

              – Kamil Maciorowski
              7 hours ago













              Just updated the answer to show that.

              – Scottie H
              5 hours ago





              Just updated the answer to show that.

              – Scottie H
              5 hours ago











              1
















              One method is to find all files, then use grep to match only the desired results:



              find . -type f | grep '/foo/[^/]*$'





              share|improve this answer





























                1
















                One method is to find all files, then use grep to match only the desired results:



                find . -type f | grep '/foo/[^/]*$'





                share|improve this answer



























                  1














                  1










                  1









                  One method is to find all files, then use grep to match only the desired results:



                  find . -type f | grep '/foo/[^/]*$'





                  share|improve this answer













                  One method is to find all files, then use grep to match only the desired results:



                  find . -type f | grep '/foo/[^/]*$'






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 8 hours ago









                  Jim L.Jim L.

                  2,2331 gold badge4 silver badges12 bronze badges




                  2,2331 gold badge4 silver badges12 bronze badges
























                      1
















                      ** has no special meaning in find patterns. -path '*/foo/*' would find all files under a directory called foo, including files in subdirectories. -path '*/foo/*' ! -path '*/foo/*/*' would exclude files like a/foo/b/foo/c. I don't think you can do this with just one invocation of POSIX find.



                      With find implementations that support -regex (GNU, BusyBox, FreeBSD, NetBSD), you can use that to ensure that there's a single / after foo.



                      find . -regex '.*/foo/[^/]*' -type f


                      Alternatively, you can use find to locate the foo directories and a shell to enumerate files in this directory.



                      Another potential approach would be to invoke find again, but I can't find a pure POSIX find solution that actually works. With any POSIX find, invoked again for each foo directory:



                      find . -name foo -type d -exec find -type d ! -name foo -prune -o -type f ;


                      Beware that this mostly works, but not quite, and it's a little fragile. You need -type d -prune to avoid recursing into subdirectories, but with just -type d -prune, find would stop at the foo directory. ! -name foo does not prune foo/foo, so a file like foo/foo/bar will be reported twice. You can't use -exec in the inner find because its would be interpreted by the outer find. If your find has -maxdepth (which is being considered for inclusion in the next version of POSIX), you can make this reliable, but there's still this limitation against -exec:



                      find . -name foo -type d -exec find -maxdepth 1 -type f ;


                      With any POSIX find and sh:



                      find . -name foo -type d -exec sh -c '
                      for x in "$0/"* "$0/".*; do
                      if [ -f "$x" ] && ! [ -L "$x" ]; then
                      …;
                      fi;
                      done
                      ' ;


                      Substitute the code you want to run on the file names for .






                      share|improve this answer



























                      • In BSD find, -regex does BRE unless you use -E. GNU find does emacs RE by default which has +. Here you could use * instead.

                        – Stéphane Chazelas
                        6 hours ago















                      1
















                      ** has no special meaning in find patterns. -path '*/foo/*' would find all files under a directory called foo, including files in subdirectories. -path '*/foo/*' ! -path '*/foo/*/*' would exclude files like a/foo/b/foo/c. I don't think you can do this with just one invocation of POSIX find.



                      With find implementations that support -regex (GNU, BusyBox, FreeBSD, NetBSD), you can use that to ensure that there's a single / after foo.



                      find . -regex '.*/foo/[^/]*' -type f


                      Alternatively, you can use find to locate the foo directories and a shell to enumerate files in this directory.



                      Another potential approach would be to invoke find again, but I can't find a pure POSIX find solution that actually works. With any POSIX find, invoked again for each foo directory:



                      find . -name foo -type d -exec find -type d ! -name foo -prune -o -type f ;


                      Beware that this mostly works, but not quite, and it's a little fragile. You need -type d -prune to avoid recursing into subdirectories, but with just -type d -prune, find would stop at the foo directory. ! -name foo does not prune foo/foo, so a file like foo/foo/bar will be reported twice. You can't use -exec in the inner find because its would be interpreted by the outer find. If your find has -maxdepth (which is being considered for inclusion in the next version of POSIX), you can make this reliable, but there's still this limitation against -exec:



                      find . -name foo -type d -exec find -maxdepth 1 -type f ;


                      With any POSIX find and sh:



                      find . -name foo -type d -exec sh -c '
                      for x in "$0/"* "$0/".*; do
                      if [ -f "$x" ] && ! [ -L "$x" ]; then
                      …;
                      fi;
                      done
                      ' ;


                      Substitute the code you want to run on the file names for .






                      share|improve this answer



























                      • In BSD find, -regex does BRE unless you use -E. GNU find does emacs RE by default which has +. Here you could use * instead.

                        – Stéphane Chazelas
                        6 hours ago













                      1














                      1










                      1









                      ** has no special meaning in find patterns. -path '*/foo/*' would find all files under a directory called foo, including files in subdirectories. -path '*/foo/*' ! -path '*/foo/*/*' would exclude files like a/foo/b/foo/c. I don't think you can do this with just one invocation of POSIX find.



                      With find implementations that support -regex (GNU, BusyBox, FreeBSD, NetBSD), you can use that to ensure that there's a single / after foo.



                      find . -regex '.*/foo/[^/]*' -type f


                      Alternatively, you can use find to locate the foo directories and a shell to enumerate files in this directory.



                      Another potential approach would be to invoke find again, but I can't find a pure POSIX find solution that actually works. With any POSIX find, invoked again for each foo directory:



                      find . -name foo -type d -exec find -type d ! -name foo -prune -o -type f ;


                      Beware that this mostly works, but not quite, and it's a little fragile. You need -type d -prune to avoid recursing into subdirectories, but with just -type d -prune, find would stop at the foo directory. ! -name foo does not prune foo/foo, so a file like foo/foo/bar will be reported twice. You can't use -exec in the inner find because its would be interpreted by the outer find. If your find has -maxdepth (which is being considered for inclusion in the next version of POSIX), you can make this reliable, but there's still this limitation against -exec:



                      find . -name foo -type d -exec find -maxdepth 1 -type f ;


                      With any POSIX find and sh:



                      find . -name foo -type d -exec sh -c '
                      for x in "$0/"* "$0/".*; do
                      if [ -f "$x" ] && ! [ -L "$x" ]; then
                      …;
                      fi;
                      done
                      ' ;


                      Substitute the code you want to run on the file names for .






                      share|improve this answer















                      ** has no special meaning in find patterns. -path '*/foo/*' would find all files under a directory called foo, including files in subdirectories. -path '*/foo/*' ! -path '*/foo/*/*' would exclude files like a/foo/b/foo/c. I don't think you can do this with just one invocation of POSIX find.



                      With find implementations that support -regex (GNU, BusyBox, FreeBSD, NetBSD), you can use that to ensure that there's a single / after foo.



                      find . -regex '.*/foo/[^/]*' -type f


                      Alternatively, you can use find to locate the foo directories and a shell to enumerate files in this directory.



                      Another potential approach would be to invoke find again, but I can't find a pure POSIX find solution that actually works. With any POSIX find, invoked again for each foo directory:



                      find . -name foo -type d -exec find -type d ! -name foo -prune -o -type f ;


                      Beware that this mostly works, but not quite, and it's a little fragile. You need -type d -prune to avoid recursing into subdirectories, but with just -type d -prune, find would stop at the foo directory. ! -name foo does not prune foo/foo, so a file like foo/foo/bar will be reported twice. You can't use -exec in the inner find because its would be interpreted by the outer find. If your find has -maxdepth (which is being considered for inclusion in the next version of POSIX), you can make this reliable, but there's still this limitation against -exec:



                      find . -name foo -type d -exec find -maxdepth 1 -type f ;


                      With any POSIX find and sh:



                      find . -name foo -type d -exec sh -c '
                      for x in "$0/"* "$0/".*; do
                      if [ -f "$x" ] && ! [ -L "$x" ]; then
                      …;
                      fi;
                      done
                      ' ;


                      Substitute the code you want to run on the file names for .







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited 5 hours ago

























                      answered 7 hours ago









                      GillesGilles

                      575k140 gold badges1188 silver badges1700 bronze badges




                      575k140 gold badges1188 silver badges1700 bronze badges















                      • In BSD find, -regex does BRE unless you use -E. GNU find does emacs RE by default which has +. Here you could use * instead.

                        – Stéphane Chazelas
                        6 hours ago

















                      • In BSD find, -regex does BRE unless you use -E. GNU find does emacs RE by default which has +. Here you could use * instead.

                        – Stéphane Chazelas
                        6 hours ago
















                      In BSD find, -regex does BRE unless you use -E. GNU find does emacs RE by default which has +. Here you could use * instead.

                      – Stéphane Chazelas
                      6 hours ago





                      In BSD find, -regex does BRE unless you use -E. GNU find does emacs RE by default which has +. Here you could use * instead.

                      – Stéphane Chazelas
                      6 hours ago











                      0
















                      The first thing that comes to mind is:



                      find $ROOT_PATH -type d -name foo | xargs -n1 -I find -type f






                      share|improve this answer










                      New contributor



                      erikjwaxx is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.





















                      • Unfortunately, this also finds the file n, which should be excluded since it lives in bar, not in foo.

                        – Kusalananda
                        8 hours ago















                      0
















                      The first thing that comes to mind is:



                      find $ROOT_PATH -type d -name foo | xargs -n1 -I find -type f






                      share|improve this answer










                      New contributor



                      erikjwaxx is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.





















                      • Unfortunately, this also finds the file n, which should be excluded since it lives in bar, not in foo.

                        – Kusalananda
                        8 hours ago













                      0














                      0










                      0









                      The first thing that comes to mind is:



                      find $ROOT_PATH -type d -name foo | xargs -n1 -I find -type f






                      share|improve this answer










                      New contributor



                      erikjwaxx is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.









                      The first thing that comes to mind is:



                      find $ROOT_PATH -type d -name foo | xargs -n1 -I find -type f







                      share|improve this answer










                      New contributor



                      erikjwaxx is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.








                      share|improve this answer



                      share|improve this answer








                      edited 8 hours ago









                      Jeff Schaller

                      49.8k11 gold badges73 silver badges165 bronze badges




                      49.8k11 gold badges73 silver badges165 bronze badges






                      New contributor



                      erikjwaxx is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.








                      answered 9 hours ago









                      erikjwaxxerikjwaxx

                      11 bronze badge




                      11 bronze badge




                      New contributor



                      erikjwaxx is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.




                      New contributor




                      erikjwaxx is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.

















                      • Unfortunately, this also finds the file n, which should be excluded since it lives in bar, not in foo.

                        – Kusalananda
                        8 hours ago

















                      • Unfortunately, this also finds the file n, which should be excluded since it lives in bar, not in foo.

                        – Kusalananda
                        8 hours ago
















                      Unfortunately, this also finds the file n, which should be excluded since it lives in bar, not in foo.

                      – Kusalananda
                      8 hours ago





                      Unfortunately, this also finds the file n, which should be excluded since it lives in bar, not in foo.

                      – Kusalananda
                      8 hours ago











                      0
















                      Tried with Below approach



                      find . -type d -iname "*foo* -exec ls -ltr ; 2>/dev/null| awk '!/^d/||/^l/print $0'





                      share|improve this answer





























                        0
















                        Tried with Below approach



                        find . -type d -iname "*foo* -exec ls -ltr ; 2>/dev/null| awk '!/^d/||/^l/print $0'





                        share|improve this answer



























                          0














                          0










                          0









                          Tried with Below approach



                          find . -type d -iname "*foo* -exec ls -ltr ; 2>/dev/null| awk '!/^d/||/^l/print $0'





                          share|improve this answer













                          Tried with Below approach



                          find . -type d -iname "*foo* -exec ls -ltr ; 2>/dev/null| awk '!/^d/||/^l/print $0'






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 22 mins ago









                          Praveen Kumar BSPraveen Kumar BS

                          2,3962 gold badges3 silver badges11 bronze badges




                          2,3962 gold badges3 silver badges11 bronze badges































                              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%2f542304%2ffind-all-files-in-directories-named-foo%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

                              Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

                              Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

                              Ласкавець круглолистий Зміст Опис | Поширення | Галерея | Примітки | Посилання | Навігаційне меню58171138361-22960890446Bupleurum rotundifoliumEuro+Med PlantbasePlants of the World Online — Kew ScienceGermplasm Resources Information Network (GRIN)Ласкавецькн. VI : Літери Ком — Левиправивши або дописавши її