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)?
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"