How to Quickly Combine PDFs at the Linux Command Line
While PDF files have their uses, they can be a bit of a pain to work with. That's especially true when you need to mash two or more PDF files together — say, when you're adding a cover to a book.
To do that deed, you can use a pair of tools that I introduced a while back. Or you can jump to the command line and use software that's probably already on your computer. For the command line junkie, the latter option might be the preferred option.
Let's take a look at a quick and dirty way to combine PDFs at the Linux command line.
What You'll Need
You'll only need one piece of software: Ghostscript, which is a set of programs that interpret and render Postscript and PDF files. Ghostscript is standard kit with many Linux distributions.
If it isn't already installed on your computer, install Ghostscript using your distribution's package manager or software centre.
To do that, crack open a terminal window. Then, navigate to the folder containing the PDF files that you want to combine.
Once you're there, run this command:
gs -dBATCH -dNOPAUSE -dQUIET -sDEVICE=pdfwrite -sOutputFile=newFile.pdf pdfFile1.pdf pdfFile2.pdf pdfFile3.pdf
That's a lot to take in. Let's break that command and its options down:
- gs runs the Ghostscript interpreter
- -dBATCH tells Ghostscript to stop running after combining the files
- -dNOPAUSE tells Ghostscript not to prompt you at the end of each page
- -dQUIET tells Ghostscript not to display output from the command
- -sDEVICE=pdfwrite tells Ghostscript to output a PDF file
- -sOutputFile=newFile.pdf tells Ghostscript to combine the files (whose names come at the end of the command) into a file named newFile.pdf
That makes a little more sense now, doesn't it?
Turning That Into a Single Command
The command above is a lot to remember, especially if you only combine PDF files every so often. An easier way of using that command is to turn it into an alias.
To do that, open the file .bashrc (found in your /home directory) and add something like this to the file:
alias mergepdf='gs -dBATCH -dNOPAUSE -dQUIET -sDEVICE=pdfwrite -sOutputFile=output.pdf'
When you run the command mergepdf at the command line, followed by the names of the PDF files you want to combine, Ghostscript creates the file output.pdf. Feel free to change the name of the alias and the output file.