Find directory names with N occurences of a set of characters












0














How to match non-recursively directory names that contain N occurrences of a subset of characters (using find)?



In my specific case, I just want directory names with 6 digits. Here is what worked but is non-specific (it will give me all directory names with digits):



find . -maxdepth 1 -type d -regex './[0-9]+'
ls -d +([0-9])


This worked too, but gave me names of directories beginning with numbers and followed by anything:



find . -maxdepth 1 -type d -name '[0-9]*'


From what I read, I don't know how to make ls match N occurrences because it uses globs and not regular expressions (and I know nothing about globs). And what I can't understand is that non of these worked using find:



find . -maxdepth 1 -type d -regex './[0-9]{6}'
find . -maxdepth 1 -type d -name '[0-9]{6}'
find . -maxdepth 1 -type d -name '[0-9]+'


I have used regular-expressions quite a few times already, and I don't understand what's wrong with my patterns (I'm also surprised I had to specify ./ with -regex )?










share|improve this question





























    0














    How to match non-recursively directory names that contain N occurrences of a subset of characters (using find)?



    In my specific case, I just want directory names with 6 digits. Here is what worked but is non-specific (it will give me all directory names with digits):



    find . -maxdepth 1 -type d -regex './[0-9]+'
    ls -d +([0-9])


    This worked too, but gave me names of directories beginning with numbers and followed by anything:



    find . -maxdepth 1 -type d -name '[0-9]*'


    From what I read, I don't know how to make ls match N occurrences because it uses globs and not regular expressions (and I know nothing about globs). And what I can't understand is that non of these worked using find:



    find . -maxdepth 1 -type d -regex './[0-9]{6}'
    find . -maxdepth 1 -type d -name '[0-9]{6}'
    find . -maxdepth 1 -type d -name '[0-9]+'


    I have used regular-expressions quite a few times already, and I don't understand what's wrong with my patterns (I'm also surprised I had to specify ./ with -regex )?










    share|improve this question



























      0












      0








      0







      How to match non-recursively directory names that contain N occurrences of a subset of characters (using find)?



      In my specific case, I just want directory names with 6 digits. Here is what worked but is non-specific (it will give me all directory names with digits):



      find . -maxdepth 1 -type d -regex './[0-9]+'
      ls -d +([0-9])


      This worked too, but gave me names of directories beginning with numbers and followed by anything:



      find . -maxdepth 1 -type d -name '[0-9]*'


      From what I read, I don't know how to make ls match N occurrences because it uses globs and not regular expressions (and I know nothing about globs). And what I can't understand is that non of these worked using find:



      find . -maxdepth 1 -type d -regex './[0-9]{6}'
      find . -maxdepth 1 -type d -name '[0-9]{6}'
      find . -maxdepth 1 -type d -name '[0-9]+'


      I have used regular-expressions quite a few times already, and I don't understand what's wrong with my patterns (I'm also surprised I had to specify ./ with -regex )?










      share|improve this question















      How to match non-recursively directory names that contain N occurrences of a subset of characters (using find)?



      In my specific case, I just want directory names with 6 digits. Here is what worked but is non-specific (it will give me all directory names with digits):



      find . -maxdepth 1 -type d -regex './[0-9]+'
      ls -d +([0-9])


      This worked too, but gave me names of directories beginning with numbers and followed by anything:



      find . -maxdepth 1 -type d -name '[0-9]*'


      From what I read, I don't know how to make ls match N occurrences because it uses globs and not regular expressions (and I know nothing about globs). And what I can't understand is that non of these worked using find:



      find . -maxdepth 1 -type d -regex './[0-9]{6}'
      find . -maxdepth 1 -type d -name '[0-9]{6}'
      find . -maxdepth 1 -type d -name '[0-9]+'


      I have used regular-expressions quite a few times already, and I don't understand what's wrong with my patterns (I'm also surprised I had to specify ./ with -regex )?







      find regular-expression directory ls






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 16 at 4:22









      Rui F Ribeiro

      38.9k1479129




      38.9k1479129










      asked Jun 21 '15 at 16:05









      Sheljohn

      5663620




      5663620






















          1 Answer
          1






          active

          oldest

          votes


















          4














          The default regex type for GNU find is emacs, which doesn't support intervals. You can specify different regex types, such as posix-egrep, which will solve your issue:



          find . -maxdepth 1 -type d -regextype posix-egrep -regex './[0-9]{6}'





          share|improve this answer





















          • Thank you, I also just found this while browsing SO: stackoverflow.com/questions/6844785/…. Would you know how to do it using -name?
            – Sheljohn
            Jun 21 '15 at 16:12












          • @Sh3ljohn -name '[0-9][0-9][0-9][0-9][0-9][0-9]'. Same thing with ls: ls -d [0-9][0-9][0-9][0-9][0-9][0-9].
            – lcd047
            Jun 21 '15 at 16:20












          • @lcd047 Sarcasm.. right? :D
            – Sheljohn
            Jun 21 '15 at 16:42






          • 2




            @Sh3ljohn Nope, serious answer, sadly. :) That's how glob works. If there's another way to make glob match exactly 6 digits, I'm not aware of it...
            – lcd047
            Jun 21 '15 at 17:30











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "106"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f211159%2ffind-directory-names-with-n-occurences-of-a-set-of-characters%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









          4














          The default regex type for GNU find is emacs, which doesn't support intervals. You can specify different regex types, such as posix-egrep, which will solve your issue:



          find . -maxdepth 1 -type d -regextype posix-egrep -regex './[0-9]{6}'





          share|improve this answer





















          • Thank you, I also just found this while browsing SO: stackoverflow.com/questions/6844785/…. Would you know how to do it using -name?
            – Sheljohn
            Jun 21 '15 at 16:12












          • @Sh3ljohn -name '[0-9][0-9][0-9][0-9][0-9][0-9]'. Same thing with ls: ls -d [0-9][0-9][0-9][0-9][0-9][0-9].
            – lcd047
            Jun 21 '15 at 16:20












          • @lcd047 Sarcasm.. right? :D
            – Sheljohn
            Jun 21 '15 at 16:42






          • 2




            @Sh3ljohn Nope, serious answer, sadly. :) That's how glob works. If there's another way to make glob match exactly 6 digits, I'm not aware of it...
            – lcd047
            Jun 21 '15 at 17:30
















          4














          The default regex type for GNU find is emacs, which doesn't support intervals. You can specify different regex types, such as posix-egrep, which will solve your issue:



          find . -maxdepth 1 -type d -regextype posix-egrep -regex './[0-9]{6}'





          share|improve this answer





















          • Thank you, I also just found this while browsing SO: stackoverflow.com/questions/6844785/…. Would you know how to do it using -name?
            – Sheljohn
            Jun 21 '15 at 16:12












          • @Sh3ljohn -name '[0-9][0-9][0-9][0-9][0-9][0-9]'. Same thing with ls: ls -d [0-9][0-9][0-9][0-9][0-9][0-9].
            – lcd047
            Jun 21 '15 at 16:20












          • @lcd047 Sarcasm.. right? :D
            – Sheljohn
            Jun 21 '15 at 16:42






          • 2




            @Sh3ljohn Nope, serious answer, sadly. :) That's how glob works. If there's another way to make glob match exactly 6 digits, I'm not aware of it...
            – lcd047
            Jun 21 '15 at 17:30














          4












          4








          4






          The default regex type for GNU find is emacs, which doesn't support intervals. You can specify different regex types, such as posix-egrep, which will solve your issue:



          find . -maxdepth 1 -type d -regextype posix-egrep -regex './[0-9]{6}'





          share|improve this answer












          The default regex type for GNU find is emacs, which doesn't support intervals. You can specify different regex types, such as posix-egrep, which will solve your issue:



          find . -maxdepth 1 -type d -regextype posix-egrep -regex './[0-9]{6}'






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jun 21 '15 at 16:11









          jordanm

          30.2k28292




          30.2k28292












          • Thank you, I also just found this while browsing SO: stackoverflow.com/questions/6844785/…. Would you know how to do it using -name?
            – Sheljohn
            Jun 21 '15 at 16:12












          • @Sh3ljohn -name '[0-9][0-9][0-9][0-9][0-9][0-9]'. Same thing with ls: ls -d [0-9][0-9][0-9][0-9][0-9][0-9].
            – lcd047
            Jun 21 '15 at 16:20












          • @lcd047 Sarcasm.. right? :D
            – Sheljohn
            Jun 21 '15 at 16:42






          • 2




            @Sh3ljohn Nope, serious answer, sadly. :) That's how glob works. If there's another way to make glob match exactly 6 digits, I'm not aware of it...
            – lcd047
            Jun 21 '15 at 17:30


















          • Thank you, I also just found this while browsing SO: stackoverflow.com/questions/6844785/…. Would you know how to do it using -name?
            – Sheljohn
            Jun 21 '15 at 16:12












          • @Sh3ljohn -name '[0-9][0-9][0-9][0-9][0-9][0-9]'. Same thing with ls: ls -d [0-9][0-9][0-9][0-9][0-9][0-9].
            – lcd047
            Jun 21 '15 at 16:20












          • @lcd047 Sarcasm.. right? :D
            – Sheljohn
            Jun 21 '15 at 16:42






          • 2




            @Sh3ljohn Nope, serious answer, sadly. :) That's how glob works. If there's another way to make glob match exactly 6 digits, I'm not aware of it...
            – lcd047
            Jun 21 '15 at 17:30
















          Thank you, I also just found this while browsing SO: stackoverflow.com/questions/6844785/…. Would you know how to do it using -name?
          – Sheljohn
          Jun 21 '15 at 16:12






          Thank you, I also just found this while browsing SO: stackoverflow.com/questions/6844785/…. Would you know how to do it using -name?
          – Sheljohn
          Jun 21 '15 at 16:12














          @Sh3ljohn -name '[0-9][0-9][0-9][0-9][0-9][0-9]'. Same thing with ls: ls -d [0-9][0-9][0-9][0-9][0-9][0-9].
          – lcd047
          Jun 21 '15 at 16:20






          @Sh3ljohn -name '[0-9][0-9][0-9][0-9][0-9][0-9]'. Same thing with ls: ls -d [0-9][0-9][0-9][0-9][0-9][0-9].
          – lcd047
          Jun 21 '15 at 16:20














          @lcd047 Sarcasm.. right? :D
          – Sheljohn
          Jun 21 '15 at 16:42




          @lcd047 Sarcasm.. right? :D
          – Sheljohn
          Jun 21 '15 at 16:42




          2




          2




          @Sh3ljohn Nope, serious answer, sadly. :) That's how glob works. If there's another way to make glob match exactly 6 digits, I'm not aware of it...
          – lcd047
          Jun 21 '15 at 17:30




          @Sh3ljohn Nope, serious answer, sadly. :) That's how glob works. If there's another way to make glob match exactly 6 digits, I'm not aware of it...
          – lcd047
          Jun 21 '15 at 17:30


















          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.





          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%2funix.stackexchange.com%2fquestions%2f211159%2ffind-directory-names-with-n-occurences-of-a-set-of-characters%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

          Morgemoulin

          Scott Moir

          Souastre