Command Line · Lesson 5 of 19
touch
0 / 5 checkpoints answered
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.
CheckWhich command creates three empty files named one, two, and three if they do not already exist?
press 1/2/3 to answer · → next lesson
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.
CheckWhat happens when you run touch report.txt and report.txt already exists?
press 1/2/3 to answer · → next lesson
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
CheckWhich command updates only the modification time of notes.txt?
press 1/2/3 to answer · → next lesson
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.
CheckWhich command copies the timestamps of source.txt to target.txt?
press 1/2/3 to answer · → next lesson
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.
CheckWhich command updates status.log if it exists but does not create it if it is missing?
press 1/2/3 to answer · → next lesson
Summary
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.
Lesson complete
You finished touch. Nice work — keep the momentum going.