<span>touch</span> command is a commonly used command in Linux and Unix systems for creating empty files or updating the timestamps of existing files.
🔧 Basic Syntax:
touch [options] filename...
✅ Main Functions
- Create empty files (if the file does not exist)
- Update file timestamps (if the file already exists):
- Modification time (mtime)
- Access time (atime)
📌 Common Usage Examples
1. Create one or more empty files
touch file1.txt
If
<span>file1.txt</span>does not exist, it will be created; if it exists, only its timestamp will be updated.
touch file1.txt file2.txt file3.log
Simultaneously create or update multiple files.
2. Update timestamp without modifying file content
touch existing_file.txt
Updates the access and modification times of
<span>existing_file.txt</span>to the current system time.
3. Specify timestamp (using the <span>-t</span> option)
Format:<span>[[CC]YY]MMDDhhmm[.ss]</span>
touch -t 202509181030.00 myreport.txt
Sets the file’s timestamp to September 18, 2025, 10:30:00.
4. Use the timestamp of a reference file (<span>-r</span> option)
touch -r reference.txt target.txt
Sets the timestamp of
<span>target.txt</span>to be the same as that of<span>reference.txt</span>.
5. Create a file without updating the timestamp of existing files (<span>-c</span> option)
touch -c logfile.txt
If the file does not exist, it will be created; if it exists, no modifications will be made (timestamp will not be updated).
6. Create a file with a specified timestamp (combined with <span>-d</span>)
touch -d "last week" oldfile.txt
touch -d "2025-01-01 12:00:00" newyear.txt
Sets the timestamp using a more readable time string.
📚 Summary of Common Options
| Option | Description |
|---|---|
<span>-a</span> |
Update only the access time |
<span>-m</span> |
Update only the modification time, without changing the access time |
<span>-c</span> |
Do not create a new file (no error if the file does not exist) |
<span>-r FILE</span> |
Use the timestamp of the reference file |
<span>-t STAMP</span> |
Use the specified timestamp (format: [[CC]YY]MMDDhhmm[.ss]) |
<span>-d TIME</span> |
Use a human-readable time string (e.g., “next Thursday”, “2 days ago”) |
⚠️ Notes
<span>touch</span>does not change file content.- By default,
<span>touch</span>updates both atime and mtime. - Regular users can only modify the timestamps of files they have write permission for.
- The precision of the time depends on the file system (usually second-level, some support nanoseconds).
💡 Practical Application Scenarios
- Used in scripts to “mark” the completion of a processing stage:
touch /tmp/stage1_done - Used with
<span>find</span>command to test file time conditions:find /var/log -newer reference.log - Used in build systems to trigger Makefile rules (through time dependencies).