2

I have a folder containing files of the format file(0).jpg to file(100).jpg

I can't directly use convert to generate a pdf here because the order ends up being 0,1,100,2,20,21,...

echo *.jpg(n) gives the correct order of files.

how do I pipe this into convert?

I have tried echo *.jpg(n) | convert - out.pdf

0
6

If the command that you'd like to execute is

convert 'file(0).jpg' 'file(1).jpg' ...etc... 'file(100).jpg' out.pdf

then either use your glob,

convert ./*.jpg(n) out.pdf

or to only include files in 0..100 range that match that pattern:

convert 'file('<0-100>').jpg'(n) out.pdf

or you could use a brace expansion:

convert 'file('{0..100}').jpg' out.pdf

Though note that it's not globbing, the strings file(0).jpg through to file(100).jpg will be passed to convert regardless of whether these are names of existing files or not. Contrary to the previous one, it would also miss files named file(012).jpg for instance.

3

If you have many files which will cause you to run into the command line limit of the OS, you should use convert's @ "filename reference" feature:

generate the list of files ... | convert @- out.pdf

On a shell which doesn't support *.jpg(n) globs like zsh does, you can generate the list of files in the right order with:

echo 'file('?').jpg' 'file('??').jpg' 'file('???').jpg' | convert @- out.pdf

Or with a shell which support brace expansion:

echo 'file('{?,??,???}').jpg' | convert @- out.pdf

Unlike {1..100} this will only match files that exist. Since echo is a built-in in most shells, it's not subject to the command line limit of the OS, so this kind of construct may turn into a useful UUoE.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.