How to find only directories without subdirectories? [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












6
















This question already has an answer here:



  • Find directories that do not contain subdirectories

    4 answers



Is there a way in linux to look through a directory tree for only those directories that are the ends of branches (I will call them leaves here), i.e., dircetories with no subdirectories in them? I looked at this question but it was never properly answered.



So if I have a directory tree



root/
├── branch1
│   ├── branch11
│   │   └── branch111 *
│   └── branch12 *
└── branch2
├── branch21 *
└── branch22
└── branch221 *


can I find only the directories that are the end of their branch (the ones marked with*), so looking only at the number of directories, not at the number of files? In my real case I am looking for the ones with files, but they're a subset of the 'leaves' that I want to find in this example.










share|improve this question















marked as duplicate by Peter Cordes, Community Jan 29 at 12:39


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • please clarify: "no matter how many files they contain" vs " I am looking for the ones with files"

    – RoVo
    Jan 28 at 13:23











  • Sorry. That comment was because of the previous post, where the answer with the most votes looked for empty directories vs directories with subdirectories.

    – alle_meije
    Jan 28 at 13:30











  • so you explicitly want to look for not (!) empty directories ? Then you should accept Bodo's answer.

    – RoVo
    Jan 28 at 14:00











  • No, I mean that I am looking for directories that don't contain subdirectories, irrespective of whether or not they contain files.

    – alle_meije
    Jan 28 at 14:38






  • 2





    but isn't that exactly what the most upvoted answer from your linked question does.

    – RoVo
    Jan 28 at 14:41
















6
















This question already has an answer here:



  • Find directories that do not contain subdirectories

    4 answers



Is there a way in linux to look through a directory tree for only those directories that are the ends of branches (I will call them leaves here), i.e., dircetories with no subdirectories in them? I looked at this question but it was never properly answered.



So if I have a directory tree



root/
├── branch1
│   ├── branch11
│   │   └── branch111 *
│   └── branch12 *
└── branch2
├── branch21 *
└── branch22
└── branch221 *


can I find only the directories that are the end of their branch (the ones marked with*), so looking only at the number of directories, not at the number of files? In my real case I am looking for the ones with files, but they're a subset of the 'leaves' that I want to find in this example.










share|improve this question















marked as duplicate by Peter Cordes, Community Jan 29 at 12:39


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • please clarify: "no matter how many files they contain" vs " I am looking for the ones with files"

    – RoVo
    Jan 28 at 13:23











  • Sorry. That comment was because of the previous post, where the answer with the most votes looked for empty directories vs directories with subdirectories.

    – alle_meije
    Jan 28 at 13:30











  • so you explicitly want to look for not (!) empty directories ? Then you should accept Bodo's answer.

    – RoVo
    Jan 28 at 14:00











  • No, I mean that I am looking for directories that don't contain subdirectories, irrespective of whether or not they contain files.

    – alle_meije
    Jan 28 at 14:38






  • 2





    but isn't that exactly what the most upvoted answer from your linked question does.

    – RoVo
    Jan 28 at 14:41














6












6








6









This question already has an answer here:



  • Find directories that do not contain subdirectories

    4 answers



Is there a way in linux to look through a directory tree for only those directories that are the ends of branches (I will call them leaves here), i.e., dircetories with no subdirectories in them? I looked at this question but it was never properly answered.



So if I have a directory tree



root/
├── branch1
│   ├── branch11
│   │   └── branch111 *
│   └── branch12 *
└── branch2
├── branch21 *
└── branch22
└── branch221 *


can I find only the directories that are the end of their branch (the ones marked with*), so looking only at the number of directories, not at the number of files? In my real case I am looking for the ones with files, but they're a subset of the 'leaves' that I want to find in this example.










share|improve this question

















This question already has an answer here:



  • Find directories that do not contain subdirectories

    4 answers



Is there a way in linux to look through a directory tree for only those directories that are the ends of branches (I will call them leaves here), i.e., dircetories with no subdirectories in them? I looked at this question but it was never properly answered.



So if I have a directory tree



root/
├── branch1
│   ├── branch11
│   │   └── branch111 *
│   └── branch12 *
└── branch2
├── branch21 *
└── branch22
└── branch221 *


can I find only the directories that are the end of their branch (the ones marked with*), so looking only at the number of directories, not at the number of files? In my real case I am looking for the ones with files, but they're a subset of the 'leaves' that I want to find in this example.





This question already has an answer here:



  • Find directories that do not contain subdirectories

    4 answers







linux shell-script find ls directory-structure






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 28 at 19:55









K7AAY

612624




612624










asked Jan 28 at 12:42









alle_meijealle_meije

198117




198117




marked as duplicate by Peter Cordes, Community Jan 29 at 12:39


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Peter Cordes, Community Jan 29 at 12:39


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • please clarify: "no matter how many files they contain" vs " I am looking for the ones with files"

    – RoVo
    Jan 28 at 13:23











  • Sorry. That comment was because of the previous post, where the answer with the most votes looked for empty directories vs directories with subdirectories.

    – alle_meije
    Jan 28 at 13:30











  • so you explicitly want to look for not (!) empty directories ? Then you should accept Bodo's answer.

    – RoVo
    Jan 28 at 14:00











  • No, I mean that I am looking for directories that don't contain subdirectories, irrespective of whether or not they contain files.

    – alle_meije
    Jan 28 at 14:38






  • 2





    but isn't that exactly what the most upvoted answer from your linked question does.

    – RoVo
    Jan 28 at 14:41


















  • please clarify: "no matter how many files they contain" vs " I am looking for the ones with files"

    – RoVo
    Jan 28 at 13:23











  • Sorry. That comment was because of the previous post, where the answer with the most votes looked for empty directories vs directories with subdirectories.

    – alle_meije
    Jan 28 at 13:30











  • so you explicitly want to look for not (!) empty directories ? Then you should accept Bodo's answer.

    – RoVo
    Jan 28 at 14:00











  • No, I mean that I am looking for directories that don't contain subdirectories, irrespective of whether or not they contain files.

    – alle_meije
    Jan 28 at 14:38






  • 2





    but isn't that exactly what the most upvoted answer from your linked question does.

    – RoVo
    Jan 28 at 14:41

















please clarify: "no matter how many files they contain" vs " I am looking for the ones with files"

– RoVo
Jan 28 at 13:23





please clarify: "no matter how many files they contain" vs " I am looking for the ones with files"

– RoVo
Jan 28 at 13:23













Sorry. That comment was because of the previous post, where the answer with the most votes looked for empty directories vs directories with subdirectories.

– alle_meije
Jan 28 at 13:30





Sorry. That comment was because of the previous post, where the answer with the most votes looked for empty directories vs directories with subdirectories.

– alle_meije
Jan 28 at 13:30













so you explicitly want to look for not (!) empty directories ? Then you should accept Bodo's answer.

– RoVo
Jan 28 at 14:00





so you explicitly want to look for not (!) empty directories ? Then you should accept Bodo's answer.

– RoVo
Jan 28 at 14:00













No, I mean that I am looking for directories that don't contain subdirectories, irrespective of whether or not they contain files.

– alle_meije
Jan 28 at 14:38





No, I mean that I am looking for directories that don't contain subdirectories, irrespective of whether or not they contain files.

– alle_meije
Jan 28 at 14:38




2




2





but isn't that exactly what the most upvoted answer from your linked question does.

– RoVo
Jan 28 at 14:41






but isn't that exactly what the most upvoted answer from your linked question does.

– RoVo
Jan 28 at 14:41











4 Answers
4






active

oldest

votes


















9














To find only those leaf directories that contain files, you can combine an answer of the referenced question https://unix.stackexchange.com/a/203991/330217 or similar questions https://stackoverflow.com/a/4269862/10622916 or https://serverfault.com/a/530328 with find's ! -empty



find rootdir -type d -links 2 ! -empty


Checking the hard links with -links 2 should work for traditional UNIX file systems. The -empty condition is not part of the POSIX standard, but should be available on most Linux systems.



According to KamilMaciorowski's comment the traditional link count semantics for directories is not valid for Btrfs. This is confirmed in https://linux-btrfs.vger.kernel.narkive.com/oAoDX89D/btrfs-st-nlink-for-directories which also mentions Mac OS HFS+ as an exception from the traditional behavior. For these file systems a different method is necessary to check for leaf directories.






share|improve this answer




















  • 3





    In Btrfs any directory reports link count of 1. I guess it's not a "normal UNIX file system" then…

    – Kamil Maciorowski
    Jan 28 at 20:13











  • @KamilMaciorowski Thanks for the hint. I added a paragraph about exceptions.

    – Bodo
    Jan 29 at 7:51


















4














You could use nested find and count number of subdirectories:



find . -type d 
( -exec sh -c 'find "$1" -mindepth 1 -maxdepth 1 -type d -print0 | grep -cz "^" >/dev/null 2>&1' _ ; -o -print )





share|improve this answer

























  • Tested on BTRFS, yes this works without relying on link-count=2. But it's not fast or efficient. It spawns sh to run a pipeline for every directory (empty or not) in the tree.

    – Peter Cordes
    Jan 29 at 5:11



















4














With zsh:



leafdirs=(**/*(ND/Fl2))


Sets the $leafdirs array with the list of matching leaf directories.



You can print it one per line with:



printf '%sn' $leafdirs


Or loop over them with:



for dir ($leafdirs) something with $dir


That's more or less a translation of @Bodo's GNU find answer, so like it works only on traditional Unix-like file systems like ext4 that implement . and .. as actual directory entries.




  • **/: any level of subdirectories


  • (NF/Fl2): glob qualifier


  • N: enable nullglob for that one glob: don't fail if there's no match. Instead, result in an empty list


  • D: enable dotglob for that one glob: look inside hidden dirs, and don't skip hidden files


  • /: select files of type directory only (like -type d)


  • F: select full directories: directories containing at least one entry other than . and .. (like GNU find's ! -empty)


  • l2: only dirs with a link count of 2 (like -links 2). One for the dir's entry in its parent directory and one for . in it. Any subdir would add one to the link count because of the .. entry in those.





share|improve this answer
































    4














    If the */ filename globbing pattern expands to something that is not the name of a directory, then the current directory has no (non-hidden) subdirectories.



    With find:



    find root -type d -exec sh -c 'set -- "$1"/*/; [ ! -d "$1" ]' sh ; ! -empty -print


    Note that this would treat a symbolic link to a directory in a leaf directory as a directory since the pattern would traverse the symbolic link.



    The -empty predicate is not standard, but often implemented. Without it, you would do something similar as with detecting subdirectories:



    find root -type d 
    -exec sh -c 'set -- "$1"/*/; [ ! -d "$1" ]' sh ;
    -exec sh -c 'set -- "$1"/*; [ -e "$1" ]' sh ; -print


    Or, a bit more efficiently,



    find root -type d -exec sh -c '
    dir=$1
    set -- "$dir"/*/
    [ -d "$1" ] && exit 1
    set -- "$dir"/*
    [ -e "$1" ]' sh ; -print


    Or, employing the -links predicate that I had forgotten about (thanks Bodo):



    find root -type d 
    -links 2
    -exec sh -c 'set -- "$1"/*; [ -e "$1" ]' sh ; -print





    share|improve this answer































      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      9














      To find only those leaf directories that contain files, you can combine an answer of the referenced question https://unix.stackexchange.com/a/203991/330217 or similar questions https://stackoverflow.com/a/4269862/10622916 or https://serverfault.com/a/530328 with find's ! -empty



      find rootdir -type d -links 2 ! -empty


      Checking the hard links with -links 2 should work for traditional UNIX file systems. The -empty condition is not part of the POSIX standard, but should be available on most Linux systems.



      According to KamilMaciorowski's comment the traditional link count semantics for directories is not valid for Btrfs. This is confirmed in https://linux-btrfs.vger.kernel.narkive.com/oAoDX89D/btrfs-st-nlink-for-directories which also mentions Mac OS HFS+ as an exception from the traditional behavior. For these file systems a different method is necessary to check for leaf directories.






      share|improve this answer




















      • 3





        In Btrfs any directory reports link count of 1. I guess it's not a "normal UNIX file system" then…

        – Kamil Maciorowski
        Jan 28 at 20:13











      • @KamilMaciorowski Thanks for the hint. I added a paragraph about exceptions.

        – Bodo
        Jan 29 at 7:51















      9














      To find only those leaf directories that contain files, you can combine an answer of the referenced question https://unix.stackexchange.com/a/203991/330217 or similar questions https://stackoverflow.com/a/4269862/10622916 or https://serverfault.com/a/530328 with find's ! -empty



      find rootdir -type d -links 2 ! -empty


      Checking the hard links with -links 2 should work for traditional UNIX file systems. The -empty condition is not part of the POSIX standard, but should be available on most Linux systems.



      According to KamilMaciorowski's comment the traditional link count semantics for directories is not valid for Btrfs. This is confirmed in https://linux-btrfs.vger.kernel.narkive.com/oAoDX89D/btrfs-st-nlink-for-directories which also mentions Mac OS HFS+ as an exception from the traditional behavior. For these file systems a different method is necessary to check for leaf directories.






      share|improve this answer




















      • 3





        In Btrfs any directory reports link count of 1. I guess it's not a "normal UNIX file system" then…

        – Kamil Maciorowski
        Jan 28 at 20:13











      • @KamilMaciorowski Thanks for the hint. I added a paragraph about exceptions.

        – Bodo
        Jan 29 at 7:51













      9












      9








      9







      To find only those leaf directories that contain files, you can combine an answer of the referenced question https://unix.stackexchange.com/a/203991/330217 or similar questions https://stackoverflow.com/a/4269862/10622916 or https://serverfault.com/a/530328 with find's ! -empty



      find rootdir -type d -links 2 ! -empty


      Checking the hard links with -links 2 should work for traditional UNIX file systems. The -empty condition is not part of the POSIX standard, but should be available on most Linux systems.



      According to KamilMaciorowski's comment the traditional link count semantics for directories is not valid for Btrfs. This is confirmed in https://linux-btrfs.vger.kernel.narkive.com/oAoDX89D/btrfs-st-nlink-for-directories which also mentions Mac OS HFS+ as an exception from the traditional behavior. For these file systems a different method is necessary to check for leaf directories.






      share|improve this answer















      To find only those leaf directories that contain files, you can combine an answer of the referenced question https://unix.stackexchange.com/a/203991/330217 or similar questions https://stackoverflow.com/a/4269862/10622916 or https://serverfault.com/a/530328 with find's ! -empty



      find rootdir -type d -links 2 ! -empty


      Checking the hard links with -links 2 should work for traditional UNIX file systems. The -empty condition is not part of the POSIX standard, but should be available on most Linux systems.



      According to KamilMaciorowski's comment the traditional link count semantics for directories is not valid for Btrfs. This is confirmed in https://linux-btrfs.vger.kernel.narkive.com/oAoDX89D/btrfs-st-nlink-for-directories which also mentions Mac OS HFS+ as an exception from the traditional behavior. For these file systems a different method is necessary to check for leaf directories.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Jan 29 at 7:49

























      answered Jan 28 at 13:13









      BodoBodo

      1,476212




      1,476212







      • 3





        In Btrfs any directory reports link count of 1. I guess it's not a "normal UNIX file system" then…

        – Kamil Maciorowski
        Jan 28 at 20:13











      • @KamilMaciorowski Thanks for the hint. I added a paragraph about exceptions.

        – Bodo
        Jan 29 at 7:51












      • 3





        In Btrfs any directory reports link count of 1. I guess it's not a "normal UNIX file system" then…

        – Kamil Maciorowski
        Jan 28 at 20:13











      • @KamilMaciorowski Thanks for the hint. I added a paragraph about exceptions.

        – Bodo
        Jan 29 at 7:51







      3




      3





      In Btrfs any directory reports link count of 1. I guess it's not a "normal UNIX file system" then…

      – Kamil Maciorowski
      Jan 28 at 20:13





      In Btrfs any directory reports link count of 1. I guess it's not a "normal UNIX file system" then…

      – Kamil Maciorowski
      Jan 28 at 20:13













      @KamilMaciorowski Thanks for the hint. I added a paragraph about exceptions.

      – Bodo
      Jan 29 at 7:51





      @KamilMaciorowski Thanks for the hint. I added a paragraph about exceptions.

      – Bodo
      Jan 29 at 7:51













      4














      You could use nested find and count number of subdirectories:



      find . -type d 
      ( -exec sh -c 'find "$1" -mindepth 1 -maxdepth 1 -type d -print0 | grep -cz "^" >/dev/null 2>&1' _ ; -o -print )





      share|improve this answer

























      • Tested on BTRFS, yes this works without relying on link-count=2. But it's not fast or efficient. It spawns sh to run a pipeline for every directory (empty or not) in the tree.

        – Peter Cordes
        Jan 29 at 5:11
















      4














      You could use nested find and count number of subdirectories:



      find . -type d 
      ( -exec sh -c 'find "$1" -mindepth 1 -maxdepth 1 -type d -print0 | grep -cz "^" >/dev/null 2>&1' _ ; -o -print )





      share|improve this answer

























      • Tested on BTRFS, yes this works without relying on link-count=2. But it's not fast or efficient. It spawns sh to run a pipeline for every directory (empty or not) in the tree.

        – Peter Cordes
        Jan 29 at 5:11














      4












      4








      4







      You could use nested find and count number of subdirectories:



      find . -type d 
      ( -exec sh -c 'find "$1" -mindepth 1 -maxdepth 1 -type d -print0 | grep -cz "^" >/dev/null 2>&1' _ ; -o -print )





      share|improve this answer















      You could use nested find and count number of subdirectories:



      find . -type d 
      ( -exec sh -c 'find "$1" -mindepth 1 -maxdepth 1 -type d -print0 | grep -cz "^" >/dev/null 2>&1' _ ; -o -print )






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Jan 28 at 14:38

























      answered Jan 28 at 13:01









      RoVoRoVo

      3,141216




      3,141216












      • Tested on BTRFS, yes this works without relying on link-count=2. But it's not fast or efficient. It spawns sh to run a pipeline for every directory (empty or not) in the tree.

        – Peter Cordes
        Jan 29 at 5:11


















      • Tested on BTRFS, yes this works without relying on link-count=2. But it's not fast or efficient. It spawns sh to run a pipeline for every directory (empty or not) in the tree.

        – Peter Cordes
        Jan 29 at 5:11

















      Tested on BTRFS, yes this works without relying on link-count=2. But it's not fast or efficient. It spawns sh to run a pipeline for every directory (empty or not) in the tree.

      – Peter Cordes
      Jan 29 at 5:11






      Tested on BTRFS, yes this works without relying on link-count=2. But it's not fast or efficient. It spawns sh to run a pipeline for every directory (empty or not) in the tree.

      – Peter Cordes
      Jan 29 at 5:11












      4














      With zsh:



      leafdirs=(**/*(ND/Fl2))


      Sets the $leafdirs array with the list of matching leaf directories.



      You can print it one per line with:



      printf '%sn' $leafdirs


      Or loop over them with:



      for dir ($leafdirs) something with $dir


      That's more or less a translation of @Bodo's GNU find answer, so like it works only on traditional Unix-like file systems like ext4 that implement . and .. as actual directory entries.




      • **/: any level of subdirectories


      • (NF/Fl2): glob qualifier


      • N: enable nullglob for that one glob: don't fail if there's no match. Instead, result in an empty list


      • D: enable dotglob for that one glob: look inside hidden dirs, and don't skip hidden files


      • /: select files of type directory only (like -type d)


      • F: select full directories: directories containing at least one entry other than . and .. (like GNU find's ! -empty)


      • l2: only dirs with a link count of 2 (like -links 2). One for the dir's entry in its parent directory and one for . in it. Any subdir would add one to the link count because of the .. entry in those.





      share|improve this answer





























        4














        With zsh:



        leafdirs=(**/*(ND/Fl2))


        Sets the $leafdirs array with the list of matching leaf directories.



        You can print it one per line with:



        printf '%sn' $leafdirs


        Or loop over them with:



        for dir ($leafdirs) something with $dir


        That's more or less a translation of @Bodo's GNU find answer, so like it works only on traditional Unix-like file systems like ext4 that implement . and .. as actual directory entries.




        • **/: any level of subdirectories


        • (NF/Fl2): glob qualifier


        • N: enable nullglob for that one glob: don't fail if there's no match. Instead, result in an empty list


        • D: enable dotglob for that one glob: look inside hidden dirs, and don't skip hidden files


        • /: select files of type directory only (like -type d)


        • F: select full directories: directories containing at least one entry other than . and .. (like GNU find's ! -empty)


        • l2: only dirs with a link count of 2 (like -links 2). One for the dir's entry in its parent directory and one for . in it. Any subdir would add one to the link count because of the .. entry in those.





        share|improve this answer



























          4












          4








          4







          With zsh:



          leafdirs=(**/*(ND/Fl2))


          Sets the $leafdirs array with the list of matching leaf directories.



          You can print it one per line with:



          printf '%sn' $leafdirs


          Or loop over them with:



          for dir ($leafdirs) something with $dir


          That's more or less a translation of @Bodo's GNU find answer, so like it works only on traditional Unix-like file systems like ext4 that implement . and .. as actual directory entries.




          • **/: any level of subdirectories


          • (NF/Fl2): glob qualifier


          • N: enable nullglob for that one glob: don't fail if there's no match. Instead, result in an empty list


          • D: enable dotglob for that one glob: look inside hidden dirs, and don't skip hidden files


          • /: select files of type directory only (like -type d)


          • F: select full directories: directories containing at least one entry other than . and .. (like GNU find's ! -empty)


          • l2: only dirs with a link count of 2 (like -links 2). One for the dir's entry in its parent directory and one for . in it. Any subdir would add one to the link count because of the .. entry in those.





          share|improve this answer















          With zsh:



          leafdirs=(**/*(ND/Fl2))


          Sets the $leafdirs array with the list of matching leaf directories.



          You can print it one per line with:



          printf '%sn' $leafdirs


          Or loop over them with:



          for dir ($leafdirs) something with $dir


          That's more or less a translation of @Bodo's GNU find answer, so like it works only on traditional Unix-like file systems like ext4 that implement . and .. as actual directory entries.




          • **/: any level of subdirectories


          • (NF/Fl2): glob qualifier


          • N: enable nullglob for that one glob: don't fail if there's no match. Instead, result in an empty list


          • D: enable dotglob for that one glob: look inside hidden dirs, and don't skip hidden files


          • /: select files of type directory only (like -type d)


          • F: select full directories: directories containing at least one entry other than . and .. (like GNU find's ! -empty)


          • l2: only dirs with a link count of 2 (like -links 2). One for the dir's entry in its parent directory and one for . in it. Any subdir would add one to the link count because of the .. entry in those.






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 28 at 17:30

























          answered Jan 28 at 17:23









          Stéphane ChazelasStéphane Chazelas

          305k57576930




          305k57576930





















              4














              If the */ filename globbing pattern expands to something that is not the name of a directory, then the current directory has no (non-hidden) subdirectories.



              With find:



              find root -type d -exec sh -c 'set -- "$1"/*/; [ ! -d "$1" ]' sh ; ! -empty -print


              Note that this would treat a symbolic link to a directory in a leaf directory as a directory since the pattern would traverse the symbolic link.



              The -empty predicate is not standard, but often implemented. Without it, you would do something similar as with detecting subdirectories:



              find root -type d 
              -exec sh -c 'set -- "$1"/*/; [ ! -d "$1" ]' sh ;
              -exec sh -c 'set -- "$1"/*; [ -e "$1" ]' sh ; -print


              Or, a bit more efficiently,



              find root -type d -exec sh -c '
              dir=$1
              set -- "$dir"/*/
              [ -d "$1" ] && exit 1
              set -- "$dir"/*
              [ -e "$1" ]' sh ; -print


              Or, employing the -links predicate that I had forgotten about (thanks Bodo):



              find root -type d 
              -links 2
              -exec sh -c 'set -- "$1"/*; [ -e "$1" ]' sh ; -print





              share|improve this answer





























                4














                If the */ filename globbing pattern expands to something that is not the name of a directory, then the current directory has no (non-hidden) subdirectories.



                With find:



                find root -type d -exec sh -c 'set -- "$1"/*/; [ ! -d "$1" ]' sh ; ! -empty -print


                Note that this would treat a symbolic link to a directory in a leaf directory as a directory since the pattern would traverse the symbolic link.



                The -empty predicate is not standard, but often implemented. Without it, you would do something similar as with detecting subdirectories:



                find root -type d 
                -exec sh -c 'set -- "$1"/*/; [ ! -d "$1" ]' sh ;
                -exec sh -c 'set -- "$1"/*; [ -e "$1" ]' sh ; -print


                Or, a bit more efficiently,



                find root -type d -exec sh -c '
                dir=$1
                set -- "$dir"/*/
                [ -d "$1" ] && exit 1
                set -- "$dir"/*
                [ -e "$1" ]' sh ; -print


                Or, employing the -links predicate that I had forgotten about (thanks Bodo):



                find root -type d 
                -links 2
                -exec sh -c 'set -- "$1"/*; [ -e "$1" ]' sh ; -print





                share|improve this answer



























                  4












                  4








                  4







                  If the */ filename globbing pattern expands to something that is not the name of a directory, then the current directory has no (non-hidden) subdirectories.



                  With find:



                  find root -type d -exec sh -c 'set -- "$1"/*/; [ ! -d "$1" ]' sh ; ! -empty -print


                  Note that this would treat a symbolic link to a directory in a leaf directory as a directory since the pattern would traverse the symbolic link.



                  The -empty predicate is not standard, but often implemented. Without it, you would do something similar as with detecting subdirectories:



                  find root -type d 
                  -exec sh -c 'set -- "$1"/*/; [ ! -d "$1" ]' sh ;
                  -exec sh -c 'set -- "$1"/*; [ -e "$1" ]' sh ; -print


                  Or, a bit more efficiently,



                  find root -type d -exec sh -c '
                  dir=$1
                  set -- "$dir"/*/
                  [ -d "$1" ] && exit 1
                  set -- "$dir"/*
                  [ -e "$1" ]' sh ; -print


                  Or, employing the -links predicate that I had forgotten about (thanks Bodo):



                  find root -type d 
                  -links 2
                  -exec sh -c 'set -- "$1"/*; [ -e "$1" ]' sh ; -print





                  share|improve this answer















                  If the */ filename globbing pattern expands to something that is not the name of a directory, then the current directory has no (non-hidden) subdirectories.



                  With find:



                  find root -type d -exec sh -c 'set -- "$1"/*/; [ ! -d "$1" ]' sh ; ! -empty -print


                  Note that this would treat a symbolic link to a directory in a leaf directory as a directory since the pattern would traverse the symbolic link.



                  The -empty predicate is not standard, but often implemented. Without it, you would do something similar as with detecting subdirectories:



                  find root -type d 
                  -exec sh -c 'set -- "$1"/*/; [ ! -d "$1" ]' sh ;
                  -exec sh -c 'set -- "$1"/*; [ -e "$1" ]' sh ; -print


                  Or, a bit more efficiently,



                  find root -type d -exec sh -c '
                  dir=$1
                  set -- "$dir"/*/
                  [ -d "$1" ] && exit 1
                  set -- "$dir"/*
                  [ -e "$1" ]' sh ; -print


                  Or, employing the -links predicate that I had forgotten about (thanks Bodo):



                  find root -type d 
                  -links 2
                  -exec sh -c 'set -- "$1"/*; [ -e "$1" ]' sh ; -print






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jan 28 at 21:36

























                  answered Jan 28 at 13:16









                  KusalanandaKusalananda

                  130k17246406




                  130k17246406












                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Displaying single band from multi-band raster using QGIS

                      How many registers does an x86_64 CPU actually have?