touch - macOS
Overview
touch is a command-line tool that creates or updates a file’s timestamp, but does not modify the file’s contents. It is often used to create empty files or update existing files without modifying their contents.
Syntax
touch [-a|-c|-m|-r file] [-t time] [-d date] [file ...]
Options/Flags
- -a: Access time is changed instead of modification time.
- -c: Do not create any files, but only change timestamps of existing files.
- -m: Modification time is changed instead of access time.
- -r file: Use the timestamps of the specified file as the new timestamps for the target files.
- -t time: Use the specified time (in YYYYMMDDhhmmss format) as the new timestamps.
- -d date: Use the specified date (in MMDDhhmmYYYY format) as the new timestamps.
Examples
- Create an empty file named myfile.txt:
touch myfile.txt
- Update the modification time of file.txt to the current time:
touch -m file.txt
- Create a new file newfile.txt with the same timestamps as oldfile.txt:
touch -r oldfile.txt newfile.txt
- Update the access time of test.txt to one hour ago:
touch -a -t "-1hour" test.txt
Common Issues
- Permission denied error: Ensure you have write permissions to the target file or directory.
- Invalid time format error: Time and date formats must follow the specified YYYYMMDDhhmmss and MMDDhhmmYYYY formats, respectively.
Integration
touch can be integrated with other commands for automation tasks, such as:
- Combine with find to update timestamps of all files in a directory:
find . -exec touch -m {} \;
- Use with xargs to process multiple files at once:
ls | xargs touch -m