Find all directories which has only one type of file











up vote
-1
down vote

favorite












Given a file type .txt, find all directories that has only that type of file.



For example



a--
b-- 1.txt
c--
|
---- 2.jpg
---- 3.txt


The command should output only b not c.










share|improve this question
























  • Please add an OS tag. For Linux: I'd probably run find, get candidate directories which at least contain this type of file, and post-process the output via a script that checks the "only" condition.
    – dirkt
    Nov 26 at 11:32















up vote
-1
down vote

favorite












Given a file type .txt, find all directories that has only that type of file.



For example



a--
b-- 1.txt
c--
|
---- 2.jpg
---- 3.txt


The command should output only b not c.










share|improve this question
























  • Please add an OS tag. For Linux: I'd probably run find, get candidate directories which at least contain this type of file, and post-process the output via a script that checks the "only" condition.
    – dirkt
    Nov 26 at 11:32













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











Given a file type .txt, find all directories that has only that type of file.



For example



a--
b-- 1.txt
c--
|
---- 2.jpg
---- 3.txt


The command should output only b not c.










share|improve this question















Given a file type .txt, find all directories that has only that type of file.



For example



a--
b-- 1.txt
c--
|
---- 2.jpg
---- 3.txt


The command should output only b not c.







linux operating-systems






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 at 8:50

























asked Nov 26 at 10:20









Jibin

1155




1155












  • Please add an OS tag. For Linux: I'd probably run find, get candidate directories which at least contain this type of file, and post-process the output via a script that checks the "only" condition.
    – dirkt
    Nov 26 at 11:32


















  • Please add an OS tag. For Linux: I'd probably run find, get candidate directories which at least contain this type of file, and post-process the output via a script that checks the "only" condition.
    – dirkt
    Nov 26 at 11:32
















Please add an OS tag. For Linux: I'd probably run find, get candidate directories which at least contain this type of file, and post-process the output via a script that checks the "only" condition.
– dirkt
Nov 26 at 11:32




Please add an OS tag. For Linux: I'd probably run find, get candidate directories which at least contain this type of file, and post-process the output via a script that checks the "only" condition.
– dirkt
Nov 26 at 11:32










1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










find . -type d -execdir sh -c '
[ "$(find "$1" -maxdepth 1 -type f -name "*.txt" -print -quit | wc -l)" -gt 0 ] &&
[ "$(find "$1" -maxdepth 1 -type f ! -name "*.txt" -print -quit | wc -l)" -eq 0 ]
' find-sh {} ; -print


The outer find supplies directories to be examined. Two inner find-s check if there is at least one .txt file and no non-.txt files in the directory. sh shell implements logic with [ … ] and &&.



Notes:





  • -maxdepth is not required by POSIX. For POSIX approach see this question.


  • -quit is not required by POSIX. This action makes find quit as soon as any matching file is reported. It's useful because we need at most one matching file to obtain the result with wc -l and [ … ], so quitting early saves time. Without -quit the whole command will work, it will just be slower when there are many files. Alternatively you can use find … | head -n 1 | wc -l; in this case head will terminate the pipe after the first file found, wc will yield the result right away, but find will only notice the broken pipe when (if) it tries to write yet another line. And it's a trade-off: head may save you some time and resources but (as a separate process) it needs time and resources to be spawned twice in every directory.

  • Paths with newlines (if any) will fool wc -l but it doesn't matter because these extra newlines may add to the count only if the "right" count is non-zero anyway, and we only need to know whether the result is zero or not.






share|improve this answer





















    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "3"
    };
    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',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    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%2fsuperuser.com%2fquestions%2f1378433%2ffind-all-directories-which-has-only-one-type-of-file%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    find . -type d -execdir sh -c '
    [ "$(find "$1" -maxdepth 1 -type f -name "*.txt" -print -quit | wc -l)" -gt 0 ] &&
    [ "$(find "$1" -maxdepth 1 -type f ! -name "*.txt" -print -quit | wc -l)" -eq 0 ]
    ' find-sh {} ; -print


    The outer find supplies directories to be examined. Two inner find-s check if there is at least one .txt file and no non-.txt files in the directory. sh shell implements logic with [ … ] and &&.



    Notes:





    • -maxdepth is not required by POSIX. For POSIX approach see this question.


    • -quit is not required by POSIX. This action makes find quit as soon as any matching file is reported. It's useful because we need at most one matching file to obtain the result with wc -l and [ … ], so quitting early saves time. Without -quit the whole command will work, it will just be slower when there are many files. Alternatively you can use find … | head -n 1 | wc -l; in this case head will terminate the pipe after the first file found, wc will yield the result right away, but find will only notice the broken pipe when (if) it tries to write yet another line. And it's a trade-off: head may save you some time and resources but (as a separate process) it needs time and resources to be spawned twice in every directory.

    • Paths with newlines (if any) will fool wc -l but it doesn't matter because these extra newlines may add to the count only if the "right" count is non-zero anyway, and we only need to know whether the result is zero or not.






    share|improve this answer

























      up vote
      1
      down vote



      accepted










      find . -type d -execdir sh -c '
      [ "$(find "$1" -maxdepth 1 -type f -name "*.txt" -print -quit | wc -l)" -gt 0 ] &&
      [ "$(find "$1" -maxdepth 1 -type f ! -name "*.txt" -print -quit | wc -l)" -eq 0 ]
      ' find-sh {} ; -print


      The outer find supplies directories to be examined. Two inner find-s check if there is at least one .txt file and no non-.txt files in the directory. sh shell implements logic with [ … ] and &&.



      Notes:





      • -maxdepth is not required by POSIX. For POSIX approach see this question.


      • -quit is not required by POSIX. This action makes find quit as soon as any matching file is reported. It's useful because we need at most one matching file to obtain the result with wc -l and [ … ], so quitting early saves time. Without -quit the whole command will work, it will just be slower when there are many files. Alternatively you can use find … | head -n 1 | wc -l; in this case head will terminate the pipe after the first file found, wc will yield the result right away, but find will only notice the broken pipe when (if) it tries to write yet another line. And it's a trade-off: head may save you some time and resources but (as a separate process) it needs time and resources to be spawned twice in every directory.

      • Paths with newlines (if any) will fool wc -l but it doesn't matter because these extra newlines may add to the count only if the "right" count is non-zero anyway, and we only need to know whether the result is zero or not.






      share|improve this answer























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        find . -type d -execdir sh -c '
        [ "$(find "$1" -maxdepth 1 -type f -name "*.txt" -print -quit | wc -l)" -gt 0 ] &&
        [ "$(find "$1" -maxdepth 1 -type f ! -name "*.txt" -print -quit | wc -l)" -eq 0 ]
        ' find-sh {} ; -print


        The outer find supplies directories to be examined. Two inner find-s check if there is at least one .txt file and no non-.txt files in the directory. sh shell implements logic with [ … ] and &&.



        Notes:





        • -maxdepth is not required by POSIX. For POSIX approach see this question.


        • -quit is not required by POSIX. This action makes find quit as soon as any matching file is reported. It's useful because we need at most one matching file to obtain the result with wc -l and [ … ], so quitting early saves time. Without -quit the whole command will work, it will just be slower when there are many files. Alternatively you can use find … | head -n 1 | wc -l; in this case head will terminate the pipe after the first file found, wc will yield the result right away, but find will only notice the broken pipe when (if) it tries to write yet another line. And it's a trade-off: head may save you some time and resources but (as a separate process) it needs time and resources to be spawned twice in every directory.

        • Paths with newlines (if any) will fool wc -l but it doesn't matter because these extra newlines may add to the count only if the "right" count is non-zero anyway, and we only need to know whether the result is zero or not.






        share|improve this answer












        find . -type d -execdir sh -c '
        [ "$(find "$1" -maxdepth 1 -type f -name "*.txt" -print -quit | wc -l)" -gt 0 ] &&
        [ "$(find "$1" -maxdepth 1 -type f ! -name "*.txt" -print -quit | wc -l)" -eq 0 ]
        ' find-sh {} ; -print


        The outer find supplies directories to be examined. Two inner find-s check if there is at least one .txt file and no non-.txt files in the directory. sh shell implements logic with [ … ] and &&.



        Notes:





        • -maxdepth is not required by POSIX. For POSIX approach see this question.


        • -quit is not required by POSIX. This action makes find quit as soon as any matching file is reported. It's useful because we need at most one matching file to obtain the result with wc -l and [ … ], so quitting early saves time. Without -quit the whole command will work, it will just be slower when there are many files. Alternatively you can use find … | head -n 1 | wc -l; in this case head will terminate the pipe after the first file found, wc will yield the result right away, but find will only notice the broken pipe when (if) it tries to write yet another line. And it's a trade-off: head may save you some time and resources but (as a separate process) it needs time and resources to be spawned twice in every directory.

        • Paths with newlines (if any) will fool wc -l but it doesn't matter because these extra newlines may add to the count only if the "right" count is non-zero anyway, and we only need to know whether the result is zero or not.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 27 at 10:14









        Kamil Maciorowski

        22.9k155072




        22.9k155072






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Super User!


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





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2fsuperuser.com%2fquestions%2f1378433%2ffind-all-directories-which-has-only-one-type-of-file%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

            Probability when a professor distributes a quiz and homework assignment to a class of n students.

            Aardman Animations

            Are they similar matrix