9

Is there a simpler way to write: wc -l foo?.txt foo??.txt ?

Since in my case I have (at most) two digit numbers, I tried using the range specification wc -l foo[1-99].txt but this ended up displaying only results between 1 and 9 (instead of 99). But this is a (nonworking) hack, and I really need something like wc -l foo[?,??].txt.

Matsmath
  • 380

2 Answers2

8

When you have two or more choices, use brace expansion:

foo{?,??}.txt

Or, to be more specific:

foo{[0-9],[0-9][0-9]}.txt

Example

Let's consider a directory with these three files:

$ ls foo*txt
foo111.txt  foo11.txt  foo1.txt

Observe:

$ echo foo{?,??}.txt
foo1.txt foo11.txt

And:

$ echo foo{[0-9],[0-9][0-9]}.txt
foo1.txt foo11.txt

Or, more concisely:

$ echo foo{,[0-9]}[0-9].txt
foo1.txt foo111.txt
John1024
  • 13,947
4

With bash's extended glob (extglob) syntax, you can use foo@(?|??) e.g.

wc -l foo@(?|??).txt

The extglob option should be enabled by default in the interactive shell on current Ubuntu releases, but if not you can enable it using

shopt -s extglob
steeldriver
  • 142,475