Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· OpenAIAi2 WildChat-4.8M (AI2)

Please write a shell script for batch squeezing PDF files with CoherentPDF (cpdf)]

  1. User

    Please write a shell script for batch squeezing PDF files with CoherentPDF (cpdf)]

  2. Assistant

    Before we start, make sure you have CoherentPDF (cpdf) installed on your system. You can download it from here.

    Here is a shell script to batch squeeze PDF files with CoherentPDF (cpdf):

    #!/bin/bash
    
    # This script squeezes all the PDF files in the current directory and
    # saves the squeezed version in a folder called 'squeezed'.
    
    # Check if 'squeezed' directory exists; if not, create it
    if [ ! -d "squeezed" ]; then
        mkdir squeezed
    fi
    
    # Iterate through all PDF files in the current directory
    for file in *.pdf; do
        # Check if the file is actually a PDF
        if file --mime-type "$file" | grep -q pdf$; then
            # Call cpdf with the '-squeeze' option to squeeze the PDF
            cpdf -squeeze "$file" -o squeezed/"$file"
            echo "Squeezed: $file"
        fi
    done
    
    echo "All PDF files have been squeezed."
    

    To use this script, follow these steps:

    1. Open a text editor and paste the script contents.
    2. Save the file with a .sh extension (for example, squeeze-pdfs.sh).
    3. Open your terminal (or command prompt on Windows with an appropriate shell) and navigate to the folder containing your PDF files.
    4. Make sure the script file is in the same folder as the PDF files or adjust the path accordingly in the terminal.
    5. Make the script executable by running chmod +x squeeze-pdfs.sh (not required on Windows).
    6. Run the script with ./squeeze-pdfs.sh (or ./squeeze-pdfs.sh on Windows' command prompt).

    The script will create a folder named "squeezed" and save the squeezed versions of your PDF files in it.