Sometimes you need to transliterate filenames from cyrilllic to latin in Linux. Doing this manually could take a lot of work if number of files over 10. Installing new software for this tiny thing is not my choice and I just dont want to do it.

So basic script was created to make this task possible. You can put it tp /usr/local/bin folder to make it usable from any part of your system.

Create a file transliterate.sh

#!/bin/sh

# Rename utility.

if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
    echo "usage: $(basename $0) FILE [...]"
    echo
    exit 0
fi
IFS=$'
    '
for f in "$@"
do
    if [ ! -f "$f" ]; then
        echo "$(basename $0) warn: this is not a regular file (skipped): $f" >&2
        continue
    fi

    NEWFILENAME="$(basename "$f")"
    NEWFILENAME="$( echo -n "$NEWFILENAME" | { uconv -x 'Any-Latin;Latin-ASCII' || cat ; } )" # convert non-latin chars using uconv from the icu-devtools package
    NEWFILENAME="$( echo -n "$NEWFILENAME" | iconv -f UTF-8 -t ascii//TRANSLIT//IGNORE )"
    NEWFILENAME="$( echo -n "$NEWFILENAME" | sed -e 's/[+]/_plus_/g' \
        |tr -c '[A-Za-z0-9._\-]' '_' \
        | tr '\[\]' '_' \
        | sed -e 's/__*/_/g' \
        | sed -e 's/_\././g' )"

    if [ -f "$(basename $f)/$NEWFILENAME" ]; then
        echo "$(basename $0) warn: target filename already exists (skipped): $(basename $f)/$NEWFILENAME" >&2
        continue
    fi
    if [ "$(basename $f)" != "$NEWFILENAME" ]; then
        echo "\`$f' -> \`$NEWFILENAME'"
        mv -i "$f" "$NEWFILENAME"
    fi
done

Run it from cli

$ ./transliterate.sh FILENAME

To transliterate files in batch in one folder use find command:

$ find . -iname "*.jpg" -exec ./transliterate.sh {} \;

This command will search for jpg files and send them one by one into our script.

https://gist.github.com/onesixromcom/883186ff031d146744eb886b81b1a5d2

Add new comment

The content of this field is kept private and will not be shown publicly.
  • No HTML tags allowed.
CAPTCHA
              oooooooo      .                            .o8       o8o 
dP""""""" .o8 "888 `"'
.ooooo. d88888b. .o888oo oooo oooo ooo .oooo888 oooo
d88' `"Y8 `Y88b 888 `88. `88. .8' d88' `888 `888
888 ]88 888 `88..]88..8' 888 888 888
888 .o8 o. .88P 888 . `888'`888' 888 888 888
`Y8bod8P' `8bd88P' "888" `8' `8' `Y8bod88P" 888
888
.o. 88P
`Y888P
Enter the code depicted in ASCII art style.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.