I have a directory where PDFs I would like to merge are downloaded. The order of merge should be the order of downloads (i.e., the first PDF to merge is downloaded first, the second PDF to merge is downloaded second, etc).
I used to execute the following terminal command in the directory of the PDFs in order to achieve this task:
gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=merged.pdf $(ls -t | tail -r)
This used to produce a merged PDF named "merged.pdf".
Recently I migrated to a new computer (both the new and old computers are Macs, which use ZSH rather than BASH), and I find that the above command stopped working on the new computer. It generates the following error:
$ gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=merged.pdf $(ls -t | tail -r)
Error: /undefinedfilename in (The)
Operand stack:
Execution stack:
%interp_exit .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- --nostringval-- --nostringval-- false 1 %stopped_push
Dictionary stack:
--dict:764/1123(ro)(G)-- --dict:0/20(G)-- --dict:75/200(L)--
Current allocation mode is local
Last OS error: No such file or directory
GPL Ghostscript 10.00.0: Unrecoverable error, exit code 1
Strangely, it works fine if I specify the PDFs to merge explicitly like
gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=merged.pdf first.pdf second.pdf third.pdf
so gs is working fine apparently. Also, I find that ls -t | tail -r produces a list of PDF file names in the order of download:
$ ls -t | tail -r
first.pdf
second.pdf
third.pdf
so ls -t | tail -r is also working fine.
How can I make gs combined with ls -t | tail -r works again? Would appreciate any insights.
How can
It seems that your
$(...)returns several words, separated by a space (i.e. something likefirst.pdf second.pdf third.pdf). They are passed togsas a single argument. Hencegsis searching for a single file with this long name.You can see what exactly is going on, by turning on
set -x. This should show the precise output of your pipe.The basic problem is however that you said that you were using code written for bash, and are now running zsh. You need to execute bash code with bash, not with zsh.