Find the answer to your Linux question:
Results 1 to 5 of 5
Greetings! I would like to ask for you help resolving a little problem. Unfortunately I`m not a programmer I have a lot of pdf files and I want to convert ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2010
    Posts
    3

    Batch script for all files in folder


    Greetings!
    I would like to ask for you help resolving a little problem. Unfortunately I`m not a programmer

    I have a lot of pdf files and I want to convert them to a lower quality for the web. I tried to use the following command (using ghostscript):
    Code:
    gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/default -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf
    Is there a way to make a batch to do this for all pdf-s in a folder?

    Thank you in advance for your help!
    I.

    ps. if anyone knows a better way to reduce pdf dpi you can tell that too

  2. #2
    Linux Engineer hazel's Avatar
    Join Date
    May 2004
    Location
    Harrow, UK
    Posts
    1,237
    for name in *.pdf ; do command ; done.

    You will have to use "$name" in your command string instead of the literal filename.
    "I'm just a little old lady; don't try to dazzle me with jargon!"
    www.hrussman.entadsl.com

  3. #3
    Just Joined!
    Join Date
    Oct 2010
    Posts
    3
    Thank you for your answer!
    But could you please a little more specific what do I have to do?

    Write the line :
    for $name in *.pdf ; do command ; done.
    in a file and run it?

    Thank you for your patiance!

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    The command that hazel gave is basically a loop in a Bash script. If we wrote a script to do it, it would look like this:
    Code:
    for file in *.pdf; do
        output_file=$(echo "$file" | sed -re 's/\.pdf$/out.pdf/')
    
        gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/default -dNOPAUSE -dQUIET -dBATCH -sOutputFile="$output_file" "$file"
    done
    What this script does is:

    For every file ending in '.pdf' in the current directory, create a string called output_file, and then run the command, substituting the input file for "$file", and the output file for "$output_file". The output file name that I created here just adds '.out' before the '.pdf' of the filename. So we go from "file.pdf" to "file.out.pdf".

    Hazel's approach was to collapse this script into a single line so that it's easy to run:
    Code:
    for file in *.pdf; do output_file=$(echo "$file" | sed -re 's/\.pdf$/out.pdf/'); gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/default -dNOPAUSE -dQUIET -dBATCH -sOutputFile="$output_file" "$file"; done
    You could also copy the script that I wrote above into a file, and be able to run, for instance, a "downgrade_all_pdfs" command to run the command automatically in the current directory.

    This is all about Bash scripting. If you want to learn more, you can check:
    Bash Guide for Beginners

    Advanced Bash-Scripting Guide

  6. #5
    Just Joined!
    Join Date
    Oct 2010
    Posts
    3
    Thank you very much for your answer!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •