Listing with `ls` and regular expression
How about:
ls -d -- *[!0-9][0-9].txt
The !
at the beginning of the group complements its meaning.
As noted in the comments, this is bash's doing, try e.g.:
printf "%s\n" *[!0-9][0-9].txt
The question asked for regular expressions. Bash, and thus ls
, does not support regular expressions here. What it supports is filename expressions (Globbing), a form of wildcards. Regular expressions are a lot more powerful than that.
If you really want to use regular expressions, you can use find -regex
like this:
find . -maxdepth 1 -regex '\./.*[^0-9][0-9]\.txt'
Find is recursive by default, but ls
is not. To only find files in the current directory, you can disable recursion with -maxdepth 1
.
Find matches against the filename with path, that's why the filenames start with ./
if you run find in .
. The regex starts with \./
to cope with that. Note that in a regex, .
is a special character matching any character, but here we really want a dot, that's why we escape it with a backslash. We do the same for the dot in .txt
, because the regex would otherwise also match Atxt
. The digit classes are same as for globbing, just that you need ^
instead of !
to invert the character class.
If you want to get the output of ls
, then you can use -exec ls
like this:
find . -maxdepth 1 -regex '\./.*[^0-9][0-9]\.txt' -exec ls -lah {} \;
find
supports a couple of different regex flavors. You can specify a -regextype
, for example:
find . -maxdepth 1 -regextype egrep -regex '\./.*[^0-9][0-9]\.txt'
For me, possible types are:
‘findutils-default’, ‘awk’, ‘egrep’, ‘ed’, ‘emacs’, ‘gnu-awk’, ‘grep’, ‘posix-awk’, ‘posix-basic’, ‘posix-egrep’, ‘posix-extended’, ‘posix-minimal-basic’, ‘sed’
You can run find -regextype help
to find out what is supported on your system.