touch
100%

Command Line · Lesson 5

touch

Learn how to create empty files and manage file timestamps with the touch command.

The touch command changes file timestamps. It is also commonly used to create one or more empty files.

The basic syntax is:

touch [OPTIONS] FILE...

Creating Empty Files

If the named file does not exist, touch creates it as an empty file:

$ touch mysuperduperfile

You can create several files in one command by listing each name:

$ touch file1.txt file2.txt file3.log

This is useful for creating placeholders, but touch does not add text to a file. Use a text editor or another command designed to write content when you need a nonempty file.

Which command creates three empty files named one, two, and three if they do not already exist?

Updating File Timestamps

Files track several timestamps. By default, running touch on an existing file changes both its access time and modification time to the current time. It leaves the file's contents unchanged.

You can compare the displayed modification time before and after running the command:

$ ls -l mysuperduperfile
$ touch mysuperduperfile
$ ls -l mysuperduperfile

The ls -l output normally displays the modification time, not the access time.

What happens when you run touch report.txt and report.txt already exists?

Controlling Which Timestamp Changes

Use -a to change only the access time or -m to change only the modification time:

$ touch -a notes.txt
$ touch -m notes.txt

Which command updates only the modification time of notes.txt?

Setting or Copying a Time

The -d option accepts a date string instead of using the current time:

$ touch -d "2026-06-23 12:30:00" mysuperduperfile

To give a file the same access and modification times as a reference file, use -r:

$ touch -r file1.txt file2.txt

Here, file1.txt supplies the timestamps and file2.txt is the file that changes. The -t option is another way to provide a time, using a compact numeric format.

Which command copies the timestamps of source.txt to target.txt?

Avoiding File Creation

Normally, touch creates a file when the named path does not exist. Add -c when you want to update a file only if it already exists:

$ touch -c existing-file.txt

If existing-file.txt is missing, this command does not create it. This behavior can be useful in scripts that should update a timestamp without introducing a new file.

Which command updates status.log if it exists but does not create it if it is missing?

Lesson complete

You finished touch

You can now use touch to create empty files and control file timestamps.

  • Create one or more empty files.

  • Update timestamps without changing file contents.

  • Select the access time or modification time.

  • Set a specific time or copy a reference file's timestamps.

  • Prevent creation of a missing file.

Keep your learning progress

Create a free account to save this lesson and continue learning on any device.

Create a free account
Next Lesson
Back to Command Line