I'm trying to come up with a short script to edit some csv files. First, I need to replace delimiters, then change encoding to UTF-16. I would like to do this in two steps, saving the results in two different folders, in case something goes wrong.

The first step (replacing delimiters) I've managed to solve like this and it works fairly well:

mkdir ./01_delimiters
find .  -maxdepth 1 -type f -name '*.csv' -exec bash -c 'sed -e "s/|/¦/g" -e "s/╬/|/g" "{}" > ./01_delimiters/"{}"' \;

Now I'd like to take all the files from the 01_delimiters folder, change their encoding and save them into a folder ./02_encoding. I've tried several versions of the following but it doesn't work:

mkdir ./02_encoding
find ./01_delimiters -maxdepth 1 -type f -name '*.csv' -exec bash -c 'iconv -f utf-8 -t utf-16BE "{}" > ./02_encoding/"{}"' \;

It finds the files alright, but throws a 'No such file or directory' error for the output. Any ideas how to make this work? I'm using Ubuntu, by the way. Thanks a lot in advance!


1 Answer 1


The {} is replaced with the full path. This works in the first command, because there there the path is just ./file.csv. In the second command, the name is ./01_delimiters/file.csv, so it tries to create ./02_encoding/./01_delimiters/file.csv.

You can

  • mkdir /02_encoding/./01_delimiters, then your output files will be placed in that directory.
  • Combine both operations into one after you verified that the first one is working. Even if something is wrong, you didn't delete the original files.
  • Change the second command to

    (cd ./01_delimiters; find . -maxdepth 1 -type f -name '*.csv' -exec bash -c 'iconv -f utf-8 -t utf-16BE "{}" > ../02_encoding/"{}"' \; )
  • Just use the shell

    for file in *.csv; do
      sed -e "s/|/¦/g" -e "s/╬/|/g" "$file" > "./01_delimiters/$file"
      iconv -f utf-8 -t utf-16BE "./01_delimiters/$file" > "./02_encoding/$file"
  • 1
    The find command here and in the question has a serious code injection vulnerability. Don't embed {} in shell code. Use -exec bash -c '...' bash {} ';' and then $1 in the code.
    – Kusalananda
    Oct 10, 2018 at 9:52

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.