1

I have a great script that I use to convert encoding in srt.files. I also created an alias for that script so I can enter the directory where I want to run the script and use my alias konvert.

However, I have now the the following scenario: more then 20 directories with srt.files inside. What should I do in order to point my script to all those directories with just one command?

Luckily all these directories are together inside one main directory, so I believe there should be an easy way to do this.

Content of the script

#!/bin/bash for file in *.srt; do iconv -f CP1250 -t UTF-8 -o "$file".utf "$file" && mv "$file".utf "$file"; done 
Zanna
  • 69,223
  • 56
  • 216
  • 327
  • @Zanna My script (I posted the code in my question) works great to convert .srt files when I `cd` into the directory where the files are stored. But I coudn't find the way to do that from a root directory that contains other directories with .srt files. Firstly I thought that I would find a way by taking advantage of that script but in the end it comes out that the better and moste effective solution is by using a different command. That's why, ultimately, I accepted the solution in the answer with command `recode`. It really did the job. – Dunav Rajna Feb 02 '18 at 08:53
  • Related: [Command to perform a recursive chmod to make all sh files in a directory executable](https://askubuntu.com/questions/889344/command-to-perform-a-recursive-chmod-to-make-all-sh-files-within-a-directory-ex) – Zanna Feb 02 '18 at 09:05

1 Answers1

3

Easy enough to accomplish without your script by using the following command line (using the application recode rather than straight iconv) which should be run from the root directory of your srt files:

find . -name '*.srt' -type f -exec bash -c 'recode -v CP1250..UTF-8 "$0"' {} \;

The command line searches recursively for all srt files and when each is found recode works on each file to change character encoding from CP1250 to UTF-8. With recode the encoding alteration does not require the clumsy filename shifting required by iconv...

How cool is the command line :)

andrew.46
  • 37,085
  • 25
  • 149
  • 228
  • Thank you, @andrew.46 your altered answer with the command `recode` did a better job than my last variation with command `iconv`. Although I had to install `recode` it was worth it! :) – Dunav Rajna Feb 02 '18 at 08:04
  • @DunavRajna This is great news! And I see as well that the askubuntu community has reopened your question so extra great news :) – andrew.46 Feb 02 '18 at 20:27