I intend to compress a few thousand PDF files in a folder recursively.
I tried with following loop:
#!/bin/bash
find "/home/user/original" -type f -name *.pdf | while read -r file
do
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/prepress -dNOPAUSE -dBATCH -dQUIET -sOutputFile="/home/user/processed$file" "$file"
done
(processed$file is used because $file brings a / at beginning & I've also tried processed/$file)
Anyway, running the loop gives the following error:
GPL Ghostscript 9.26: **** Could not open the file /home/user/processed/home/user/original/test001.pdf .
**** Unable to open the initial device, quitting.
For some reason its looking for pdf in path/to/output/path/to/input. I tried changing to ./ links instead of / but to no avail.
If I run the following on its own, it outputs a compressed pdf nicely
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/prepress -dNOPAUSE -dBATCH -dQUIET -sOutputFile="/home/user/processed/output.pdf" "input.pdf"
Any ideas why the loop isn't working?
P.S. All directories are 777 for now to make sure there aren't any permission errors
find
directly, before piping it towhile read ...
?cd /home/user/original
andfind . -type f ...
instead? That should give you paths starting with./
, which you can use like/home/user/processed/$file
(note extra slash).