0

I am trying to execute a command depending on the file type within directory. But am unable to check the content within directory using wildcard. When provided a literal filename I am able to execute.

find ./* -type d -execdir bash -c 'DIR=$(basename {}); if [[ -e {}/*.png ]]; then echo "img2pdf {}/*.png -o $DIR.pdf"; fi ' \;
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Aparmar1
  • 3
  • 4
  • The file/directory/etc tests don't work with wildcards. See ["Bash check if file exists with double bracket test and wildcards"](https://stackoverflow.com/questions/24615535/bash-check-if-file-exists-with-double-bracket-test-and-wildcards). – Gordon Davisson Mar 05 '22 at 20:34

1 Answers1

0

Instead of going over directories, and then looking for png-s inside, find can find png-s straight away:

find . -name '*.png'

Then you can process it as you do, or using xargs:

find . -name '*.png' | xargs -I '{}' img2pdf '{}' -o '{}.pdf'

The command above will process convert each png to a separate pdf.

If you want to pass all png-s at once, and call img2pdf once:

find . -name '*.png' | xargs img2pdf -o out.pdf
battlmonstr
  • 5,841
  • 1
  • 23
  • 33
  • Oh that's because I want to have a consolidated pdf output of all the ```.png``` files, named after the directory recursively. I am now using ```[ -n "$(find {} -maxdepth 1 -name '*.png*' | head -1)" ] && img2pdf {}/*.png``` – Aparmar1 Mar 09 '22 at 20:57
  • Ok, this is even easier. I've updated the answer. – battlmonstr Mar 09 '22 at 22:47