touch
Overview
The touch
command changes file timestamps. It’s commonly used to create empty files or update access and modification times of existing files.
Syntax
touch [options] file...
Common Options
Option | Description |
---|---|
-a |
Change access time only |
-m |
Change modification time only |
-c |
Don’t create new files |
-d time |
Use specified time |
-r ref_file |
Use ref_file’s times |
-t time |
Use specified time [[CC]YY]MMDDhhmm[.ss] |
--time=WORD |
Change specified time: access, modify, change |
--date=STRING |
Parse STRING and use it for time |
--no-create |
Don’t create new files |
Key Use Cases
- Create empty files
- Update timestamps
- Batch file creation
- File time synchronization
- File existence checking
Examples with Explanations
Example 1: Create File
touch newfile
Create empty file or update timestamp
Example 2: Specific Time
touch -t 202312201200 file
Set timestamp to specified date/time
Example 3: Reference File
touch -r ref_file target_file
Copy timestamps from ref_file
Understanding Output
- No output by default
- Error messages for:
- Permission denied
- Invalid date format
- Directory not writable
- Invalid option
Common Usage Patterns
Create multiple files:
touch file1 file2 file3
Update access time:
touch -a file
Set specific date:
touch -d "2 days ago" file
Performance Analysis
- Fast operation
- Minimal system impact
- Inode updates only
- No data modification
- Multiple file efficiency
Additional Resources
Best Practices
- Use -c to prevent accidental creation
- Verify timestamp format
- Check file permissions
- Consider timezone impact
- Use with find for batch operations