'Wildcard within if conditional of Bash fails to execute. Works when literal filename provided

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 ' \;


Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1