Using shell pattern matching for renaming files


for i in *.jpg; do mv $i ${i%jpg}JPG; done

The above will convert all the file names ending in jpg to ending with JPG. The key is the ${i%pattern} syntax, which says delete the part that matches the pattern and return the rest. That means if the file is named picture.jpg, ${i%jpg} will return picture. In the example above, JPG is then appended to the end of the returned part, which means picture.jpg would be moved to picture.JPG.

See page 99 of the O'Reilly book on bash for other pattern matching tools.

05/25/2005