# Grep with wildcard

**URL:** <https://community.unix.com/t/grep-with-wildcard/374405>\
**Category:** Shell Programming and Scripting\
**Created:** [December 21, 2018, 2:24pm UTC](https://community.unix.com/t/grep-with-wildcard/374405 "2018-12-21T14:24:29Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![abdossamad2003](https://community.unix.com/letter_avatar/abdossamad2003/32/6_449c533f586939b6e5f246632a46421f.png) [@abdossamad2003](https://community.unix.com/u/abdossamad2003)\
**Post date:** [December 21, 2018, 2:24pm UTC](https://community.unix.com/t/grep-with-wildcard/374405/1 "2018-12-21T14:24:29Z")

</div>

hi anyone  
how can use grep with wildcard. for example grep "sample?txt" filename doesn't show sample1txt or grep "sample\*txt" filename doesn't show sample123.txt that there is in filename.

many thanks  
samad

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_449c533f586939b6e5f246632a46421f.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [December 21, 2018, 2:35pm UTC](https://community.unix.com/t/grep-with-wildcard/374405/2 "2018-12-21T14:35:05Z")

</div>

grep uses regexes, not globs, with slightly different meanings. In glob, \* means 'zero or more characters', in regex, it means 'zero or more of the previous character'.

So something\* in regex terms would match something, somethingg, somethingggggggggggggggggggggg, somethin, but not somethina.

In regex, ? means "zero or one of the previous character" while . means "any character". You can combine the two as .? to mean "zero or one of any character" for example.

So try `grep 'sample.txt'` to match sampleatxt, samplebtxt, samplectxt, etc.

---

<div class="post-metadata">

**Author:** ![abdossamad2003](https://community.unix.com/letter_avatar/abdossamad2003/32/6_449c533f586939b6e5f246632a46421f.png) [@abdossamad2003](https://community.unix.com/u/abdossamad2003)\
**Post date:** [December 21, 2018, 2:57pm UTC](https://community.unix.com/t/grep-with-wildcard/374405/3 "2018-12-21T14:57:47Z")

</div>

Thanks  
so, What is the way to find the following words in grep:  
e.g: sample123.txt sampleahgdtxt

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [December 21, 2018, 3:14pm UTC](https://community.unix.com/t/grep-with-wildcard/374405/4 "2018-12-21T15:14:12Z")

</div>

With regards to post #1, that would be:

```nohighlight
sample.txt

```

and

```nohighlight
sample.*txt 

```

respectively

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [December 22, 2018, 5:55am UTC](https://community.unix.com/t/grep-with-wildcard/374405/5 "2018-12-22T05:55:27Z")

</div>

> [@corona688](#):
>
> ...  
> So something\* in regex terms would match something, somethingg, somethingggggggggggggggggggggg, somethin, but not somethina.  
> ...

Hmm - it would match "somethina" unless terminated by e.g. line end ` $ ` or something else...

---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/6_449c533f586939b6e5f246632a46421f.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [December 22, 2018, 8:13am UTC](https://community.unix.com/t/grep-with-wildcard/374405/6 "2018-12-22T08:13:29Z")

</div>

Yes it would match the somethin in somethina.  
Often you can improve that with grep -w (word match), then somethina or somethinga or sample.txta or asample.txt would not match.

---

<div class="post-metadata">

**Author:** ![bakunin](https://community.unix.com/letter_avatar/bakunin/32/6_449c533f586939b6e5f246632a46421f.png) [@bakunin](https://community.unix.com/u/bakunin)\
**Post date:** [December 22, 2018, 2:59pm UTC](https://community.unix.com/t/grep-with-wildcard/374405/7 "2018-12-22T14:59:11Z")

</div>

> [@madeingermany](#):
>
> Often you can improve that with grep -w (word match)

That will work only if your `grep` is a non-POSIX `grep` , because this is a (GNU, i believe) non-standard extension ([see here](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/grep.html)).

bakunin

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [December 22, 2018, 6:42pm UTC](https://community.unix.com/t/grep-with-wildcard/374405/8 "2018-12-22T18:42:54Z")

</div>

> [@bakunin](#):
>
> [..] because this is a (GNU, i believe) non-standard extension [..]

GNU, but also BSD grep...

---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/6_449c533f586939b6e5f246632a46421f.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [December 23, 2018, 2:10pm UTC](https://community.unix.com/t/grep-with-wildcard/374405/9 "2018-12-23T14:10:23Z")

</div>

... and SysV. And all derivates i.e. most if not all commercial Unix.

---

<div class="post-metadata">

**Author:** ![abdossamad2003](https://community.unix.com/letter_avatar/abdossamad2003/32/6_449c533f586939b6e5f246632a46421f.png) [@abdossamad2003](https://community.unix.com/u/abdossamad2003)\
**Post date:** [June 21, 2019, 8:14am UTC](https://community.unix.com/t/grep-with-wildcard/374405/10 "2019-06-21T08:14:03Z")

</div>

Many thanks  
how can i find any help for regex e.g in "man command in terminal"?

test is:  
somethin

somethina

somethinabc

something

somethinggg

```nohighlight
grep "something*" test

```

```nohighlight
somethin
somethina
somethinabc
something
somethinggg

```

somethina !?  
somethinabc !?

but

```nohighlight
grep "something?" test

```

no thing find

---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/6_449c533f586939b6e5f246632a46421f.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [June 21, 2019, 8:27am UTC](https://community.unix.com/t/grep-with-wildcard/374405/11 "2019-06-21T08:27:20Z")

</div>

```nohighlight
man regexp

```

gives you a comprehensive description of what is in the libc (that C-compiled programs use).  
The trailing `g*` means " `g` zero or more times" and is useless unless there is a `$` anchor (line end):  
`g*$` forces any number of `g` until the line end.  
The `g?` in the meaning of " `g` zero or one time" is only defined for ERE (extended regular expression), used with egrep or grep -E and awk and most modern languages.

---

<div class="post-metadata">

**Author:** ![abdossamad2003](https://community.unix.com/letter_avatar/abdossamad2003/32/6_449c533f586939b6e5f246632a46421f.png) [@abdossamad2003](https://community.unix.com/u/abdossamad2003)\
**Post date:** [June 24, 2019, 6:10am UTC](https://community.unix.com/t/grep-with-wildcard/374405/12 "2019-06-24T06:10:55Z")

</div>

> [@corona688](#):
>
> grep uses regexes, not globs, with slightly different meanings. In glob, \* means 'zero or more characters', in regex, it means 'zero or more of the previous character'.
> 
> So something\* in regex terms would match something, somethingg, somethingggggggggggggggggggggg, somethin, but not somethina.
> 
> In regex, ? means "zero or one of the previous character" while . means "any character". You can combine the two as .? to mean "zero or one of any character" for example.
> 
> So try `grep 'sample.txt'` to match sampleatxt, samplebtxt, samplectxt, etc.

"grep uses regexes, not globs" is this mean extended regular expression is used by grep?  
and globs means basin regular expression?

many thanks  
samad

--- Post updated at 05:52 AM ---

> [@corona688](#):
>
> grep uses regexes, not globs, with slightly different meanings. In glob, \* means 'zero or more characters', in regex, it means 'zero or more of the previous character'.
> 
> So something\* in regex terms would match something, somethingg, somethingggggggggggggggggggggg, somethin, but not somethina.
> 
> In regex, ? means "zero or one of the previous character" while . means "any character". You can combine the two as .? to mean "zero or one of any character" for example.
> 
> So try `grep 'sample.txt'` to match sampleatxt, samplebtxt, samplectxt, etc.

but something\* match "somethinga" that a in somethinga isn't previous character!

--- Post updated at 06:10 AM ---

> [@corona688](#):
>
> grep uses regexes, not globs, with slightly different meanings. In glob, \* means 'zero or more characters', in regex, it means 'zero or more of the previous character'.
> 
> So something\* in regex terms would match something, somethingg, somethingggggggggggggggggggggg, somethin, but not somethina.
> 
> In regex, ? means "zero or one of the previous character" while . means "any character". You can combine the two as .? to mean "zero or one of any character" for example.
> 
> So try `grep 'sample.txt'` to match sampleatxt, samplebtxt, samplectxt, etc.

you say " but not somethina" this is incorrect because the something\* match somethina.

```plaintext
[samad@localhost test]$ grep "something*" test 
somethina

```

---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/6_449c533f586939b6e5f246632a46421f.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [June 24, 2019, 7:08am UTC](https://community.unix.com/t/grep-with-wildcard/374405/13 "2019-06-24T07:08:34Z")

</div>

As I said already, `grep something*` without an ending anchor is like `grep somethin` because there may be zero `g` s and any characters may follow.  
The shell and find -name use glob expression.  
grep uses basic regular expression.  
egrep (or the equivalent grep -E) use extended regular expression.
