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 unzip Command: List and Extract ZIP Archives

Linux unzip Command: List and Extract ZIP Archives

Learn how to list and Extract ZIP Archives with the Linux unzip command, including practical examples, key options, and important precautions.

Linux comm Command: Compare Two Sorted Files

Linux comm Command: Compare Two Sorted Files

Learn how to compare Two Sorted Files with the Linux comm command, including practical examples, key options, and important precautions.

Linux fg Command: Bring a Shell Job to the Foreground

Linux fg Command: Bring a Shell Job to the Foreground

Learn how to bring a Shell Job to the Foreground with the Linux fg command, including practical examples, key options, and important precautions.

Linux jobs Command: List Jobs in the Current Shell

Linux jobs Command: List Jobs in the Current Shell

Learn how to list Jobs in the Current Shell with the Linux jobs command, including practical examples, key options, and important precautions.

Linux clear Command: Clear the Terminal Display

Linux clear Command: Clear the Terminal Display

Learn how to use the Linux clear command to clear the visible terminal screen and understand scrollback behavior, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux ls Command: List Files and Directories

Linux ls Command: List Files and Directories

Learn how to use the Linux ls command to list files and directories with permissions, sizes, timestamps, and sorting options, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux kill Command: Send a Signal to a Process ID

Linux kill Command: Send a Signal to a Process ID

Learn how to send a Signal to a Process ID with the Linux kill command, including practical examples, key options, and important precautions.

Linux Tutorial / Navigate Directories with pwd, ls, cd, and tree

Linux Tutorial / Navigate Directories with pwd, ls, cd, and tree

Use pwd, ls, cd, and tree to find your current location, inspect directories, navigate paths, and check where each command takes you.

Linux whatis Command: Show One-Line Command Descriptions

Linux whatis Command: Show One-Line Command Descriptions

Learn how to use the Linux whatis command to display concise descriptions of commands and manual pages, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux Tutorial / Find Files and Commands with find, locate, which, and whereis

Linux Tutorial / Find Files and Commands with find, locate, which, and whereis

Compare find and locate for file searches, then use which, whereis, and Bash type to distinguish executable paths from shell command resolution.