1

I use Synology as storage for films from CCTV cameras. After copying a file from the CCTV system to Synology, I convert it to the AVI format. So my files have names like this:

10.01.07-10.01.48[M][@0][0].dav.avi

I want to rename it recursively from the names like above for names like this:

10.01.07-10.01.48.avi 

Please notice that each filename consist always the same part [M][@0][0].dav.avi

But the problem is that there is no rename command in Synology shell (it sh NOT bash)

I tried to use find command in many different syntaxes but without success. Can You help me to find out solution?

4

You could do:

find . -name '*\[M]\[@0]\[0].dav.avi' -type f -exec sh -c '
  for file do
    echo mv "$file" "${file%"[M][@0][0].dav.avi"}.avi"
  done' sh {} +

(remove echo if that looks all right).

Or if you want to rename all the .dav.avi to .avi, but also remove a [M][@0][0] if there:

find . -name '*.dav.avi' -type f -exec sh -c '
  for file do
    new=${file%.*.*}
    new=${new%"[M][@0][0]"}.avi
    echo mv "$file" "$new"
  done' sh +

Those are POSIX syntax and also work with busybox utilities so should work on synology.

A few notes (based on observations of your own and other answers):

0

Here is a quick solution using sed to rename the files:

echo "10.01.07-10.01.48[M][@0][0].dav.avi" | sed 's/\[.*dav//'
10.01.07-10.01.48.avi

I know nothing about the Synology shell though, but I hope that sed is available!

Use (for example) in a loop:

#!/bin/sh

for file in *avi; do
    newName=$(echo "$file" | sed 's/\[.*dav//')
    mv -v "$file" "$newName" 
done

Before:

ls
09.01.07-13.11.48[M][@0][0].dav.avi
110.01.07-13.11.48[M][@0][0].dav.avi
12.01.07-13.11.48[M][@0][0].dav.avi

Running the script:

./renameAvi.sh 
”09.01.07-13.11.48[M][@0][0].dav.avi” -> ”09.01.07-13.11.48.avi”
”110.01.07-13.11.48[M][@0][0].dav.avi” -> ”110.01.07-13.11.48.avi”
”12.01.07-13.11.48[M][@0][0].dav.avi” -> ”12.01.07-13.11.48.avi”

After:

ls
09.01.07-13.11.48.avi   12.01.07-13.11.48.avi
110.01.07-13.11.48.avi 


Edit: If you want to use find one way to implement is like in this oneliner:

find . -name "*.avi" | while read fileName; do newName=$(echo "$fileName" | sed 's/\[.*dav//'); mv -v "$fileName" "$newName"; done
”./01.01.07-13.11.38[M][@0][0].dav.avi” -> ”./01.01.07-13.11.38.avi”
”./01.01.07-13.11.39[M][@0][0].dav.avi” -> ”./01.01.07-13.11.39.avi”

sed --version
sed (GNU sed) 4.2.2

find --version
find (GNU findutils) 4.4.2
0

For recursive files renaming you should use below command.

find . -name "*dav.avi" | while read fileName; do newName=$(echo "$fileName" | sed 's/\[.*dav//'); mv -v "$fileName" "$newName"; done

Change from the above script

(I could not add the comment above so adding answer here with small change in the script that maulinglawns has already mentioned)

  • Use to search for *dav.avi as you will have many recursive files in your Synology storage, so once this files are renamed it should be done again.
  • It is the very small change but will effect if your actual system is ~GBs of data files.
0

Finally I found out solution. Renaming is done in two steps. It is because it is possible that other CCTV recorders will transmit files with different names but always in DAV format. So simply adding additional line similar to second one, will allow cooperation with other types of recorders:

find $katalog -type f -name *.dav.avi | sed -e "p;s/.dav.avi/\.avi/" | xargs -n2 mv 2>/dev/null
find $katalog -type f -name *.avi | sed -e "p;s/\[M\]\[@0\]\[0\].avi/\.avi/" | xargs -n2 mv 2>/dev/null

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.