2

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

but not the filename with the "7" character after "FILE_", 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)?

Jabber1
  • 69
  • 5
  • 3
    Btw.: [regex](https://en.wikipedia.org/wiki/Regular_expression) != [globbing](https://en.wikipedia.org/wiki/Glob_(programming)) – Cyrus Oct 16 '20 at 11:17
  • Using shell [glob patterns](https://www.gnu.org/software/bash/manual/bash.html#Pattern-Matching), you'd have to write `FILE_[0-9][0-9][0-9].txt` – glenn jackman Oct 16 '20 at 13:07

2 Answers2

3

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"
Thân LƯƠNG Đình
  • 3,082
  • 2
  • 11
  • 21
  • 1
    If it may be useful in this case or for anybody else that comes across this question in the future, `find -regextype gnu-awk -regex ./FILE_[0-9]{3}.txt` also achieves the same result. – Rfroes87 Oct 16 '20 at 12:26
0

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

Shawn
  • 47,241
  • 3
  • 26
  • 60