Make 'find -regextype egrep' as alias

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











up vote
1
down vote

favorite












I am just starting learning regex and want to use it instead of others everywhere for practice.



I encounter such a situation when tried to find files with extensions sh or md



$ find . regex ".*.(sh|md)$"
.
./bogus.py
./cofollow.py
./data8.txt
./example.sh
./longest_word_2.sh
./posit_param.sh
./cobroadcast2.py


Unfortunately it output /bogus.py,



I notice the BRE rules and tried escape ()



$ find . -regex ".*.(sh|md)$"
#get nothing return


After series of search, I got -regextype solution Regular Expressions - Finding Files



$ find . -regextype posix-extended -iregex ".*.(sh|md)$"
./example.sh
./longest_word_2.sh
./posit_param.sh

$ find . -regextype egrep -iregex ".*.(sh|md)$"
./example.sh
./longest_word_2.sh
./posit_param.sh
./table_regex_bat.md


Additionally, a nice modular solution



$ find -type f | egrep ".*.(sh|md)$"
./example.sh
./longest_word_2.sh
./posit_param.sh
./table_regex_bat.md


However, there is a shortcut in BSD to accomplish such a task
with a -E predicate.



$ /usr/bin/find -E . -regex ".*.(sh|md)$"
./example.sh
./longest_word_2.sh
./posit_param.sh


I am determined to exclusively take the GNU tool in order to make my codes and skills portable.



So I am starting to alias 'find -regextype egrep`,

Unfortunately find obtain the $1 as path.



How could I solve them problem in a handy way?










share|improve this question









New contributor




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























    up vote
    1
    down vote

    favorite












    I am just starting learning regex and want to use it instead of others everywhere for practice.



    I encounter such a situation when tried to find files with extensions sh or md



    $ find . regex ".*.(sh|md)$"
    .
    ./bogus.py
    ./cofollow.py
    ./data8.txt
    ./example.sh
    ./longest_word_2.sh
    ./posit_param.sh
    ./cobroadcast2.py


    Unfortunately it output /bogus.py,



    I notice the BRE rules and tried escape ()



    $ find . -regex ".*.(sh|md)$"
    #get nothing return


    After series of search, I got -regextype solution Regular Expressions - Finding Files



    $ find . -regextype posix-extended -iregex ".*.(sh|md)$"
    ./example.sh
    ./longest_word_2.sh
    ./posit_param.sh

    $ find . -regextype egrep -iregex ".*.(sh|md)$"
    ./example.sh
    ./longest_word_2.sh
    ./posit_param.sh
    ./table_regex_bat.md


    Additionally, a nice modular solution



    $ find -type f | egrep ".*.(sh|md)$"
    ./example.sh
    ./longest_word_2.sh
    ./posit_param.sh
    ./table_regex_bat.md


    However, there is a shortcut in BSD to accomplish such a task
    with a -E predicate.



    $ /usr/bin/find -E . -regex ".*.(sh|md)$"
    ./example.sh
    ./longest_word_2.sh
    ./posit_param.sh


    I am determined to exclusively take the GNU tool in order to make my codes and skills portable.



    So I am starting to alias 'find -regextype egrep`,

    Unfortunately find obtain the $1 as path.



    How could I solve them problem in a handy way?










    share|improve this question









    New contributor




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





















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I am just starting learning regex and want to use it instead of others everywhere for practice.



      I encounter such a situation when tried to find files with extensions sh or md



      $ find . regex ".*.(sh|md)$"
      .
      ./bogus.py
      ./cofollow.py
      ./data8.txt
      ./example.sh
      ./longest_word_2.sh
      ./posit_param.sh
      ./cobroadcast2.py


      Unfortunately it output /bogus.py,



      I notice the BRE rules and tried escape ()



      $ find . -regex ".*.(sh|md)$"
      #get nothing return


      After series of search, I got -regextype solution Regular Expressions - Finding Files



      $ find . -regextype posix-extended -iregex ".*.(sh|md)$"
      ./example.sh
      ./longest_word_2.sh
      ./posit_param.sh

      $ find . -regextype egrep -iregex ".*.(sh|md)$"
      ./example.sh
      ./longest_word_2.sh
      ./posit_param.sh
      ./table_regex_bat.md


      Additionally, a nice modular solution



      $ find -type f | egrep ".*.(sh|md)$"
      ./example.sh
      ./longest_word_2.sh
      ./posit_param.sh
      ./table_regex_bat.md


      However, there is a shortcut in BSD to accomplish such a task
      with a -E predicate.



      $ /usr/bin/find -E . -regex ".*.(sh|md)$"
      ./example.sh
      ./longest_word_2.sh
      ./posit_param.sh


      I am determined to exclusively take the GNU tool in order to make my codes and skills portable.



      So I am starting to alias 'find -regextype egrep`,

      Unfortunately find obtain the $1 as path.



      How could I solve them problem in a handy way?










      share|improve this question









      New contributor




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











      I am just starting learning regex and want to use it instead of others everywhere for practice.



      I encounter such a situation when tried to find files with extensions sh or md



      $ find . regex ".*.(sh|md)$"
      .
      ./bogus.py
      ./cofollow.py
      ./data8.txt
      ./example.sh
      ./longest_word_2.sh
      ./posit_param.sh
      ./cobroadcast2.py


      Unfortunately it output /bogus.py,



      I notice the BRE rules and tried escape ()



      $ find . -regex ".*.(sh|md)$"
      #get nothing return


      After series of search, I got -regextype solution Regular Expressions - Finding Files



      $ find . -regextype posix-extended -iregex ".*.(sh|md)$"
      ./example.sh
      ./longest_word_2.sh
      ./posit_param.sh

      $ find . -regextype egrep -iregex ".*.(sh|md)$"
      ./example.sh
      ./longest_word_2.sh
      ./posit_param.sh
      ./table_regex_bat.md


      Additionally, a nice modular solution



      $ find -type f | egrep ".*.(sh|md)$"
      ./example.sh
      ./longest_word_2.sh
      ./posit_param.sh
      ./table_regex_bat.md


      However, there is a shortcut in BSD to accomplish such a task
      with a -E predicate.



      $ /usr/bin/find -E . -regex ".*.(sh|md)$"
      ./example.sh
      ./longest_word_2.sh
      ./posit_param.sh


      I am determined to exclusively take the GNU tool in order to make my codes and skills portable.



      So I am starting to alias 'find -regextype egrep`,

      Unfortunately find obtain the $1 as path.



      How could I solve them problem in a handy way?







      bash






      share|improve this question









      New contributor




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









      New contributor




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




      share|improve this question








      edited 4 hours ago









      Inian

      3,338822




      3,338822






      New contributor




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









      asked 4 hours ago









      Sawajiri

      1926




      1926




      New contributor




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





      New contributor





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






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




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          Don't use an alias to pass arguments around. They are not portable and useful only on interactive shells. Use a function instead and pass the arguments as paths needed



          regexFind() 
          (( "$#" ))


          and call the function as



          regexFind "/home/foo/bar"



          Also to add to your findings, note that bash also has an intrinsic way to glob files. You just need to enable a couple of extended shell options to make it work. The -s enables the option and -u disables it.



          The nullglob allows to ignore un-expanded glob results as valid matches. So assuming you want to match files ending with *.sh and *.md, you just need to navigate to that particular directory and do



          shopt -s nullglob
          fileList=(*.sh)
          fileList+=(*.md)
          shopt -u nullglob


          and print the results to see below. Remember to quote the expansion to prevent the filenames from undergoing Word-Splitting.



          printf '%sn' "$fileList[@]"





          share|improve this answer





























            up vote
            1
            down vote













            find . -type f ( -name '*.sh' -o -name '*.md' )


            This would work with all implementations of find since it doesn't require support for regular expression matching.



            To make this more flexible:



            suffixfind () (
            dir=$1
            shift

            for suf do
            set -- "$@" -o -name "*.$suf"
            shift
            done
            shift

            find "$dir" -type f ( "$@" )
            )


            This helper shell function (which would work in any sh-like shell) would pick out the first command line argument and put it in the variable dir. Then it would construct a list of -name "*.<suf1>" -o -name "*.<suf2>" (etc.) with all the filename suffixes on the function's command line before calling find with that list to find files in or under $dir.



            You would use it like



            suffixfind /usr sh md txt


            to find all regular files with names ending in .sh, .md or .txt in or under the path /usr.




            About your mentioning of GNU tools and portability: Note that GNU tools on non-Linux systems are sometimes available, but with a g prefix to the tool names. GNU find would therefore be available as gfind to distinguish it from the native find implementation on the system.



            Your "GNU portable" approach would therefore have to test whether gfind was available before testing whether find is in fact GNU find. Not until you've done that (possibly by testing the return status and output of find --version) can you be comfortable in knowing that you are dealing with GNU find.






            share|improve this answer






















            • And a big portability problem is that you cannot e.g. access GNU find under it's native name gfind if you are on Linux. So you are right, the portable way is to use POSIX features only - in special if you can do the job with POSIX features.
              – schily
              23 mins ago











            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',
            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
            );



            );






            Sawajiri is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f479298%2fmake-find-regextype-egrep-as-alias%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            3
            down vote



            accepted










            Don't use an alias to pass arguments around. They are not portable and useful only on interactive shells. Use a function instead and pass the arguments as paths needed



            regexFind() 
            (( "$#" ))


            and call the function as



            regexFind "/home/foo/bar"



            Also to add to your findings, note that bash also has an intrinsic way to glob files. You just need to enable a couple of extended shell options to make it work. The -s enables the option and -u disables it.



            The nullglob allows to ignore un-expanded glob results as valid matches. So assuming you want to match files ending with *.sh and *.md, you just need to navigate to that particular directory and do



            shopt -s nullglob
            fileList=(*.sh)
            fileList+=(*.md)
            shopt -u nullglob


            and print the results to see below. Remember to quote the expansion to prevent the filenames from undergoing Word-Splitting.



            printf '%sn' "$fileList[@]"





            share|improve this answer


























              up vote
              3
              down vote



              accepted










              Don't use an alias to pass arguments around. They are not portable and useful only on interactive shells. Use a function instead and pass the arguments as paths needed



              regexFind() 
              (( "$#" ))


              and call the function as



              regexFind "/home/foo/bar"



              Also to add to your findings, note that bash also has an intrinsic way to glob files. You just need to enable a couple of extended shell options to make it work. The -s enables the option and -u disables it.



              The nullglob allows to ignore un-expanded glob results as valid matches. So assuming you want to match files ending with *.sh and *.md, you just need to navigate to that particular directory and do



              shopt -s nullglob
              fileList=(*.sh)
              fileList+=(*.md)
              shopt -u nullglob


              and print the results to see below. Remember to quote the expansion to prevent the filenames from undergoing Word-Splitting.



              printf '%sn' "$fileList[@]"





              share|improve this answer
























                up vote
                3
                down vote



                accepted







                up vote
                3
                down vote



                accepted






                Don't use an alias to pass arguments around. They are not portable and useful only on interactive shells. Use a function instead and pass the arguments as paths needed



                regexFind() 
                (( "$#" ))


                and call the function as



                regexFind "/home/foo/bar"



                Also to add to your findings, note that bash also has an intrinsic way to glob files. You just need to enable a couple of extended shell options to make it work. The -s enables the option and -u disables it.



                The nullglob allows to ignore un-expanded glob results as valid matches. So assuming you want to match files ending with *.sh and *.md, you just need to navigate to that particular directory and do



                shopt -s nullglob
                fileList=(*.sh)
                fileList+=(*.md)
                shopt -u nullglob


                and print the results to see below. Remember to quote the expansion to prevent the filenames from undergoing Word-Splitting.



                printf '%sn' "$fileList[@]"





                share|improve this answer














                Don't use an alias to pass arguments around. They are not portable and useful only on interactive shells. Use a function instead and pass the arguments as paths needed



                regexFind() 
                (( "$#" ))


                and call the function as



                regexFind "/home/foo/bar"



                Also to add to your findings, note that bash also has an intrinsic way to glob files. You just need to enable a couple of extended shell options to make it work. The -s enables the option and -u disables it.



                The nullglob allows to ignore un-expanded glob results as valid matches. So assuming you want to match files ending with *.sh and *.md, you just need to navigate to that particular directory and do



                shopt -s nullglob
                fileList=(*.sh)
                fileList+=(*.md)
                shopt -u nullglob


                and print the results to see below. Remember to quote the expansion to prevent the filenames from undergoing Word-Splitting.



                printf '%sn' "$fileList[@]"






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 4 hours ago

























                answered 4 hours ago









                Inian

                3,338822




                3,338822






















                    up vote
                    1
                    down vote













                    find . -type f ( -name '*.sh' -o -name '*.md' )


                    This would work with all implementations of find since it doesn't require support for regular expression matching.



                    To make this more flexible:



                    suffixfind () (
                    dir=$1
                    shift

                    for suf do
                    set -- "$@" -o -name "*.$suf"
                    shift
                    done
                    shift

                    find "$dir" -type f ( "$@" )
                    )


                    This helper shell function (which would work in any sh-like shell) would pick out the first command line argument and put it in the variable dir. Then it would construct a list of -name "*.<suf1>" -o -name "*.<suf2>" (etc.) with all the filename suffixes on the function's command line before calling find with that list to find files in or under $dir.



                    You would use it like



                    suffixfind /usr sh md txt


                    to find all regular files with names ending in .sh, .md or .txt in or under the path /usr.




                    About your mentioning of GNU tools and portability: Note that GNU tools on non-Linux systems are sometimes available, but with a g prefix to the tool names. GNU find would therefore be available as gfind to distinguish it from the native find implementation on the system.



                    Your "GNU portable" approach would therefore have to test whether gfind was available before testing whether find is in fact GNU find. Not until you've done that (possibly by testing the return status and output of find --version) can you be comfortable in knowing that you are dealing with GNU find.






                    share|improve this answer






















                    • And a big portability problem is that you cannot e.g. access GNU find under it's native name gfind if you are on Linux. So you are right, the portable way is to use POSIX features only - in special if you can do the job with POSIX features.
                      – schily
                      23 mins ago















                    up vote
                    1
                    down vote













                    find . -type f ( -name '*.sh' -o -name '*.md' )


                    This would work with all implementations of find since it doesn't require support for regular expression matching.



                    To make this more flexible:



                    suffixfind () (
                    dir=$1
                    shift

                    for suf do
                    set -- "$@" -o -name "*.$suf"
                    shift
                    done
                    shift

                    find "$dir" -type f ( "$@" )
                    )


                    This helper shell function (which would work in any sh-like shell) would pick out the first command line argument and put it in the variable dir. Then it would construct a list of -name "*.<suf1>" -o -name "*.<suf2>" (etc.) with all the filename suffixes on the function's command line before calling find with that list to find files in or under $dir.



                    You would use it like



                    suffixfind /usr sh md txt


                    to find all regular files with names ending in .sh, .md or .txt in or under the path /usr.




                    About your mentioning of GNU tools and portability: Note that GNU tools on non-Linux systems are sometimes available, but with a g prefix to the tool names. GNU find would therefore be available as gfind to distinguish it from the native find implementation on the system.



                    Your "GNU portable" approach would therefore have to test whether gfind was available before testing whether find is in fact GNU find. Not until you've done that (possibly by testing the return status and output of find --version) can you be comfortable in knowing that you are dealing with GNU find.






                    share|improve this answer






















                    • And a big portability problem is that you cannot e.g. access GNU find under it's native name gfind if you are on Linux. So you are right, the portable way is to use POSIX features only - in special if you can do the job with POSIX features.
                      – schily
                      23 mins ago













                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    find . -type f ( -name '*.sh' -o -name '*.md' )


                    This would work with all implementations of find since it doesn't require support for regular expression matching.



                    To make this more flexible:



                    suffixfind () (
                    dir=$1
                    shift

                    for suf do
                    set -- "$@" -o -name "*.$suf"
                    shift
                    done
                    shift

                    find "$dir" -type f ( "$@" )
                    )


                    This helper shell function (which would work in any sh-like shell) would pick out the first command line argument and put it in the variable dir. Then it would construct a list of -name "*.<suf1>" -o -name "*.<suf2>" (etc.) with all the filename suffixes on the function's command line before calling find with that list to find files in or under $dir.



                    You would use it like



                    suffixfind /usr sh md txt


                    to find all regular files with names ending in .sh, .md or .txt in or under the path /usr.




                    About your mentioning of GNU tools and portability: Note that GNU tools on non-Linux systems are sometimes available, but with a g prefix to the tool names. GNU find would therefore be available as gfind to distinguish it from the native find implementation on the system.



                    Your "GNU portable" approach would therefore have to test whether gfind was available before testing whether find is in fact GNU find. Not until you've done that (possibly by testing the return status and output of find --version) can you be comfortable in knowing that you are dealing with GNU find.






                    share|improve this answer














                    find . -type f ( -name '*.sh' -o -name '*.md' )


                    This would work with all implementations of find since it doesn't require support for regular expression matching.



                    To make this more flexible:



                    suffixfind () (
                    dir=$1
                    shift

                    for suf do
                    set -- "$@" -o -name "*.$suf"
                    shift
                    done
                    shift

                    find "$dir" -type f ( "$@" )
                    )


                    This helper shell function (which would work in any sh-like shell) would pick out the first command line argument and put it in the variable dir. Then it would construct a list of -name "*.<suf1>" -o -name "*.<suf2>" (etc.) with all the filename suffixes on the function's command line before calling find with that list to find files in or under $dir.



                    You would use it like



                    suffixfind /usr sh md txt


                    to find all regular files with names ending in .sh, .md or .txt in or under the path /usr.




                    About your mentioning of GNU tools and portability: Note that GNU tools on non-Linux systems are sometimes available, but with a g prefix to the tool names. GNU find would therefore be available as gfind to distinguish it from the native find implementation on the system.



                    Your "GNU portable" approach would therefore have to test whether gfind was available before testing whether find is in fact GNU find. Not until you've done that (possibly by testing the return status and output of find --version) can you be comfortable in knowing that you are dealing with GNU find.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 16 mins ago

























                    answered 1 hour ago









                    Kusalananda

                    113k15216344




                    113k15216344











                    • And a big portability problem is that you cannot e.g. access GNU find under it's native name gfind if you are on Linux. So you are right, the portable way is to use POSIX features only - in special if you can do the job with POSIX features.
                      – schily
                      23 mins ago

















                    • And a big portability problem is that you cannot e.g. access GNU find under it's native name gfind if you are on Linux. So you are right, the portable way is to use POSIX features only - in special if you can do the job with POSIX features.
                      – schily
                      23 mins ago
















                    And a big portability problem is that you cannot e.g. access GNU find under it's native name gfind if you are on Linux. So you are right, the portable way is to use POSIX features only - in special if you can do the job with POSIX features.
                    – schily
                    23 mins ago





                    And a big portability problem is that you cannot e.g. access GNU find under it's native name gfind if you are on Linux. So you are right, the portable way is to use POSIX features only - in special if you can do the job with POSIX features.
                    – schily
                    23 mins ago











                    Sawajiri is a new contributor. Be nice, and check out our Code of Conduct.









                     

                    draft saved


                    draft discarded


















                    Sawajiri is a new contributor. Be nice, and check out our Code of Conduct.












                    Sawajiri is a new contributor. Be nice, and check out our Code of Conduct.











                    Sawajiri is a new contributor. Be nice, and check out our Code of Conduct.













                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f479298%2fmake-find-regextype-egrep-as-alias%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    What does second last employer means? [closed]

                    List of Gilmore Girls characters

                    One-line joke