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.