Bashing The Wildcard

This is quick note about wildcard usage for the  Linux command line particularly on  how to select/include some files among the others.

Let say you have a directory  that contains the following files:

$ls mh
apache-log.tar.gz.00
apache-log.tar.gz.01
apache-log.tar.gz.02
apache-log.tar.gz.03
apache-log.tar.gz.04
apache-log.tar.gz.05
apache-log.tar.gz.06
apache-log.tar.gz.07
apache-log.tar.gz.08
apache-log.tar.gz.09
apache-log.tar.gz.10
apache-log.tar.gz.11
apache-log.tar.gz.12
apache-log.tar.gz.13

To copy the first 10 files,  you can do this

$cp apache*0[0-9] cperdana/

The command will copy file apache-log.tar.gz.00 to apache-log.tar.gz.09 into the cperdana. The key here is that the  * and the square bracket character.

Now comes the tricky part. How  do you copy the last 9 of the files (apache-log.tar.gz.05 – apache-log.tar.gz.13)

In regular expression, I would put the ‘OR’ character (|), which looks like this:
$cp apache*(0[5-9]|1[0-3]) cperdana/

But apparently, this will not produce the expected result, instead bash will throw an error.

So how to do that?
The magic character we are looking is the ‘curly bracket’ 🙂
$ls manchaster{United,City}
will result manchasterUnited and manchasterCity only.

As our problem, here how to go about it
$cp apache*{0[5-9],1[0-3]} cperdana/

Leave a Reply