1

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.

1

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.

|improve this answer|||||
  • Thank you for your reply. There are more than 1000 tmp folders so I need any automated script. Thank you again for your help. – jose Sep 26 '19 at 0:17
  • @Jose : Edited my answer. This now works for automated finding of tmp folders as well – Anshul Choudhary Sep 26 '19 at 21:08
  • Thank you I need to combine all PDFs inside its own tmp folder not All PDFs inside ALL tmp folders. I've already updated question description... – jose Sep 26 '19 at 21:31
  • @Jose, updated the code to reflect your "updated" requirements. If it works, could you kindly accept my answer. – Anshul Choudhary Sep 26 '19 at 21:55
  • Thank you. It seems that script is working but inserted some white blank pages in combined pdf file. By the way, is there any trick to combine only certain files based in name? Thanks. – jose Sep 27 '19 at 10:32
0

Apparently /System/Library/Automator/Combine PDF Pages.action/Contents/Resources/join.py is a native tool on Mac to combine multiple PDF files. If that doesn't quite do what you want, you'll need to install some additional software to handle that part. Look at Imagemagick, Graphicsmagick, or pdfjam for that. I'm sure there's many others.

For the script part, you can do it all with a one-liner (shown broken up here for simplicity, but you can put it all on one-line):

for tmpdir in $(find . -type d -name "tmp"); do 
    list_of_files_to_combine=$(find . -name "*.pdf") && echo "Combining pdf files in $d" && some_pdf_combiner_tool $list_of_files_to_combine pdf-name.pdf && rm $list_of_files_to_combine;
done

Replace some_pdf_combiner_tool with the name of whatever tool you chose above.

I recommend testing this thoroughly to ensure the combining worked before deleting the original pdf files.

Note also that rm from the Terminal is going to just delete the files directly, not put them in the Trash for possible later retrieval. If trashing them from the command line is important to you, see How to move files to trash from command line?

|improve this answer|||||
  • 1
    Here's an application that is native to macOS which will join pdfs- /System/Library/Automator/Combine PDF Pages.action/Contents/Resources/join.py – fd0 Sep 26 '19 at 15:40
  • 1
    Upvoted for Highlighting the issue of rm vs trash. – Anshul Choudhary Sep 26 '19 at 20:31
  • Thank you for your reply but this command combine all pdfs inside any tmp folder in one pdf and i need to combine the pdfs inside its own tmp folder. – jose Sep 26 '19 at 21:28
  • OK @jose, I've edited the post to combine pdfs inside each tmp folder instead. – woolfie Sep 27 '19 at 17:29
  • Why is looping over find's output bad practice? - unix.stackexchange.com/questions/321697/… – fd0 Sep 27 '19 at 17:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .