Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm attempting to write a script for use with HandbrakeCli to convert .mkv-files to XBOX 360 compatible mp4-files.

for f in "$@"
    echo "$f"
    HandbrakeCli -i "$f" -o ~/Movies/Handbrake/"${f%.*}.mp4" --preset "Normal" --mixdown "stereo" --subtitle "1" --width 720 --keep-display-aspect

The problem is that the destination filename (according to output from HandbrakeCli) includes the full path to the input file. Naturally I'm only interested in the filename without extension since I add .mp4.

I'm not used to shell scripting so help is very much appreciated!

share|improve this question
Also see this discussion of this question on stackoverflow:… – guenter Oct 3 '11 at 11:11
up vote 3 down vote accepted

First remove the path:


Then change the extension:


(Not necessarily in that order, of course.)

share|improve this answer

Since you know the extension of your input file, you can say

new=~/Movies/Handbrake/"$(basename "$f" .mkv)".mp4
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .