Search code examples
shmv

Moving ttf file causes extra extension


I rename my files after compression for the negotiation to occur correctly. One of the commands used is

find dist -name "*.ttf" -type f -print -exec sh -c 'f="{}"; mv -- "$f" "${f%.ttf}.ttf.ttf"' \;

The same command works perfectly fine for html, css, svg, etc extensions and results in files with double extensions as expected. eg - <>.html.html

But, the above command results in .ttf.ttf.ttf

I am running this as part of gitlab CI which generates a fresh build. So, there is no chance of conflict as it starts from an empty folder altogether. This is done for serving compressed files for a static site. The server is Apache and I cannot find anything in the httpd.conf or .htaccess that might be additionally renaming the file.

Expected output - *.ttf files should be renamed to *.ttf.ttf


Solution

  • To avoid race conditions and other implementation-specific obstacles, do this in two steps. First, rename each file with a "marker":

    find dist -name "*.ttf" -type f -print -exec sh -c 'mv -- "$1" "${1%.ttf}.ttf.foo"' _ {} \;
    

    then replace the "marker" with the proper extension:

    find dist -name "*.foo" -type f -print -exec sh -c 'mv -- "$1" "${1%.foo}.ttf"' _ {} \;
    

    This guarantees that find cannot create a file with the same extension that it is processing.