List file range with UNIX wildcards

14

3

Say for example I have the following files in my working directory: mjk0001.fits, mjk0002.fits, ... numerically increasing all the way to mjk9999.fits.

Is there a way to use UNIX wildcards to list or operate on a sequential group of them? For example, if I need to run a process on 0025 through 0050, what format would I use?

I have tried the following but have had no success:

ls *[25-50].fits
ls mjk00[25-50].fits
ls mjk[0025-0050]*
ls *[0025-0050]*

Thanks in advance everyone, MK

Matt Keene

Posted 2013-04-25T20:15:33.537

Reputation: 143

For reference: pattern matching, globbing

– ignis – 2013-04-26T09:21:01.343

Answers

20

You can use brace expansion:

ls mjk00{25..50}.fits

Leading zeros can be included if necessary:

ls mjk0{000..149}.fits

chepner

Posted 2013-04-25T20:15:33.537

Reputation: 5 645

1Using this syntax, you can also specify a step value: {25..50..2}. – nobar – 2017-03-27T19:55:05.783

1

Not directly, but you can use multiple glob patterns:

ls mjk002[56789].fits mjk00[34]?.fits

It's not exactly what you are looking for, but it's the closest thing that glob patterns offer, and it is certainly better than typing each fileystem. At least the number of patterns you will need per numerical range is bounded by the log of the numerical endpoints of the range.

Celada

Posted 2013-04-25T20:15:33.537

Reputation: 2 120

1

You could use a regex with grep. I'm not a regex guru. The following returns the pattern with the numbers betweeen 25-50:

ls | sort -n | grep -E 'mjk00([2][5-9]|[3-4][0-9]|[5][0]).fits'

I don't know if it is possible and how eventually generalize this with grep.

Heisenbug

Posted 2013-04-25T20:15:33.537

Reputation: 645

0

It may be possible to achieve greater portability or flexibility by using a for loop and printf:

for i in $(seq 25 50 1) ; do ls --escape $(printf "mjk%04d*" $i) ||true; done

This handles spaces well and the results are well suited for piping to xargs for further processing.

Numbers with multiple matches will result in all matching files being listed.

Numbers with no matches will report an error on stderr, but the ||true suppresses a bash-level error return.


In the basic case, though, using for and printf is no different than the more concise:

ls --escape mjk{0025..0050..1}* ||true

nobar

Posted 2013-04-25T20:15:33.537

Reputation: 530

If you want to separate the results of brace expansion, you can use a for loop: for i in {025..050} ; do ... ; done – nobar – 2017-12-07T21:54:42.803