Bash: Multiple pattern matching
I use this script to convert all the .png
files in a directory to .jpg
files. If I want to convert not just png files, but also tif, gif a开发者_运维技巧nd bmp files into jpg, how this script can be modified?
#!/bin/bash
for f in *.png ; do
convert "$f" -resize 50% "${f%.*}.jpg"
done
Just add the exensions you want to process; for example:
for f in *.png *.tif *.gif; do
or just:
for f in *.{png,tif,gif}; do
another approach could be: find every image file in a directory or a tree of folders and convert them to jpg except if the image is already a jpg file; for example (not tested):
find . -exec bash -c 'file "$1" | grep "image data" | grep -iv JPEG && convert "$1" -resize 50% "${1%.*}.jpg"' {} {} \;
for f in *.{png,tif,gif,bmp}; do
精彩评论