Skip to content

Instantly share code, notes, and snippets.

@rodrigodev
Created December 8, 2020 21:26
Show Gist options
  • Select an option

  • Save rodrigodev/e4dc6d5c654969a7e6492a97603e503b to your computer and use it in GitHub Desktop.

Select an option

Save rodrigodev/e4dc6d5c654969a7e6492a97603e503b to your computer and use it in GitHub Desktop.

Revisions

  1. rodrigodev created this gist Dec 8, 2020.
    17 changes: 17 additions & 0 deletions iconv-convert-encoding.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    #!/bin/bash
    #enter input encoding here
    FROM_ENCODING="ISO_8859-1"
    #output encoding(UTF-8)
    TO_ENCODING="UTF-8"
    #convert
    CONVERT="iconv -f $FROM_ENCODING -t $TO_ENCODING"
    #loop to convert multiple files
    for file in ./*.txt; do
    encoding=$(file -I "$file" | awk '{print $3}')
    if [ "$encoding" == "charset=iso-8859-1" ]; then
    $CONVERT "$file" >"$file.new" &&
    mv -f "$file.new" "$file"
    fi

    done
    exit 0