textutil
is a powerful command-line utility included with macOS that is used for converting and manipulating text files. It supports a variety of formats including plain text, Rich Text Format (RTF), Rich Text Format Directory (RTFD), HTML, Word (doc, docx), OpenDocument Text (odt), WordPerfect (wp), and Webarchive.
The general syntax for using textutil
is as follows:
textutil -convert format [-output file] [-cat format] [-info] [-extension ext] [-inputencoding enc] [-outputencoding enc] file...
Here are some examples of how textutil
can be used:
-
Convert a single file to another format:
textutil -convert txt Document.docx
-
Specify the output file name:
textutil -convert txt -output Output.txt Document.docx
-
Convert multiple files at once:
textutil -convert txt Document1.docx Document2.docx Document3.docx
-
Concatenate files:
textutil -cat txt -output Output.txt Document1.txt Document2.txt
-
Change the text encoding of a file:
textutil -convert txt -inputencoding ISO-8859-1 -outputencoding UTF-8 Document.txt
-
Extract the text content from an HTML file:
textutil -convert txt Document.html
-
Create a Word document from a text file:
textutil -convert docx Document.txt
-
View the information of a file:
textutil -info Document.docx
-
Converting to an OpenDocument Text (odt) file:
textutil -convert odt Document.docx
-
Converting to a WordPerfect (wp) file:
textutil -convert wp Document.docx
-
Converting to a webarchive file:
textutil -convert webarchive Document.html
-
Concatenating files of different formats:
textutil -cat docx -output Output.docx Document.txt Document.docx
-
Converting a file to HTML with specified text encoding:
textutil -convert html -outputencoding UTF-8 Document.docx
-
Converting an RTF file to plain text:
textutil -convert txt Document.rtf
-
Converting all files of a certain type in a directory:
textutil -convert txt *.docx
-
Creating a Rich Text Format Directory (RTFD) file from a text file:
textutil -convert rtfd Document.txt
-
Concatenating and converting files:
textutil -cat docx -output Combined.docx File1.txt File2.txt File3.txt
-
Converting a Word document to a
.pdf
usingcupsfilter
:textutil -convert ps Document.docx cupsfilter Document.ps > Document.pdf
-
Read from standard input (
stdin
) and output to standard file:echo "This is some text"
| textutil -convert rtf -stdin -output Output.rtf ```
-
Output to standard output (
stdout
):textutil -convert txt -stdout Document.docx
-
Specify the text encoding for output files:
textutil -convert txt -encoding ISO-8859-1 Document.docx
-
Specify the input text encoding:
textutil -convert docx -inputencoding ISO-8859-1 Document.txt
-
Force the input files to be interpreted in a specific format:
textutil -convert txt -format docx Document.docx
-
Specify font and size when converting plain text to rich text:
textutil -convert rtf -font Helvetica -fontsize 12 Document.txt
-
Exclude specific HTML elements from the output:
textutil -convert txt -excludedelements "(script, style)" Document.html
Remember, in all these examples, replace Document.docx
, Document.html
, etc. with the actual paths to your files. All these commands are run from the terminal application.
To get more detailed information about textutil
and its usage, you can view its manual page by typing man textutil
in the Terminal. The manual page will provide a detailed description of how to use textutil
, including a list of all its options and examples of its usage.
That's the basic guide to using textutil
. It's a powerful tool that can be extremely useful for working with different text file formats on a macOS system. Be sure to replace the filenames and paths in the examples with those that match your actual files and directory structure.