RTF files created using the ‘touch’ command will not open

snow leopardterminal

I'm no expert on CLI or using the terminal or anything, but I like playing around with my computer.

When I create a .txt .html or .md file or whatever using the 'touch' command in terminal, they work fine. But .rtf file I create just give me the error

"The document “foo.rtf” could not be opened."

What am I doing wrong? Are you not able to create files in this manner?

Best Answer

As you've likely discovered, touching a nonexistent file will create a zero-byte file with the name you specify. (touch is also used to update the modification time on an existing file. See man touch for further details.)

The problem you're running into is that though a completely empty file is valid HTML, text, or Markdown, it is not valid RTF—this is true of many file formats, actually, which are structured in a particular way and require some data to be present be valid.

Based on my experimentation, the bare minimum RTF file appears to be

{\rtf1}

If you'd like to create a blank RTF file from the shell, try this:

echo '{\rtf1}' >Foo.rtf 

Note the above will overwrite Foo.rtf if it exists.