Dave's Brain

Browse - programming tips - bash change extension of a file

Date: 2009jul8
Language: bash

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
What this info useful to you? You can donate to say thanks

Add a comment

Sign in to add a comment
Copyright © 2008-2012, 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.