Search code examples
linuxencodingfindiconv

Linux find reencode all files to subdirectory


I'm trying to reencode all files in a directory and put the results in a subdirectory. I'm using

find . -type f -name '*.txt' -execdir iconv -f utf-16 -t utf-8 {} >  reencoded/{} \;

But the filename does not replace the second occurrence of '{}', there is a result in reencoded/{} instead.


Solution

  • Wrap the command inside a call to sh -c, which can then reference the {} as $0:

    find . -type f -name '*.txt' -execdir sh -c 'iconv -f utf-16 -t utf-8 "$0" >  reencoded/"$0"' {} \;