Merge pdf files with numerical sort

you can embed the result of command using $(), so you can do following

$ pdfunite $(ls -v *.pdf) output.pdf

or

$ pdfunite $(ls *.pdf | sort -n) output.pdf

However, note that this does not work when filename contains special character such as whitespace.

In the case you can do the following:

ls -v *.txt | bash -c 'IFS=$'"'"'\n'"'"' read -d "" -ra x;pdfunite "${x[@]}" output.pdf'

Although it seems a little bit complicated, its just combination of

  • Bash: Read tab-separated file line into array
  • build argument lists containing whitespace
  • How to escape single-quotes within single-quoted strings?

Note that you cannot use xargs since pdfunite requires input pdf's as the middle of arguments. I avoided using readarray since it is not supported in older bash version, but you can use it instead of IFS=.. read -ra .. if you have newer bash.