Convert a Text File to RTF, HTML, DOC, and more via Command Line
Do you need to convert a text file to RTF, plain text TXT, HTML, DOC, or another familiar document format? The excellent textutil command line utility can make quick work of text file conversion and manipulation on the Mac, and there is no need to download any additional software or tools as it’s built right in to Mac OS.
How to Convert Text File to RTF, HTML, DOC, etc from Mac Terminal
To begin your text conversion, you will need a starting text document that you wish to convert. It can be any text format if you’re testing this out, or use it with a document created entirely for this purpose.
Launch the Terminal application as found in /Applications/Utilities/ and then use the following command syntax:
textutil -convert filetype filename
Conversion options are txt, rtf, rtfd, html, doc, docx, odt, wordml, and webarchive, here’s sample syntax converting a text file named test.txt to an rtf:
textutil -convert rtf test.txt
Textutil will automatically append the filename with the appropriate extension. If you want to give the file a new name in the conversion process, use the -output flag as follows:
textutil -convert rtf test.txt -output NewFileName.rtf
If there’s multiple text documents you want to join together into a single new file, use textutil’s built-in cat function:
textutil -cat rtf file1.txt file2.txt file3.txt -output combinedFiles.rtf
You can quickly verify the conversion was successful by opening the newly created rtf file in TextEdit with:
You can also go the other direction and convert from any of the aforementioned filetypes back to txt, just remember that plaintext does not support any styling so the document will be stripped of any unique fonts, font sizes, styling, or other aspects of a rich text file.
The command line tool textutil allows you to quickly convert text files to many other useful formats and perform other text manipulation tasks. Additional help and options for textutil can be found by summarizing the help tool, or referring to the man page:
Or to get the complete manual page on textutil:
Don’t forget that you can also convert a text file to a spoken audio file, this can be achieved through the command line or by using the simpler “Add to iTunes as Spoken Track” method.