EarthIsHome
EarthIsHome

Reputation: 735

Bash: using {} to replace filename extension

I'm testing out bash filename replacement with parameter expansion as shown here: How to change file extension in Linux shell script?

However, I can't seem to get the following to work as I intend:

given the following files:

tmp/01. Losing A Whole Year.flac
tmp/02. Narcolepsy.flac

I run find for flac files into parallel running the echo command for testing output. I'm trying to replace .flac with .mp3.

find tmp -type f -name '*.flac' | time parallel -j+0 --eta 'echo "{} {.flac%%.mp3}"'

My output is:

tmp/01. Losing A Whole Year.flac {.flac%%.mp3}
tmp/02. Narcolepsy.flac {.flac%%.mp3}

I'm planning on using this with ffmpeg if that makes a difference.

Edit: Expected output:

tmp/01. Losing A Whole Year.flac tmp/01. Losing A Whole Year.mp3
tmp/02. Narcolepsy.flac tmp/02. Narcolepsy.mp3

Edit 2: To replace the file extension, I can use:

find tmp -type f -name '*.flac' | time parallel -j+0 --eta 'echo "{} {.flac%%.mp3}"'

However, this provides the following output

tmp/01. Losing A Whole Year.flac tmp/01.\ Losing\ A\ Whole\ Year.mp3
tmp/02. Narcolepsy.flac tmp/02.\ Narcolepsy.mp3

Maybe this is an ffmpeg question now, because ffmpeg will not be able to find the file if there's a \ character in place of a space character.

Upvotes: 1

Views: 660

Answers (1)

EarthIsHome
EarthIsHome

Reputation: 735

I think I figured it out:

find tmp -type f -name '*.flac' | time parallel -j+0 --eta 'echo "{} {.}.mp3"'

will produce the expected results.

Although, I don't know why this doesn't work:

find tmp -type f -name '*.flac' | time parallel -j+0 --eta 'echo "{} {.flac}.mp3"'

Edit: While the above seems to work, \ characters are inserted to replace blank spaces, which would cause ffmpeg to not find the desired files.

Upvotes: 1

Related Questions