Creating single pattern for matching multiple files.

Hi friends,

I have a some files in a directory. for example

856-abc
856-def
851-abc
945-def
956-abc
852-abc

i want to display only those files whose name starts with 856* 945* and 851* using a single pattern.
i.e

856-abc
856-def
851-abc
945-def

the rest of the two files should not be displayed.
i.e.

956-abc
852-abc

so that if i enter a command like

ls -l $pattern

all the files matching the pattern should be displayed. can anyone write a pattern for these 3 files?

thanks

$ pattern="856* 945* 851*"
$ ls $pattern
851-abc  856-abc  856-def  945-def
1 Like

You can also use extended globbing as described by the bash man page:

  ?(pattern-list)   Matches zero or one occurrence of the given patterns
  *(pattern-list)   Matches zero or more occurrences of the given patterns
  +(pattern-list)   Matches one or more occurrences of the given patterns
  @(pattern-list)   Matches one of the given patterns
  !(pattern-list)   Matches anything except one of the given patterns

First of all, make sure that you enable them before using:

shopt -s extglob

Then use it with ls command:

ls @(856|945|851)*
851-abc  856-abc  856-def  945-def
1 Like