3 ms·
Better: for f in *.png; do convert "$f" "${f/%.png/.webp}" ; done Quoting the arguments lets it correctly handle file names with funny characters, includi
by _kst_ 5y ago
Better:
for f in *.png; do convert "$f" "${f/%.png/.webp}" ; done
Quoting the arguments lets it correctly handle file names with funny characters, including spaces. Using the `${parameter/pattern/string}` parameter substitution means the shell does all the work itself without invoking an external command (other than convert, of course). The '%' in the parameter substitution requires the pattern to match at the end, so "foo.png.png" becomes "foo.png.webp" and not "foo.webp.png" -- an unlikely corner case, but you might as well do it right. ("foo.png.blah" doesn't match "*.png" in the first place.)
For elaborate commands like this, I typically run it once with an "echo" inserted ("echo convert ..."), confirm that the resulting commands make sense, and then hit up-arrow to edit the command and delete the echo.
https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html https://www.gnu.org/software/bash/manual/html_node/Shell-Par...
- Flimm 5y agoQuoting is not enough, as that won't handle filenames beginning with dash correctly.