When you need to combine PDFs inside a script or batch job, the command line is the fastest route. Here is the exact syntax for three free tools, pdfunite, qpdf, and pdftk, so you can merge files on Linux or macOS. You will also see how to control page order, which matters when you are assembling reports, invoices, or scans.
Why Use the Command Line to Merge PDFs
A graphical tool is fine for two files. But when you merge dozens of PDFs every day, or want the process automated in a shell script, the terminal is the practical choice. These tools copy page objects without re-rendering. Output quality stays identical to the originals. The file size lands roughly at the sum of the inputs, with no extra compression or quality loss.
All three are free and open source. They run directly in your terminal, so you chain them with other shell commands, drop them into cron jobs, or call them from your own scripts. The learning curve is short. Learn the syntax once and apply it to any number of files.
Merging with Pdfunite
Basic Syntax
pdfunite is part of the Poppler suite, included in most Linux distributions and available for macOS. Its syntax is straightforward: list the input files, then the output file as the last argument.
pdfunite a.pdf b.pdf c.pdf combined.pdfThis merges the files in the order given. a.pdf becomes the first pages, then b.pdf, then c.pdf. The last argument is always the output. Use relative or absolute paths, and mix different page sizes; pdfunite places them in sequence.
Using Wildcards
To merge all PDFs in a folder, use a shell wildcard:
pdfunite *.pdf combined.pdfBe careful: the shell expands *.pdf in alphabetical order. That works if your files have zero-padded names like 01.pdf, 02.pdf ... 10.pdf. Without zero-padding, 10.pdf comes before 2.pdf, which is probably not what you want.
Merging with Qpdf
Basic Merge Command
qpdf gives you more control, especially when you need to select specific pages or ranges.
qpdf --empty --pages a.pdf b.pdf -- combined.pdfThe --empty flag starts with an empty PDF. --pages tells qpdf that what follows is a list of files and optional page ranges. The double dash separates the input list from the output filename.
Selecting Page Ranges
Add page ranges after any file. To take only pages 1 through 3 from a.pdf and then all of b.pdf:
qpdf --empty --pages a.pdf 1-3 b.pdf -- combined.pdfUse ranges like 1- for page 1 to the end, or r1-r3 for pages numbered from the end. This makes qpdf the tool for extracting and reordering pages while merging.
Handling Encrypted Files
If an input file is encrypted, supply a password for the file that follows it with --password=:
qpdf --empty --pages --password=secret a.pdf b.pdf -- combined.pdfThat applies the password to a.pdf only. For multiple encrypted files, repeat the option before each one.
Merging with Pdftk
Basic Merge Command
pdftk uses an intuitive merge command:
pdftk a.pdf b.pdf cat output combined.pdfThe word cat is a verb here, meaning concatenate the pages. List the input files, then cat, then output followed by the output filename. Input order determines page order in the result.
pdftk accepts wildcards:
pdftk *.pdf cat output combined.pdfAgain, the shell expands the wildcard alphabetically. Zero-padded names keep the intended sequence.
Specifying Page Ranges
pdftk also allows page ranges, with slightly different syntax from qpdf. To take pages 1-3 from a.pdf and then all of b.pdf:
pdftk a.pdf 1-3 b.pdf cat output combined.pdfUse 1- for “from page 1 to the end” and r1 for the last page. This gives you fine control over the final page order.
Controlling the Order of Pages and Files
File Order Is Page Order
Order matters. All three tools preserve the sequence in which you list the input files. If you need a specific order, list the files in that exact sequence.
For scripts, the most reliable method is to generate the file list explicitly. Do not rely on wildcards. Build an array in a shell script:
files=(01.pdf 02.pdf 10.pdf) pdfunite "${files[@]}" combined.pdfOr with qpdf:
qpdf --empty --pages "${files[@]}" -- combined.pdfWildcard Pitfalls
If you do use wildcards, the shell sorts alphabetically. 01.pdf, 02.pdf ... 10.pdf works correctly. But 1.pdf, 2.pdf ... 10.pdf puts 10.pdf after 1.pdf and before 2.pdf. Rename your files with leading zeros, or use a loop to build the list in the order you want.
Mixing Files and Page Ranges
For page-level reordering, qpdf and pdftk give you the most flexibility. Mix whole files and specific page ranges in a single command.
Installing the Tools on Linux and MacOS
On Debian or Ubuntu
Open a terminal and run:
sudo apt install qpdf poppler-utilsThis installs both qpdf and pdfunite. pdfunite comes with poppler-utils. For pdftk, install the pdftk package separately; it is included in the default repositories.
On macOS
Use Homebrew. Run:
brew install qpdf popplerThis gives you qpdf and pdfunite. For pdftk, install it with brew install pdftk; the formula may be named pdftk-java on some versions. After installation, verify the tools are available by typing which pdfunite, which qpdf, or which pdftk.
Practical Examples for Batch Merging
Ready-to-Use Commands
- Merge all PDFs in a folder, in alphabetical order:pdfunite *.pdf combined.pdf
- Merge specific files in a custom order:qpdf --empty --pages report.pdf invoice.pdf scan.pdf -- final.pdf
- Take pages 1-3 from one file and all of another:pdftk part1.pdf 1-3 part2.pdf cat output result.pdf
- Merge files with a shell loop to control order:for f in file*.pdf; do echo "$f"; done | sort -V | xargs pdfunite combined.pdf
In the last example, sort -V sorts version numbers naturally, so file2.pdf comes before file10.pdf. Use this when your filenames lack leading zeros.
These tools copy page objects without re-rendering. Output quality stays identical to the originals. The output size will be roughly the sum of the input sizes.