-1

How can i batch rename my files with specific command by mv?

find /opt/media/rec -maxdepth 1 -type f -name '*.mp4' -mmin +1 -exec basename {} \; | awk -F_ -v OFS="_" '{sub(/\.[^.]*$/,"",$3);$3=gensub(/-([^-]*$)/,"_\\1","g",$3);gsub(/\./,"-",$3);gsub(/\./,"-",$2);;print $3,$1,$2,$4}'

I have a directory /opt/media/rec. In this directory stored recordings of live stream from WOWZA. I need to rename files to another name (for example, i have "now_360p_2014-05-19-18.12.49.996-FET_0.mp4", by the upper comand with awk file name transform to other name "2014-05-19_18-12-01_now_720p_0.mp4"). I do this script

#!/bin/bash

for f in $(find /opt/media/rec/ -maxdepth 1 -iname "*.mp4");
do
mv
awk -F_ -v OFS="_" '{sub(/\.[^.]*$/,"",$3);
                    $3=gensub(/-([^-]*$)/,"_\\1","g",$3);
                    gsub(/\./,"-",$3);
                    print $3,$1,$2,$4}'
done

but i have error:

[root@media rec]# sh autorename.sh
: command not found2:
'utorename.sh: line 3: syntax error near unexpected token `
'utorename.sh: line 3: `for f in $(find /opt/media/rec/ -maxdepth 1 -iname "*.mp4");
that other guy
  • 116,971
  • 11
  • 170
  • 194
NZT
  • 53
  • 5
  • We need a little more detail (please read the [FAQ] and [Ask]). What isn't working? What do you expect to happen? What actually happens? What is the question? – Jim Garrison May 19 '14 at 15:38
  • Not really understand what has the `basename` and `awk` with the `mv`? Could you be more specific? (and here is nowhere your `for`). :( – clt60 May 19 '14 at 15:39
  • You really don't understand how this works. I suggest you leave your files alone otherwise you'll end up deleting them by accident. – Ahmed Masud May 19 '14 at 15:57
  • Yes, i am newbie, really dont understand how it works yet. – NZT May 19 '14 at 17:42

1 Answers1

0

You have a telltale garbled error message:

:command not found2:

This is caused by carriage returns in the output, because your script has Windows \r\n line separators.

You should run your script through tr -d '\r' or dos2unix. See the bash tag wiki, Step 1, for more information.

You can save yourself some trouble by installing appropriate tooling. Here's a screenshot of your code in Vim with Syntastic and ShellCheck. The problem and solution is automatically pointed out.

Note that this only fixes your immediate error message and lets you keep going. There's still work to do.

Community
  • 1
  • 1
that other guy
  • 116,971
  • 11
  • 170
  • 194