regex quantifiers in bash --simple vs extended matching {n} times

1000 views Asked by At

I'm using the bash shell and trying to list files in a directory whose names match regex patterns. Some of these patterns work, while others don't. For example, the * wildcard is fine:

$ls FILE_*
FILE_123.txt    FILE_2345.txt   FILE_789.txt

And the range pattern captures the first two of these with the following:

$ls FILE_[1-3]*.txt
FILE_123.txt    FILE_2345.txt

As expected. Great. But now I want to count digits:

$ls FILE_[0-9]{3}.txt 
ls: FILE_[0-9]{3}.txt: No such file or directory

Shouldn't this give me the filenames with three numeric digits following "FILE_" (i.e. FILE_123.txt and FILE_789.txt, but not FILE_2345.txt) Can someone tell me how I should be using the {n} quantifier (i.e. "match this pattern n times)?

2

There are 2 answers

1
Thân LƯƠNG Đình On BEST ANSWER

ls uses with glob pattern, you can not use {3}. You have to use FILE_[0-9][0-9][0-9].txt. Or, you could the following command.

ls | grep -E "FILE_[0-9]{3}.txt"

Edit:

Or, you also use find command.

find . -regextype egrep -regex '.*/FILE_[0-9]{3}\.txt'

The .*/ prefix is needed to match a complete path. On Mac OS X :

find -E . -regex ".*/FILE_[0-9]{3}\.txt"
0
Shawn On

Bash filename expansion does not use regular expressions. It uses glob pattern matching, which is distinctly different, and what you're trying with FILE_[0-9]{3}.txt does brace expansion followed by filename expansion. Even bash's extended globbing feature doesn't have an equivalent to regular expression's {N}, so as already mentioned you have to use FILE_[0-9][0-9][0-9].txt