Glob with Numerical Order
Once more, zsh's glob qualifiers come to the rescue.
Depending on your environment you can use
ls -v with GNU coreutils, e.g.:
gs -q -sPAPERSIZE=a4 -dNOPAUSE -dBATCH -sDEVICE=pdfwrite \ -sOutputFile=out.pdf $(ls -v)
Or if you are on recent versions of FreeBSD or OpenBSD:
gs -q -sPAPERSIZE=a4 -dNOPAUSE -dBATCH -sDEVICE=pdfwrite \ -sOutputFile=out.pdf $(ls | sort -V)
If all the files in question have the same prefix
(i.e., the text before the number;
c in this case), you can use
gs …args… c?.pdf c??.pdf
c?.pdf expands to
c??.pdf expands to
(and up to
c99.pdf, as applicable).
While each command-line word containing pathname expansion character(s)
is expanded to a list of filenames sorted (collated) in accordance
the lists resulting from the expansion of adjacent wildcards (globs)
are not merged; they are simply concatenated.
(I seem to recall that the shell man page once stated this explicitly,
but I can’t find it now.)
Of course if the files can go up to
you should use
c?.pdf c??.pdf c???.pdf.
Admittedly, this can get tedious if you have a lot of digits.
You can abbreviate it a little; for example, for (up to) five digits,
you can use
If your list of filenames is sparse
(e.g., there’s a
c0.pdf and a
but not necessarily every number in between),
you should probably set the
Otherwise, if (for example) you have no files with two-digit numbers,
you would get a literal
c??.pdf argument passed to your program.
If you have multiple prefixes
with numbers of one or two digits),
you can use the obvious, brute force approach:
a?.pdf a??.pdf b?.pdf b??.pdf c?.pdf c??.pdf
or collapse it to