Linux touch Command: Create Files and Change Timestamps

This guide explains how to use the Linux touch command to create empty files and change access or modification timestamps, including reference times, date formats, and symlink handling. It focuses on practical Linux usage, predictable automation, and the cases where a seemingly small path or option mistake can change the result.

The examples use GNU Coreutils behavior. Check touch --help, man touch, and your distribution documentation when portability or a version-specific option matters.

What Is the touch Command?

touch changes the access and modification timestamps of files. If a named file does not exist, it creates an empty regular file unless an option such as -c prevents creation. It cannot set ctime or a file's birth time to an arbitrary value.

Basic Syntax

touch [OPTION]... FILE...

Quote paths that contain spaces. When supported, place -- before operands that begin with a hyphen so they are not interpreted as options.

Basic touch Usage

Create an empty file

Name a path that does not exist to create a zero-byte file. You can create several files in one command.

touch notes.txt
touch file1.txt file2.txt file3.txt
touch "annual report.txt"

Update timestamps to the current time

When the file already exists, its contents and size remain unchanged while its access and modification times are updated. Use stat to compare the metadata before and after the command.

stat notes.txt
touch notes.txt
stat notes.txt

Do not create missing files

Use -c or --no-create when only existing files should be changed. A missing operand is left missing.

touch -c existing.txt

Checking the execution result

The touch command usually does not display anything on the screen even if it succeeds. To check if the newly created file has a size of 0 bytes, query it as follows.

touch empty-demo.txt
stat -c '%s %n' empty-demo.txt

Example output:

0 empty-demo.txt

If you execute the same command on an existing file, the size and content remain the same, and only the access and modification times may be updated. You can check the difference with stat -c '%x %y'.

Key Options

Option Purpose
-a Change only the access time.
-m Change only the modification time.
-c Do not create missing files.
-d STRING Parse a human-readable date string.
-t STAMP Use the [[CC]YY]MMDDhhmm[.ss] timestamp format.
-r FILE Copy access and modification times from a reference file.
-h Change a symbolic link itself instead of its target where supported.
--time=WORD Select access or modification time.

Long options often make reviewed scripts easier to understand. When a script must run on non-GNU systems, verify every non-POSIX option on the target platform.

Practical Examples

Change access time or modification time separately

The -a and -m switches select which timestamp is changed. Using both is equivalent to changing both fields.

touch -a access-log.txt
touch -m generated.css

Set a specific date and time

Use an explicit timezone in automation so the result does not depend on the machine's local timezone. The compact -t form is useful when a fixed numeric timestamp is required.

touch -d '2026-09-23 14:30:00 +0900' report.txt
touch -t 202609231430.00 report.txt

Copy timestamps from another file

The -r option applies the reference file's access and modification times to the destination. Verify the result when timestamps affect a build or synchronization process.

touch -r reference.txt target.txt
stat reference.txt target.txt

touch and Related Commands

Command or method Primary role Best fit
touch Create an empty file or change atime and mtime Preparing files and timestamps
truncate Change a file's logical size Creating a zero-byte or fixed-size file
date Display and convert dates Date calculations and formatting
stat Read file metadata Verifying timestamp changes

Important Cautions

It does not empty an existing file

Running touch on an existing file preserves its data. Use truncate -s 0 only when you intentionally want to reduce the file to zero bytes.

ctime and birth time are different

The kernel updates ctime when inode metadata changes. touch does not let you assign an arbitrary ctime or birth time.

Timestamp changes can hide dependency problems

Touching files can force or skip timestamp-based build actions. Use it deliberately so it does not conceal an incorrect dependency graph.

FAQ

Does touch delete the contents of an existing file?

No. It preserves the contents and size while changing selected timestamps.

How do I avoid creating a missing file?

Use touch -c FILE.

How do I change only the modification time?

Use touch -m FILE. Use -a for only the access time.

Can I copy timestamps from another file?

Yes. Use touch -r REFERENCE_FILE TARGET_FILE.

Related Resources

More in This Category
Linux whoami Command: Show the Effective User Name

Linux whoami Command: Show the Effective User Name

Learn how to show the Effective User Name with the Linux whoami command, including practical examples, key options, and important precautions.

Linux lscpu Command: Inspect CPU Architecture and Cores

Linux lscpu Command: Inspect CPU Architecture and Cores

Learn how to inspect CPU Architecture and Cores with the Linux lscpu command, including practical examples, key options, and important precautions.

Linux tail Command: View the End of a File and Follow Logs

Linux tail Command: View the End of a File and Follow Logs

Learn how to use the Linux tail command to view the last lines of a file and follow log updates in real time, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux pgrep Command: Find Process IDs by Name or Criteria

Linux pgrep Command: Find Process IDs by Name or Criteria

Learn how to find Process IDs by Name or Criteria with the Linux pgrep command, including practical examples, key options, and important precautions.

Linux unalias Command: Remove Command Aliases

Linux unalias Command: Remove Command Aliases

Learn how to use the Linux unalias command to remove one or all aliases from the current shell session, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux chmod Command: Change File and Directory Permissions

Linux chmod Command: Change File and Directory Permissions

Learn how to change File and Directory Permissions with the Linux chmod command, including practical examples, key options, and important precautions.

Linux xargs Command: Pass Standard Input as Command Arguments

Linux xargs Command: Pass Standard Input as Command Arguments

Learn how to pass Standard Input as Command Arguments with the Linux xargs command, including practical examples, key options, and important precautions.

Linux traceroute Command: Trace Routers on the Way to a Host

Linux traceroute Command: Trace Routers on the Way to a Host

Learn how to trace Routers on the Way to a Host with the Linux traceroute command, including practical examples, key options, and important precautions.

Linux Tutorial / Standard Input, Output, Redirection, and Pipes

Linux Tutorial / Standard Input, Output, Redirection, and Pipes

Understand standard input, output, and error; practice file redirection and pipes; and learn why overwrite risk and redirection order matter.

Linux watch Command: Run a Command Repeatedly

Linux watch Command: Run a Command Repeatedly

Learn how to use the Linux watch command to rerun a command at regular intervals and highlight changing output, with essential options, practical examples, output interpretation, and common troubleshooting tips.