Difference between * and ?* in Bash [closed]
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgO9GURib1T8z7lCwjOGLQaGtrueEthgQ8LO42ZX8cOfTqDK4jvDDpKkLFwf2J49kYCMNW7d4ABih_XCb_2UXdq5fPJDkoyg7-8g_YfRUot-XnaXkNYycsNp7lA5_TW9td0FFpLQ2APzKcZ/s1600/1.jpg)
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYQ0N5W1qAOxLP7t7iOM6O6AzbZnkXUy16s7P_CWfOb5UbTQY_aDsc727chyphenhyphen5W4IppVNernMMQeaUFTB_rFzAd95_CDt-tnwN-nBx6JyUp2duGjPaL5-VgNO41AVsA_vu30EJcipdDG409/s400/Clash+Royale+CLAN+TAG%2523URR8PPP.png)
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.
bash wildcards
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.
add a comment |Â
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.
bash wildcards
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 runninggrep
with a regular expression, orfind
with a filename pattern, or as a wildcard.
â Jeff Schaller
Aug 8 at 11:07
add a comment |Â
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.
bash wildcards
*
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.
bash wildcards
edited Aug 8 at 5:26
![](https://i.stack.imgur.com/AjIZY.jpg?s=32&g=1)
![](https://i.stack.imgur.com/AjIZY.jpg?s=32&g=1)
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 runninggrep
with a regular expression, orfind
with a filename pattern, or as a wildcard.
â Jeff Schaller
Aug 8 at 11:07
add a comment |Â
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 runninggrep
with a regular expression, orfind
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
add a comment |Â
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.
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
tols
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-filesdu -scm .??*
vsdu -scm .*
.
â jimmij
Aug 8 at 13:17
add a comment |Â
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.
add a comment |Â
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.
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
tols
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-filesdu -scm .??*
vsdu -scm .*
.
â jimmij
Aug 8 at 13:17
add a comment |Â
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.
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
tols
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-filesdu -scm .??*
vsdu -scm .*
.
â jimmij
Aug 8 at 13:17
add a comment |Â
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.
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.
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
tols
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-filesdu -scm .??*
vsdu -scm .*
.
â jimmij
Aug 8 at 13:17
add a comment |Â
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
tols
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-filesdu -scm .??*
vsdu -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
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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.
answered Aug 7 at 21:12
RalfFriedl
2,7251419
2,7251419
add a comment |Â
add a comment |Â
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, orfind
with a filename pattern, or as a wildcard.â Jeff Schaller
Aug 8 at 11:07