MacOS – How to combine multiple pdf files based on location and name with command line

bashmacospdfterminal

I have multiple folders with this structure:

Folder Name/ tmp

Other Folder name/tmp

and so on…

Inside tmp folder I have many pdf files with this name pattern:

pdf-name-1.pdf
pdf-name-2.pdf
…. and more

I’m looking for the way to joint all pdf files inside All tmp directories with this name structure, move original PDFs to trash and rename combined pdf with pdf-name.pdf

Is there any way to do it using command line or bash script in macOS?

Than you very much.

UPDATE FOR CLARIFICATION

I need to combine all PDFs inside its own tmp folder not All PDFs inside ALL tmp folders.

Best Answer

Although you specifically asked for bash script but as a supplemental answer you could consider using the python script below. Interestingly, a part of python project (Ipython) was specifically designed to replace bash.

from PyPDF2 import PdfFileMerger
import os 



def merge_it(files,path):
    merger = PdfFileMerger()
    for file in files:
        merger.append(file)

    merger.write(os.path.join(path,"combined_file.pdf"))
    merger.close()


for path, dirs, files in os.walk(os.getcwd()):
    for dir in dirs:
        if dir == 'tmp':

            pdf_files = []
            for file in os.listdir(os.path.join(path,dir)):
                if file.endswith(".pdf"):
                    pdf_files.append(os.path.join(path,dir, file))
            merge_it(pdf_files,os.path.join(path,dir))

The thing this program demands is to hard-code the paths containing /tmp sub-folders. That can also be automated depending on how much automation you want. I won't recommend removing original pdfs unless they are occupying too much space.

UPDATE: Searches for all sub-folders having name 'tmp' in the current working directory and combines all pdf files inside each of the tmp folder.