5

I have 90 sub-folders in a folder. Each sub-folder contains pdf files. Total pdf files are nearly 2200. How can I extract page number 3 to 10 from all pdfs?

To extract the pages from one pdf, I am using the following command.

pdftk *.pdf cat 3-10 output 3-10.pdf

2 Answers 2

5

This one liner (split into 2 lines for ease of reading) was tested and works well on my system:

find . -name '*.pdf' -type f -exec bash -c \
'pdftk "$0" cat 3-10 output "${0%.pdf}_3-10.pdf"' {} \;

Simply open a Terminal window in the base folder (the one that contains all of the sub-folders) and copy and paste the entire one line command given above. It will:

  1. Traverse all of the sub-folders and identify all the pdfs
  2. Extract pages 3-10 from each (using your example command)
  3. Give a sensible output filename: the original name with _3-10 added

And this should neatly and economically accomplish your purpose...

Variation:

Optionally you could give a different output location to collect all of the altered pdf documents. For example you could create a folder called ~/extracted and alter the commandline above to the following:

find . -name '*.pdf' -type f -exec bash -c \
'pdftk "$0" cat 3-10 output "~/extracted/${0%.pdf}_3-10.pdf"' {} \;

And thus all of the altered pdf files would appear in ~/extracted.

Endless possibilities :).

0
1

You can use qpdf as pdftk is not available on ubuntu bionic by default any more: find . -name '*.pdf' -type f -exec bash -c 'qpdf --empty --pages "$0" 3-10 -- "temp/${0%.pdf}_1.pdf"' {} \;

This will put the new pdf's into temp folder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.