0

I'm having trouble manipulating strings in bash. I wish to re-write extensions.

I have the following 2 files in a Downloads directory

  • Example 001.mkv
  • Example 002.mkv

Using the script below I always get the same filenames returned without .mkv rewritten into .mp4.

find /Downloads -name \*.mkv -execdir echo $(file={}; echo ${file/mkv/mp4};) \;

I understand this isn't all you need to re-format a file but this script is part of a larger script that is passed to FFMPEG.

Here is the full command with FFMPEG.

find /Downloads -name \*.mkv -execdir ffmpeg -i {} -vcodec copy -acodec copy $(file={}; echo ${file/mkv/mp4};) \;

2 Answers2

0

You can try using bash -c to execute your command

find /Downloads -name \*.mkv -execdir bash -c 'file={}; echo ${file/.mkv/.mp4}' \;
Jacek Trociński
  • 882
  • 1
  • 8
  • 23
0

The exec and execdir are generally intended to actually execute command not to echo/print info about the files found (print/printf).

There are several ways to do this and here's one.

You could first try using the rename command that can use regex substitution for renaming files. This would require all the files to be renamed to be in the same folder /Downloads: (syntax may very according to the implementation of rename that ships with your distro)

ls *.mkv
a.mkv  b.mkv
rename .mkv .mp4 *.mkv
ls *.mp4
a.mp4
b.mp4

Let's suppose that the mkv files are also present in subdirectories of /Downloads:

find . -type f -name "*.mkv"
./sundir/d.mkv
./sundir/c.mkv
./a.mkv
./b.mkv
find -type f -name "*.mkv" -exec rename .mkv .mp4 {} \;
find . -type f -name "*.mp4"
./sundir/c.mp4
./sundir/d.mp4
./b.mp4
./a.mp4
louigi600
  • 716
  • 6
  • 16