The Linux command line is quite magical in that it can do wonders in few lines of writing. Work which used to take hundreds of hours can be done easily using a one-line command or a Bash shell script.

We would like to deal with PDFs today; As they are the default format for sharing documents and most people reading this article have probably used tons of them in their lives. We’ll show you how you can divide a large PDF file (say 500 pages) into much smaller PDF files (E.g 100 PDF files where each one of them is 5 pages long).

This could be useful for automation tasks or other types of use cases that may come to your mind where dealing with the original PDF is just not quite feasible.

Getting the Shell Script

First, you have to install pdftk, which is a well-known command line tool to work with PDF files on Linux and other Unix-like operating systems. You may grab it by searching for its name in your package repository, or simply installing it as a Snap package:

sudo snap install pdftk

The good-and-working Shell script to do the the job was actually provided as an answer to a Unix StackExchange question, by the user @Unlink. It simply takes two parameters named $pagesper (Number of pages in each PDF chunk you want) and $file (Name or path of the PDF file you want to target), and then just uses a while loop in Bash to invoke the pdftk command, that’s it.

Here is the script:

number=$(pdfinfo -- "$file" 2> /dev/null | awk '$1 == "Pages:" {print $2}')


while [ "$count" -gt "$counter" ]; do
 start=$((counter*pagesper + 1));
 end=$((start + pagesper - 1));
 if [ $end -gt $number ]; then

 counterstring=$(printf %01d "$counter")
pdftk "$file" cat "${start}-${end}" output "${filename}_${counterstring}.pdf"
 counter=$((counter + 1))

Put it in your home folder (Or wherever you want to work with the PDF file) under the name, and then make it executable with:

chmod +x

Now we are ready to use the script.

Dividing the PDF File into Smaller Chunks

Simply run the Bash script using the following command:

pagesper=5 file=my_large_file.pdf ./

Where 5 is the number of pages in each divided PDF file you want, and my_large_file.pdf is the name of the PDF file you want to divide (Must be of course in the same directory as the Shell script).

And you’ll see how the smaller PDF files are generated:


Each one of these files contains 5 pages as we requested.

Other PDF Manipulation Possibilities

You can use the pdftk command line without the script to do further manipulations with PDF files on Linux. Run pdftk --help to see its full documentation.

You can for example divide the PDF file into smaller chunks where each new PDF file is just 1 page long using the following command (So that if you have 300 pages in the PDF, 300 smaller files will be generated where each one of them is 1 page long):

pdftk my_large_file.pdf burst

You may also extract specific ranges from the PDF file into smaller files (E.g you may just want pages 1-4, 8-12 and 16 from the PDF file and nothing else):

pdftk A=my_large_file.pdf cat A1-4 A8-12 A16 output new_filename.pdf

And the new PDF will contain only the pages you specified in the range.


So as you have seen it may not take you 5 minutes of work to get this script kicking in order to manipulate your large PDF files however you like. All thanks to the Linux command line utilities like pdftk and the power of the Bash shell.

If you have any further questions or additions on PDF manipulation on Linux, then we would love to have them in the comments section below.

1 Comment
Jeroen February 6, 2022

Another possibility to manipulate pdf files is poppler.

Poppler consists of the following packages:
* pdfdetach — lists or extracts embedded files (attachments)
* pdffonts — font analyzer
* pdfimages — image extractor
* pdfinfo — document information
* pdfseparate — page extraction tool
* pdfsig — verifies digital signatures
* pdftocairo — PDF to PNG/JPEG/PDF/PS/EPS/SVG converter using Cairo
* pdftohtml — PDF to HTML converter
* pdftoppm — PDF to PPM/PNG/JPEG image converter
* pdftops — PDF to PostScript (PS) converter
* pdftotext — text extraction
* pdfunite — document merging tool

To extract pages 11 to 14 from sample.pdf one would do:
pdfseparate -f 11 -l 14 sample.pdf sample-%d.pdf, this woud result in sample-11.pdf, sample-12.pdf, sample-13.pdf and sample-14.pdf, and of course the original pdf would still exist.

To unite the pages do:
pdfunite sample-11.pdf, sample-12.pdf, sample-13.pdf sample-14.pdf sample-part.pdf, this would result in sample-part.pdf existing out of pages 11 to 14 of the original file sample.pdf.

Get a VPN



Become a Supporter

For the price of one cup of coffee per month:

  • Support the FOSS Post to produce more content.
  • Get a special account on our website.
  • Remove all the ads you are seeing (including this one!).
  • Help us get to our goal of 100 supporters, to start many initiatives.

Opinions Column

Latest Distribution Reviews

Tools We Use