I want find a list of .gif files, convert them all to series of png images with filename identifiers and loop those series in this LaTeX. I am thining here how to combine the find + convert.

  1. I can list .gif file by :

    find -L *.gif -type f
  2. I can convert .gif image to a series of png images BUT without own identifier

    # TODO take filename from find list and give to convert and to the resulted filename
    convert -coalesce giphy.gif out%05d.png

Pseudocode where {}.png tries to say that take the new filename from the list of filenames but I think it cannot work because the list contains also .gif extension so it should be removed :

find -L *.gif -type f -exec convert -coalesce {} {}.png +

OS: Debian 8.7

  • 1
    Is your question about using the basename of each file found as the basename of the corresponding converted output files? if so you should be able to do something like find -L . -name '*.gif' -execdir sh -c 'for f; do convert -coalesce "$f" "${f%.gif}%05d.png"; done' sh {} + Jun 8, 2017 at 23:56
  • @steeldriver Yes, it seems to be correct. What is the role role of -execdir here? - - So you apply a sh loop on a gif image for each list image. Yes, that seems to be correct. - - Do you have any proposal how to find the image series info now to LaTeX and loop it in the linked LaTeX code. Jun 9, 2017 at 4:09

2 Answers 2


With zsh

autoload -U zmv # best in ~/.zshrc

splitgif() convert -coalesce "$@"
zmv -p splitgif './(***/)(*).gif(#q-.)' './${1//\%/%%}${2//\%/%%}%05d.png'

Note the need for ./ to avoid problems with files named gif:whatever.gif for instance and ${1//\%/%%} to escape the % characters that may be found in the file paths.

  • *** is the same as ** but follows symlinks when descending the directory tree like your -L
  • (#q...) glob qualifier
  • -: apply the following checks to the target of the symlink for symlinks (stat() instead lstat()) like with your -L.
  • .: only regular files (equivalent of -type f)
  • This answer sounds even better than the first one. - - I am thinking if Zsh can be integrated with LaTeX. It would mean to call Zsh from bashful package in LaTEX but no experience. My goal tex.stackexchange.com/q/374073/13173 but not sure if one step possible or 2 step approach better. What do you think? Jun 10, 2017 at 7:26
  • 1
    @Léo, in your bash code, you should be able to do zsh << 'EOZ' with the zsh code below (terminated by a line with EOZ). Or zsh /dev/fd/3 3<< 'EOZ' if the zsh code needs to do something with stdin. Jun 10, 2017 at 7:31

Steeldriver's great answer in comment

 find -L . -name '*.gif' -execdir sh -c 'for f; 
      do convert -coalesce "$f" "${f%.gif}%05d.png"; 
 done' sh {} +

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.