find で pdf の一覧を抽出し、pdfinfo で情報を見る
対象ファイル(aディレクトリの中身)
$ ls a a 3.pdf a1.pdf a2.pdf
"a 3.pdf" は、ファイル名に空白文字を含む。
xargs が効かないみたいだったので、以下のスクリプトを作った。
find <ディレクトリ> -type f -name "*.pdf" | while read file; do echo "FILE:" "$file" pdfinfo "$file" done
空白文字があるファイルに対応するため、$file はダブルクオーテーションでくくっている。
実行してみる。
$ find a -type f -name "*.pdf" | while read file; do echo "FILE:" "$file"; pdfinfo "$file"; done FILE: a/a1.pdf Title: a Creator: Pages Producer: macOS バージョン11.1(ビルド20C69) Quartz PDFContext CreationDate: Wed Jan 20 11:36:07 2021 ModDate: Wed Jan 20 11:36:07 2021 Tagged: no UserProperties: no Suspects: no Form: none JavaScript: no Pages: 1 Encrypted: no Page size: 595.28 x 841.89 pts (A4) Page rot: 0 File size: 3968 bytes Optimized: no PDF version: 1.3 FILE: a/a2.pdf Title: a Creator: Pages Producer: macOS バージョン11.1(ビルド20C69) Quartz PDFContext ... Optimized: no PDF version: 1.3 FILE: a/a 3.pdf Title: a Creator: Pages Producer: macOS バージョン11.1(ビルド20C69) Quartz PDFContext ... Optimized: no PDF version: 1.3
find で pdf の一覧を抽出し、pdftk でメタデータを変更する
find <ディレクトリ> -type f -name "*.pdf" | while read file; do echo "FILE:" "$file" cp "$file" "$file".bak cat "$file" | pdftk - update_info_utf8 keyword.txt output "$file" done
(keyword.txt)
InfoKey: Keywords InfoValue: aaa
実行してみる。
$ find a -type f -name "*.pdf" | while read file; do echo "FILE:" "$file"; cp "$file" "$file".bak; cat "$file" | pdftk - update_info_utf8 keyword.txt output "$file"; done FILE: a/a1.pdf FILE: a/a2.pdf FILE: a/a 3.pdf $ find a -type f -name "*.pdf" | while read file; do echo "FILE:" "$file"; pdfinfo "$file"; done FILE: a/a1.pdf Title: a Keywords: aaa Creator: Pages ... Optimized: no PDF version: 1.3 FILE: a/a2.pdf Title: a Keywords: aaa Creator: Pages ... Optimized: no PDF version: 1.3 FILE: a/a 3.pdf Title: a Keywords: aaa Creator: Pages ... Optimized: no PDF version: 1.3
参考
Removing PDF Metadata with PDF Toolkit
http://www.marshalgraham.com/2011/04/removing-pdf-metadata-with-pdf-toolkit.html
Bash escaping spaces in filename, in variable
https://stackoverflow.com/questions/19122448/bash-escaping-spaces-in-filename-in-variable