Working with files

Linux commands: touch

Learn how the Linux touch command creates a new empty file, and how running it on a file that already exists just updates that file's modification timestamp.

8 minute lesson

~~~

touch updates a file’s access and modification timestamps. When the path does not exist, it creates an empty file:

touch apple.txt

Inspect the result:

ls -l apple.txt
touch apple.txt
ls -l apple.txt

The second touch does not open the file in write mode and does not erase its contents. It changes timestamps. This distinction makes it safe for build tools that use modification time to decide whether work is stale.

Use -c when you want to update an existing path without creating it:

touch -c optional.txt

You can set a specific timestamp with -t, although the exact accepted format is easy to mistype. Prefer copying a known timestamp when that expresses the goal:

touch -r source.txt destination.txt

Creating a file with touch says nothing about its contents or permissions beyond the normal defaults. Use redirection when you need content, for example printf '%s\n' 'hello' > greeting.txt.

Try it: put text in a file, record ls -l and stat output, run touch, and verify that the text stays while the modification time changes.

Lesson completed

Take this course offline

Get every free book and course as PDF and EPUB files.

Get the download library →