Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

To create an empty file in a Unix shell, run touch filename. To create a file with text, use printf '%sn' 'your text' > filename. Use >> instead of > to append without deleting existing contents.

Create an empty file

Run touch followed by the file path:

touch notes.txt

On GNU/Linux, GNU Coreutils 9.11 documents that touch creates a missing file empty by default. If the file already exists, it leaves its contents intact but updates its access and/or modification timestamps. That timestamp behavior is why touch is more than a file-creation command. See the GNU Coreutils 9.11 touch documentation.

If you want to create an empty file with shell redirection instead, use:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
: > notes.txt

The colon command produces no output; > opens the path for output. Unlike touch, this empties an existing file. POSIX shell redirection creates a missing file and truncates an existing one when opening it for output, as described in The Open Group’s POSIX.1-2017 Shell Command Language.

Create a file that contains text

Use printf to supply the text and > to direct it into a file:

printf '%sn' 'First line' > notes.txt

This creates notes.txt if it is missing. If it already exists, > replaces its contents, so use this only when overwriting is intended.

Add text without replacing existing contents

Use >> to append a line to a file:

printf '%sn' 'Another line' >> notes.txt

Append redirection creates the file if it does not exist and adds the output after existing content if it does. For data already coming through a pipe, tee can write it to a file and also display it:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
printf '%sn' 'Hello' | tee greeting.txt

By default, tee overwrites an existing output file. Add -a to append instead:

printf '%sn' 'More' | tee -a greeting.txt

These file-creation and overwrite behaviors are documented in the GNU Coreutils 9.11 manual.

Choose the command by its effect

Goal Command Effect if the file exists
Create an empty file touch notes.txt Preserves contents; updates timestamps.
Create an empty file or empty an existing one : > notes.txt Truncates it to empty.
Write text or replace existing contents printf '%sn' 'First line' > notes.txt Replaces existing contents.
Append text printf '%sn' 'Another line' >> notes.txt Preserves existing contents and adds the new line.

If the file is not created

Check that the path is spelled as intended and that you can write to its parent directory. Shell redirection must open or create the destination before the command runs; it fails if that operation is not permitted or the path is invalid. The details of numeric permission modes vary by system and are not needed to choose between these creation methods.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Portability note

The redirection examples use POSIX shell behavior. The specific touch details above refer to GNU Coreutils 9.11, commonly used on GNU/Linux; Unix-like systems may ship different utility implementations and options. The POSIX touch specification also describes creating a missing operand.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.