Good day.

Need help: you need to write a script that would replace Russian letters in the file name, without touching the extension, in English (at least 3-4 letters).

Help me please!

    2 answers 2

    If there are no Russian letters in the file extension, you can use the following method:

    $ echo "привет" | sed 'y/рвт/rvt/' пrиvеt 

    The y/ streaming sed command replaces the letters of the first group with the corresponding letters from the second.

    If this functionality is not enough, there is always tr .

    • If the replacement is one to one, then tr. and for i in *; do mv $ i tr Русские Нерусские ; done (the tr command with parameters must be enclosed in reverse apostrophes - who else would say how to type this in the comments) - alexlz
    • > who else would say how to type it in the comments Just like in the answer =) You can first format it in the answer field and then insert it into the comment. And, if we are talking about apostrophes, then it seems to be enough to screen with a backslash. `test` - Ilya Pirogov

    In addition to the first answer.

    If this functionality is not enough, there is always tr.

    Please note that tr does not support unicode. Therefore, if the file names are in UTF-8, then tr will not work.

    The y / streaming sed command replaces the letters of the first group with the corresponding letters from the second.

    If it is required, replace one Cyrillic letter with two Latin letters, then you can use the s command. Several teams can be listed through ;

    transliterate.sh

     #!/bin/bash NEW=`echo "$1" | sed ' y/абвгдезийклмнопрстуфхцыАБВГДЕЗИЙКЛМНОПРСТУФХЦЫ/abvgdezijklmnoprstufhcyABVGDEZIJKLMNOPRSTUFHCY/; s/ё/yo/g; s/ж/zh/g; s/ч/ch/g; s/ш/sh/g; s/щ/shh/g; s/э/je/g; s/ю/ju/g; s/я/ja/g; s/Ё/YO/g; s/Ж/ZH/g; s/Ч/CH/g; s/Ш/SH/g; s/Щ/SHH/g; s/Э/JE/g; s/Ю/JU/g; s/Я/JA/g; s/[ъьЪЬ]//g'` mv -v $1 $NEW 

    $ ./transliterate.sh тест «тест» -> «test»

    For batch renaming, you can use the find

     find . -type f -name '*[а-яА-Я]*' -exec ./transliterate.sh {} \;