Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

keith3d

macrumors newbie
Original poster
Feb 18, 2018
1
0
Hi - I have about 2000 text files I need to convert from MacRoman encoding to UTF-8. I discovered that I can do this using the iconv command like this: iconv -f MACROMAN -t UTF-8 *.txt > results.txt but this concatenates all of the converted files into a single file. What I want is to retain the original file names (or possibly append "8" to them). I've also tried iconv -f MACROMAN -t utf-8 "{}" > results/"{}" but I just get an error message telling me that the directory "{}" doesn't exist. I'm also struggling to specify paths in commands - I thought I knew how to do this, but apparently not. Do I have to go right back to the root directory when I enter a path?

Any help ENORMOUSLY appreciated. I've spent hours on this!
 

neliason

macrumors 6502a
Oct 1, 2015
508
1,257
You could use the find command. It will find any files matching whatever name pattern you specificy. It has an ‘-exec’ option. This species a command to run on each file. I would think that would do what you want.
 

mfram

Contributor
Jan 23, 2010
1,333
380
San Diego, CA USA
That's correct, the 'find' command is likely the easiest way to accomplish what you want. I can give a couple of examples. Using find with "-exec" causes the find command to execute the given command on each file. When doing this, it will replace {} with the filename. That's why you see that sequence in command examples. The find command takes a directory name as its primary argument. If you already used a 'cd' command to get to the appropriate directory, then "." is the current directory. The find command traverses the complete directory structure under that directory, so I suggest using the -print command to verify the list of files it is going to act on.

Print all files called "*.txt" under the current directory:
Code:
find . -name '*.txt' -print

Convert all *.txt files under the directory "filedir" and put the output into a file name appended with .utf8:
Code:
find filedir -name '*.txt' -print -exec  iconv -f MACROMAN -t UTF-8 "{}" > "{}.utf8" \;

Convert all files under the directory filedir and place in the directory "results" under your home directory:
Code:
 find filedir -type f -print -exec  iconv -f MACROMAN -t UTF-8 "{}" > ~/results/"{}" \;

If you need to edit the pathname of the output files, then you will likely need a more complicated shell script that creates the final path you are looking for.

You can test the commands that find is going to do by adding 'echo' to the front of it. I'd recommend you do that until you get used to how find works.

Code:
 find filedir -type f  -exec  echo iconv -f MACROMAN -t UTF-8 "{}" > ~/results/"{}" \;

I put double quotes around the paths in the example command because if the file or directory names have spaces in them, then the double quotes are needed.
 
Last edited:
  • Like
Reactions: neliason
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.