Skip to content

ls Command Regular Expression

  • Linux

ls Command

We have tried several ways to list files by the pattern of regular expression like this:

$ ls -l erptbs_(16|20|40|42|86)_*
ksh: syntax error: `(' unexpected

It seems that ls command cannot recognize round brackets, so we enclose the pattern string in a pair of double quotation marks.

$ ls -l "erptbs_(16|20|40|42|86)_*"
erptbs_(16|20|40|41|42|86)_* not found

It looks like the pattern of regular express is ignored in quotations.

Solution

This is because ls command adopts Glob patterns to match filenames, not regular expression, so it cannot recognize round brackets and pipes.

To overcome this, we use egrep command to retrieve them.

$ ls -l | egrep "erptbs_(16|20|40|42|8)_*"
-rw-r-----    1 oracle   oinstall   10493952 Sep 30 10:27 erptbs_16_01.dbf
-rw-r-----    1 oracle   oinstall   10493952 Sep 30 10:27 erptbs_20_01.dbf
-rw-r-----    1 oracle   oinstall   10493952 Sep 30 10:27 erptbs_40_01.dbf
-rw-r-----    1 oracle   oinstall   10493952 Sep 30 10:27 erptbs_42_01.dbf
-rw-r-----    1 oracle   oinstall   10493952 Sep 30 10:27 erptbs_86_01.dbf
-rw-r-----    1 oracle   oinstall   10493952 Sep 30 10:27 erptbs_86_02.dbf

We found them.

Leave a Reply

Your email address will not be published. Required fields are marked *