このコードを短くする方法を教えてください。
find /home/peace/* -iname "*.pdf" -exec pdfgrep -i yello {} \; -exec cp {} /home/peace/Desktop/yello \;
find /home/peace/* -iname "*.pdf" -exec pdfgrep -i green {} \; -exec cp {} /home/peace/Desktop/green \;
find /home/peace/* -iname "*.pdf" -exec pdfgrep -i blue {} \; -exec cp {} /home/peace/Desktop/blue \;
find /home/peace/* -iname "*.pdf" -exec pdfgrep -i grey {} \; -exec cp {} /home/peace/Desktop/grey \;
find /home/peace/* -iname "*.pdf" -exec pdfgrep -i black {} \; -exec cp {} /home/peace/Desktop/black \;
find /home/peace/* -iname "*.pdf" -exec pdfgrep -i white {} \; -exec cp {} /home/peace/Desktop/white \;
ベストアンサー1
そしてzsh
:
#! /bin/zsh -
cd /home/peace || exit
set -o extendedglob # for (#i)
pdf=(*.(#i)pdf(.ND)) # all pdf files case insensitive, including hidden ones
# in the current directory, only considering regular files.
colors=(yello green blue grey black white)
(($#pdf)) || exit 0 # exit with success if there's no pdf file (job already done)
ret=0
for color ($colors) {
files=(${(0)"$(pdfgrep -ilZe $color -- $pdf)"}) # all files containing the color
# using NUL-delimited records
if (($#files)) { # some files were found
mv -i -- $files Desktop/$color/ || ret=$? # move them, record failures
pdf=(${pdf:|files}) # remove the files from the list as we've moved them
}
}
exit $ret
これにより、呼び出し回数とpdfgrep
ディレクトリ読み取り回数が最小限に抑えられます。/home/peace