October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

Create a File in Unix: Empty Files, Text, and Appending

Use touch for an empty file, printf with > to write or replace text, and >> to append without losing existing contents.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use touch filename to create an empty file, or use shell redirection with printf when you want to put text in it. The key difference: touch leaves existing file contents intact, while > replaces them and >> appends.

Create an empty file without changing its contents

Run touch with the file name:

touch notes.txt

If notes.txt does not exist, GNU Coreutils 9.11 creates it as an empty file. If it already exists, touch updates its timestamps rather than clearing its contents. It is primarily a timestamp utility; creating a missing file is one of its effects. See the GNU Coreutils 9.11 touch documentation.

As an Amazon Associate I earn from qualifying purchases.

For an empty file, shell redirection is another option:

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

The colon is a shell built-in that produces no output, and > sends that output to the file. Unlike touch, this empties the file if it already exists.

Create a file and write text to it

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

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

If the file is missing, the shell creates it. If it already exists, the shell truncates it before writing, replacing its previous contents. The POSIX.1-2017 Shell Command Language specification defines these redirection behaviors.

Append text without replacing what is there

Use >> when new text should follow existing contents:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
printf '%sn' 'Another line' >> notes.txt

If the file does not exist, it is created; if it does exist, the new output is appended. This is the safer choice when you need to keep the file’s existing text.

Write piped output with tee

tee copies standard input to both standard output and a named file:

printf '%sn' 'Hello' | tee greeting.txt

A missing output file is created. By default, an existing file is overwritten; add -a to append instead:

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

GNU documents these behaviors in its Coreutils manual.

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.

Choose the command by its effect

Goal Command Effect if the file already exists
Create an empty file or leave existing contents alone touch notes.txt Preserves contents and updates timestamps
Create a file or make it empty : > notes.txt Truncates it to empty
Write text or replace existing text printf '%sn' 'First line' > notes.txt Replaces existing contents
Add text while keeping existing contents printf '%sn' 'Another line' >> notes.txt Appends
Send piped output to a file and the terminal printf '%sn' 'Hello' | tee notes.txt Overwrites by default; use tee -a to append
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

If the file cannot be created

Check the spelling and location of the path, then confirm that you can write to its parent directory. Shell redirection fails when the destination cannot be opened or created, for example because the directory is not writable. POSIX specifies the redirection operation but does not establish a single numeric permission mode that applies to every setup.

What is portable across Unix-like systems?

The redirection examples describe POSIX shell behavior. The touch behavior cited here comes from GNU Coreutils 9.11, which is applicable to GNU/Linux; Unix-like systems may ship other utility implementations, so GNU-specific options should not be assumed to work identically everywhere. The POSIX touch specification also describes creating a missing file.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.