Avoiding typing in long filenames with spaces in cli (in this case to compress pdf's using ghostscript)


I have loads of pdf’s to compress,
From here I can do it via this:

gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook \ -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf

But my pdf’s all have long names with spaces in so it will take ages to type in all the filenames and / 's etc

How can I streamline this to just make the output fiename the input filename + ‘small’ or whatever, also without having to type in the input filename, please?

Also, to do this to all pdfs in a folder?

I think this could also be a give a man a fish / teach a man to fish type of things that will help me with all sorts of things in the future

Cheers, gurus!


EDIT: using densify now but think cli would be great

I really must try to work things out for myself before asking here, sorry!

This seems to work:

find . -name '*.pdf' | while read pdf; do gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile="${pdf}_small.pdf" "$pdf"; done

1 Like

You can write a shell script and make your life even easier.

Shell script to reduce pdf size

That’s just one example I found to give you and idea. There are plenty of other examples posted on git etc … You can probably find/modify one to do what you need.


I will try to put the one I found into a shell script as it seems much shorter
never used scripts so will be fun to learn
I tried to put the line in an alias but didnt work for some reason


You need to get the command quoted and escaped properly. Similar to this example:

Complex bash alias

Sometimes it’s best to make a simple script.