Search code examples
linuxiconv

iconv inside for loop can't find output directory


I have this code:

for file in Folder/*; do iconv -f iso-8859-1 -t UTF-8 $file /Users/username/Desktop/LatinToUTF8.txt ; done

I'm running it in Terminal on Mac OS X High Sierra. The command loops through all the files in the folder but I'm getting the error:

iconv: /Users/username/Desktop/LatinToUTF8test.txt: No such file or directory

Any ideas what I'm doing wrong?

The weird thing is that I've tested this iconv code outside of the for loop and it works fine there. Ie. this code works fine on it's own:

iconv -f iso-8859-1 -t UTF-8 /Users/username/Desktop/Document.txt /Users/username/Desktop/LatinToUTF8.txt ; done


Solution

  • Following the man page, iconv writes to stdout by default. It should be:

    iconv -f iso-8859-1 -t UTF-8 "${file}" > /Users/username/Desktop/LatinToUTF8.txt
    

    or, using the -o option:

    iconv -f iso-8859-1 -t UTF-8 "${file}" -o /Users/username/Desktop/LatinToUTF8.txt