Dave's Brain

Browse - Programming Tips - How can I change the extension (suffix) of a file in a bash script?

Date: 2009jul8
Language: bash
OS: Linux

Q.  How can I change the extension (suffix) of a file in a bash script?

A.  Use the ${} construct in bash.  For example, to change the
extension of variable $I from .flac to .mp3 and save in variable $MP3 do this:

MP3=${I/\.flac/.mp3}
Here is a script that converts a folder of .flac files to .mp3:
#!/bin/sh for I in *.flac; do MP3=${I/\.flac/.mp3} ffmpeg -i "$I" "$MP3" done

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.