Difference between * and ?* in Bash [closed]

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








up vote
6
down vote

favorite












* is roughly a wild card character with a unlimited amount of length.
? is roughly a wild card character for one or zero length.



Is there a difference between using * vs ?* when searching for strings in bash? I've seen both used, and can't wrap my head around the differences.







share|improve this question














closed as unclear what you're asking by Michael Homer, SivaPrasath, Jeff Schaller, Jesse_b, steve Aug 8 at 19:11


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 4




    In the context of globbing or regex?
    – llua
    Aug 7 at 21:09






  • 1




    I think this is a globbing question, not a regular-expression question, in which case the tags should change; if it's about Bash regular expressions instead/as well then the text should change to say what it's really asking. It looks like the regular-expressions tag was added in someone's edit so I presume that's the wrong part.
    – Michael Homer
    Aug 8 at 5:24







  • 1




    I've edited accordingly, but please revert & edit to say what you wanted if I got that wrong. There's confusion because "one or zero" is a regex thing, but ?* isn't a useful thing to write in a regular expression.
    – Michael Homer
    Aug 8 at 5:27










  • Can you show an example of this "search for strings in bash"? It could be a incidentally bash while running grep with a regular expression, or find with a filename pattern, or as a wildcard.
    – Jeff Schaller
    Aug 8 at 11:07














up vote
6
down vote

favorite












* is roughly a wild card character with a unlimited amount of length.
? is roughly a wild card character for one or zero length.



Is there a difference between using * vs ?* when searching for strings in bash? I've seen both used, and can't wrap my head around the differences.







share|improve this question














closed as unclear what you're asking by Michael Homer, SivaPrasath, Jeff Schaller, Jesse_b, steve Aug 8 at 19:11


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 4




    In the context of globbing or regex?
    – llua
    Aug 7 at 21:09






  • 1




    I think this is a globbing question, not a regular-expression question, in which case the tags should change; if it's about Bash regular expressions instead/as well then the text should change to say what it's really asking. It looks like the regular-expressions tag was added in someone's edit so I presume that's the wrong part.
    – Michael Homer
    Aug 8 at 5:24







  • 1




    I've edited accordingly, but please revert & edit to say what you wanted if I got that wrong. There's confusion because "one or zero" is a regex thing, but ?* isn't a useful thing to write in a regular expression.
    – Michael Homer
    Aug 8 at 5:27










  • Can you show an example of this "search for strings in bash"? It could be a incidentally bash while running grep with a regular expression, or find with a filename pattern, or as a wildcard.
    – Jeff Schaller
    Aug 8 at 11:07












up vote
6
down vote

favorite









up vote
6
down vote

favorite











* is roughly a wild card character with a unlimited amount of length.
? is roughly a wild card character for one or zero length.



Is there a difference between using * vs ?* when searching for strings in bash? I've seen both used, and can't wrap my head around the differences.







share|improve this question














* is roughly a wild card character with a unlimited amount of length.
? is roughly a wild card character for one or zero length.



Is there a difference between using * vs ?* when searching for strings in bash? I've seen both used, and can't wrap my head around the differences.









share|improve this question













share|improve this question




share|improve this question








edited Aug 8 at 5:26









Michael Homer

42.5k6108148




42.5k6108148










asked Aug 7 at 21:04









TrevorKS

1188




1188




closed as unclear what you're asking by Michael Homer, SivaPrasath, Jeff Schaller, Jesse_b, steve Aug 8 at 19:11


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.






closed as unclear what you're asking by Michael Homer, SivaPrasath, Jeff Schaller, Jesse_b, steve Aug 8 at 19:11


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.









  • 4




    In the context of globbing or regex?
    – llua
    Aug 7 at 21:09






  • 1




    I think this is a globbing question, not a regular-expression question, in which case the tags should change; if it's about Bash regular expressions instead/as well then the text should change to say what it's really asking. It looks like the regular-expressions tag was added in someone's edit so I presume that's the wrong part.
    – Michael Homer
    Aug 8 at 5:24







  • 1




    I've edited accordingly, but please revert & edit to say what you wanted if I got that wrong. There's confusion because "one or zero" is a regex thing, but ?* isn't a useful thing to write in a regular expression.
    – Michael Homer
    Aug 8 at 5:27










  • Can you show an example of this "search for strings in bash"? It could be a incidentally bash while running grep with a regular expression, or find with a filename pattern, or as a wildcard.
    – Jeff Schaller
    Aug 8 at 11:07












  • 4




    In the context of globbing or regex?
    – llua
    Aug 7 at 21:09






  • 1




    I think this is a globbing question, not a regular-expression question, in which case the tags should change; if it's about Bash regular expressions instead/as well then the text should change to say what it's really asking. It looks like the regular-expressions tag was added in someone's edit so I presume that's the wrong part.
    – Michael Homer
    Aug 8 at 5:24







  • 1




    I've edited accordingly, but please revert & edit to say what you wanted if I got that wrong. There's confusion because "one or zero" is a regex thing, but ?* isn't a useful thing to write in a regular expression.
    – Michael Homer
    Aug 8 at 5:27










  • Can you show an example of this "search for strings in bash"? It could be a incidentally bash while running grep with a regular expression, or find with a filename pattern, or as a wildcard.
    – Jeff Schaller
    Aug 8 at 11:07







4




4




In the context of globbing or regex?
– llua
Aug 7 at 21:09




In the context of globbing or regex?
– llua
Aug 7 at 21:09




1




1




I think this is a globbing question, not a regular-expression question, in which case the tags should change; if it's about Bash regular expressions instead/as well then the text should change to say what it's really asking. It looks like the regular-expressions tag was added in someone's edit so I presume that's the wrong part.
– Michael Homer
Aug 8 at 5:24





I think this is a globbing question, not a regular-expression question, in which case the tags should change; if it's about Bash regular expressions instead/as well then the text should change to say what it's really asking. It looks like the regular-expressions tag was added in someone's edit so I presume that's the wrong part.
– Michael Homer
Aug 8 at 5:24





1




1




I've edited accordingly, but please revert & edit to say what you wanted if I got that wrong. There's confusion because "one or zero" is a regex thing, but ?* isn't a useful thing to write in a regular expression.
– Michael Homer
Aug 8 at 5:27




I've edited accordingly, but please revert & edit to say what you wanted if I got that wrong. There's confusion because "one or zero" is a regex thing, but ?* isn't a useful thing to write in a regular expression.
– Michael Homer
Aug 8 at 5:27












Can you show an example of this "search for strings in bash"? It could be a incidentally bash while running grep with a regular expression, or find with a filename pattern, or as a wildcard.
– Jeff Schaller
Aug 8 at 11:07




Can you show an example of this "search for strings in bash"? It could be a incidentally bash while running grep with a regular expression, or find with a filename pattern, or as a wildcard.
– Jeff Schaller
Aug 8 at 11:07










2 Answers
2






active

oldest

votes

















up vote
15
down vote



accepted










The difference is that in bash (as you tagged the question) * matches any string with length zero or more characters, while ?* matches a string with at least 1 character. Consider for example two files: file.txt and xfile.txt and try to list them with ls ?*file.txt or ls *file.txt.



One real case scenario when I use such construct is to list hidden files. Very often I just do



ls .??*


Double question marks are here to prevent listing the current directory . and the parent directory .., like it would be with a simpler form ls .*.




I need to point here that my .??* is not perfect; for example filenames with only two characters, like .f, don't match this pattern. More reliable solution is ls ..?,.[!.]*, but usually that is too much to type for me.






share|improve this answer






















  • find /home/?*/.[!.]?* -maxdepth 0 -perm /g+wx,o+rwx -exec chmod g-wx,o-rwx '' +
    – TrevorKS
    Aug 8 at 1:34










  • That is the command the lead me to this question, same strategy taken to avoid the current and parent directories.
    – TrevorKS
    Aug 8 at 1:35






  • 1




    Just an FYI, if you are using GNU coreutils (and you probably are), you can just pass -a to ls to see all files, or -A to see all files except . and ...
    – Jonathan Callen
    Aug 8 at 7:31










  • @JonathanCallen Yes of course, but for other commands that's not always the case, like for example checking the size of dot-files du -scm .??* vs du -scm .*.
    – jimmij
    Aug 8 at 13:17


















up vote
6
down vote













Where did you get that definition from, of to what kind of expression do you refer?



If we talk about shell expressions, both characters can be used alone, but the definition for ? doesn't match, as then it always matches one character and not one or zero.



If we talk about regular expressions, both are operators that are placed after an expression. So in "?*" would have to be placed after something else.






share|improve this answer



























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    15
    down vote



    accepted










    The difference is that in bash (as you tagged the question) * matches any string with length zero or more characters, while ?* matches a string with at least 1 character. Consider for example two files: file.txt and xfile.txt and try to list them with ls ?*file.txt or ls *file.txt.



    One real case scenario when I use such construct is to list hidden files. Very often I just do



    ls .??*


    Double question marks are here to prevent listing the current directory . and the parent directory .., like it would be with a simpler form ls .*.




    I need to point here that my .??* is not perfect; for example filenames with only two characters, like .f, don't match this pattern. More reliable solution is ls ..?,.[!.]*, but usually that is too much to type for me.






    share|improve this answer






















    • find /home/?*/.[!.]?* -maxdepth 0 -perm /g+wx,o+rwx -exec chmod g-wx,o-rwx '' +
      – TrevorKS
      Aug 8 at 1:34










    • That is the command the lead me to this question, same strategy taken to avoid the current and parent directories.
      – TrevorKS
      Aug 8 at 1:35






    • 1




      Just an FYI, if you are using GNU coreutils (and you probably are), you can just pass -a to ls to see all files, or -A to see all files except . and ...
      – Jonathan Callen
      Aug 8 at 7:31










    • @JonathanCallen Yes of course, but for other commands that's not always the case, like for example checking the size of dot-files du -scm .??* vs du -scm .*.
      – jimmij
      Aug 8 at 13:17















    up vote
    15
    down vote



    accepted










    The difference is that in bash (as you tagged the question) * matches any string with length zero or more characters, while ?* matches a string with at least 1 character. Consider for example two files: file.txt and xfile.txt and try to list them with ls ?*file.txt or ls *file.txt.



    One real case scenario when I use such construct is to list hidden files. Very often I just do



    ls .??*


    Double question marks are here to prevent listing the current directory . and the parent directory .., like it would be with a simpler form ls .*.




    I need to point here that my .??* is not perfect; for example filenames with only two characters, like .f, don't match this pattern. More reliable solution is ls ..?,.[!.]*, but usually that is too much to type for me.






    share|improve this answer






















    • find /home/?*/.[!.]?* -maxdepth 0 -perm /g+wx,o+rwx -exec chmod g-wx,o-rwx '' +
      – TrevorKS
      Aug 8 at 1:34










    • That is the command the lead me to this question, same strategy taken to avoid the current and parent directories.
      – TrevorKS
      Aug 8 at 1:35






    • 1




      Just an FYI, if you are using GNU coreutils (and you probably are), you can just pass -a to ls to see all files, or -A to see all files except . and ...
      – Jonathan Callen
      Aug 8 at 7:31










    • @JonathanCallen Yes of course, but for other commands that's not always the case, like for example checking the size of dot-files du -scm .??* vs du -scm .*.
      – jimmij
      Aug 8 at 13:17













    up vote
    15
    down vote



    accepted







    up vote
    15
    down vote



    accepted






    The difference is that in bash (as you tagged the question) * matches any string with length zero or more characters, while ?* matches a string with at least 1 character. Consider for example two files: file.txt and xfile.txt and try to list them with ls ?*file.txt or ls *file.txt.



    One real case scenario when I use such construct is to list hidden files. Very often I just do



    ls .??*


    Double question marks are here to prevent listing the current directory . and the parent directory .., like it would be with a simpler form ls .*.




    I need to point here that my .??* is not perfect; for example filenames with only two characters, like .f, don't match this pattern. More reliable solution is ls ..?,.[!.]*, but usually that is too much to type for me.






    share|improve this answer














    The difference is that in bash (as you tagged the question) * matches any string with length zero or more characters, while ?* matches a string with at least 1 character. Consider for example two files: file.txt and xfile.txt and try to list them with ls ?*file.txt or ls *file.txt.



    One real case scenario when I use such construct is to list hidden files. Very often I just do



    ls .??*


    Double question marks are here to prevent listing the current directory . and the parent directory .., like it would be with a simpler form ls .*.




    I need to point here that my .??* is not perfect; for example filenames with only two characters, like .f, don't match this pattern. More reliable solution is ls ..?,.[!.]*, but usually that is too much to type for me.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Aug 8 at 20:14

























    answered Aug 7 at 21:12









    jimmij

    28.9k867100




    28.9k867100











    • find /home/?*/.[!.]?* -maxdepth 0 -perm /g+wx,o+rwx -exec chmod g-wx,o-rwx '' +
      – TrevorKS
      Aug 8 at 1:34










    • That is the command the lead me to this question, same strategy taken to avoid the current and parent directories.
      – TrevorKS
      Aug 8 at 1:35






    • 1




      Just an FYI, if you are using GNU coreutils (and you probably are), you can just pass -a to ls to see all files, or -A to see all files except . and ...
      – Jonathan Callen
      Aug 8 at 7:31










    • @JonathanCallen Yes of course, but for other commands that's not always the case, like for example checking the size of dot-files du -scm .??* vs du -scm .*.
      – jimmij
      Aug 8 at 13:17

















    • find /home/?*/.[!.]?* -maxdepth 0 -perm /g+wx,o+rwx -exec chmod g-wx,o-rwx '' +
      – TrevorKS
      Aug 8 at 1:34










    • That is the command the lead me to this question, same strategy taken to avoid the current and parent directories.
      – TrevorKS
      Aug 8 at 1:35






    • 1




      Just an FYI, if you are using GNU coreutils (and you probably are), you can just pass -a to ls to see all files, or -A to see all files except . and ...
      – Jonathan Callen
      Aug 8 at 7:31










    • @JonathanCallen Yes of course, but for other commands that's not always the case, like for example checking the size of dot-files du -scm .??* vs du -scm .*.
      – jimmij
      Aug 8 at 13:17
















    find /home/?*/.[!.]?* -maxdepth 0 -perm /g+wx,o+rwx -exec chmod g-wx,o-rwx '' +
    – TrevorKS
    Aug 8 at 1:34




    find /home/?*/.[!.]?* -maxdepth 0 -perm /g+wx,o+rwx -exec chmod g-wx,o-rwx '' +
    – TrevorKS
    Aug 8 at 1:34












    That is the command the lead me to this question, same strategy taken to avoid the current and parent directories.
    – TrevorKS
    Aug 8 at 1:35




    That is the command the lead me to this question, same strategy taken to avoid the current and parent directories.
    – TrevorKS
    Aug 8 at 1:35




    1




    1




    Just an FYI, if you are using GNU coreutils (and you probably are), you can just pass -a to ls to see all files, or -A to see all files except . and ...
    – Jonathan Callen
    Aug 8 at 7:31




    Just an FYI, if you are using GNU coreutils (and you probably are), you can just pass -a to ls to see all files, or -A to see all files except . and ...
    – Jonathan Callen
    Aug 8 at 7:31












    @JonathanCallen Yes of course, but for other commands that's not always the case, like for example checking the size of dot-files du -scm .??* vs du -scm .*.
    – jimmij
    Aug 8 at 13:17





    @JonathanCallen Yes of course, but for other commands that's not always the case, like for example checking the size of dot-files du -scm .??* vs du -scm .*.
    – jimmij
    Aug 8 at 13:17













    up vote
    6
    down vote













    Where did you get that definition from, of to what kind of expression do you refer?



    If we talk about shell expressions, both characters can be used alone, but the definition for ? doesn't match, as then it always matches one character and not one or zero.



    If we talk about regular expressions, both are operators that are placed after an expression. So in "?*" would have to be placed after something else.






    share|improve this answer
























      up vote
      6
      down vote













      Where did you get that definition from, of to what kind of expression do you refer?



      If we talk about shell expressions, both characters can be used alone, but the definition for ? doesn't match, as then it always matches one character and not one or zero.



      If we talk about regular expressions, both are operators that are placed after an expression. So in "?*" would have to be placed after something else.






      share|improve this answer






















        up vote
        6
        down vote










        up vote
        6
        down vote









        Where did you get that definition from, of to what kind of expression do you refer?



        If we talk about shell expressions, both characters can be used alone, but the definition for ? doesn't match, as then it always matches one character and not one or zero.



        If we talk about regular expressions, both are operators that are placed after an expression. So in "?*" would have to be placed after something else.






        share|improve this answer












        Where did you get that definition from, of to what kind of expression do you refer?



        If we talk about shell expressions, both characters can be used alone, but the definition for ? doesn't match, as then it always matches one character and not one or zero.



        If we talk about regular expressions, both are operators that are placed after an expression. So in "?*" would have to be placed after something else.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Aug 7 at 21:12









        RalfFriedl

        2,7251419




        2,7251419












            Popular posts from this blog

            pylint3 and pip3 broken

            Missing snmpget and snmpwalk

            How to enroll fingerprints to Ubuntu 17.10 with VFS491