Categorized | Linux

How to batch repair file and folder names encoding on windows after a windows->linux file transfer?

Posted on 11 April 2010 by Abidoon

For examples, folders named “Équipement” are now named “équipement”. I guess linux changed the ISO encoding to UTF-8 one. Anyone know a way to fix it?

Tags | , , , , , , , , ,

One Response to “How to batch repair file and folder names encoding on windows after a windows->linux file transfer?”

  1. rlfwolf says:

    Assuming you want to convert the filenames from ISO to UTF8 then here is a possible solution. Open a shell and cd into the base of the directory structure you want to convert. Then paste the following in a single line first:
    for name in `find . -depth `; do new_name=`echo $name | iconv -f ISO-8859-1 -t UTF-8`; [ "$new_name" != "$name" ] && echo “$name => $new_name”; done

    Once you’ve verified that it’s going to do the right thing, then paste and execute this:
    for name in `find . -depth `; do new_name=`echo $name | iconv -f ISO-8859-1 -t UTF-8`; [ "$new_name" != "$name" ] && mv “$name” “$new_name”; done

    If you are more comfortable working with a shell script in a file, here’s the same thing broken out to separate lines you can put in a file to execute. Don’t forget to chmod it after you create it.
    #!/bin/sh

    for name in `find . -depth `; do
    new_name=`echo $name | iconv -f ISO-8859-1 -t UTF-8`
    # new_name=`echo $name | iconv -f UTF-8 -t ISO-8859-1`
    if [ "$new_name" != "$name" ]; then
    echo “$name => $new_name”
    # mv “$name” “$new_name”
    fi
    done
    ## END SCRIPT

    Start out with the move (mv) line commented out to make sure it’ll do the right thing before it changes anything. Then, uncomment that line and run again to actually rename the files. You can reverse the conversion (UTF-8 to ISO-8859-1 instead of ISO-8859-1 to UTF-8) by switching the parameters for the iconv command. I’ve put the commented line so you can swap between the two options. Just comment out one or the other.

    Good luck.


Leave a Reply