Creating a File Quickly

You can quickly create an empty file (for later editing) using the touch command, providing a filename that does not exist (see File Properties):

touch newfile

Another quick technique uses the echo command with the -n option, redirecting its output to a new file. This option prevents a newline character from being echoed, making the file truly empty:

echo -n > newfile2

You can also write data into a new file by redirecting the output of a program (see Input/output redirection):

echo anything at all > newfile

If you accidentally provide the name of an existing file, touch will preserve it but redirecting echo will erase its contents. So be careful!

Get Macintosh Terminal Pocket Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.