Glob with Numerical Order
Once more, zsh's glob qualifiers come to the rescue.
echo *.pdf(n)
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 c0.pdf
c1.pdf
… c9.pdf
.
c??.pdf
expands to c10.pdf
c11.pdf
… c20.pdf
(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
with the LC_COLLATE
variable,
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 c999.pdf
,
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 c?{,?{,?{,?{,?}}}}.pdf
.
If your list of filenames is sparse
(e.g., there’s a c0.pdf
and a c12345.pdf
,
but not necessarily every number in between),
you should probably set the nullglob
option.
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
(e.g., a<number>.pdf
,
b<number>.pdf
,
and c<number>.pdf
,
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 {a,b,c}?{,?}.pdf
.