I have a folder include many txt files and sub-folders which also contains many txt files.

I want to change all of the files' encoding automatically. Right now I know how to change one file using the command:

iconv -f gbk -t utf8 sample.txt > sample.wiki

I need the name of the file not changed and change its extension to .wiki. How can I achieve this automatically ?


You can iterate though the .txt files, change encoding, save the output in .wiki file of the same name using -o option of iconv and if successful remove the relevant .txt file:

shopt -s globstar  ## Enables recursive glob matching
for f in **/*.txt; do
    iconv -f gbk -t utf8 -o "${f%.txt}".wiki "$f" && rm "$f"


If the input file is not in given source encoding or already in the target encoding, iconv will return true without doing any operation, hence the .wiki file will not be created and the .txt file will be removed.

To work around this you can save the STDOUT after conversion in a variable and if the variable is not-empty only then save the output in the .wiki file and remove the txt file:

shopt -s globstar  ## Enables recursive glob matching
for f in **/*.txt; do
    out="$(iconv -f gbk -t utf8 "$f")" 
    [[ -n $out ]] && echo "$out" >"${f%.txt}".wiki && rm "$f"
| improve this answer | |
  • This really helps me, can't imagine to do this manually. I have about 200 files and this command line works just right! thanks for the nice answer. – an offer can't refuse Aug 21 '16 at 10:20
  • @buzhidao Glad i could help :) – heemayl Aug 21 '16 at 10:38
  • Neat trick with shell expansion and &&. Would find be faster? I usually -print0 with find , then pipe to xargs or parallel. – sdkks Aug 21 '16 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.