touch (Create Empty File / Update Timestamp)
The touch command is used to create empty files or change file timestamps (access and modification times) in the Linux filesystem.
1. Basic Usage
touch [options] filename
- If the file doesn't exist: Creates an empty file with zero bytes.
- If the file already exists: Updates the "last modified" timestamp to the current time without affecting the content.
2. Key Options
| Option | Name | Description |
|---|---|---|
-a |
access | Changes only the access time (atime). |
-m |
modification | Changes only the modification time (mtime). |
-t |
timestamp | Uses a specific time instead of the current time. (Format: [[CC]YY]MMDDhhmm[.ss]) |
-r |
reference | Uses the timestamp of a reference file for the target file. |
-c |
no-create | Does not create a new file if it doesn't already exist. |
3. Practical Examples
① Creating Multiple Empty Files
touch app.py utils.py README.md
② Changing a File's Date to the Past (e.g., Jan 1, 2024, 10:30 AM)
touch -t 202401011030 archive.zip
③ Syncing Timestamps Between Two Files
touch -r original.txt duplicate.txt
# Sets duplicate.txt's time to match original.txt.
4. [Tip] Understanding the 3 Timestamps (via stat command)
You can view detailed file times using stat filename:
1. Access (atime): The last time the file was read.
2. Modify (mtime): The last time the file's content was edited.
3. Change (ctime): The last time the file's metadata (permissions, owner) was changed.