Cheat Sheet #day49 - touch
touch
Command Cheatsheet
The touch
command in Unix/Linux is used primarily to create empty files or update the access and modification times of existing files. Below are the basic and advanced usages of the touch
command along with practical examples.
Basic Usage
Create an empty file
touch filename
Create multiple empty files
touch file1 file2 file3
Update the timestamp of an existing file
touch filename
Common Options
Change only the access time
touch -a filename
Change only the modification time
touch -m filename
Use a specific date and time
touch -t [[CC]YY]MMDDhhmm[.ss] filename
Reference another file's timestamps
touch -r reference_file filename
Do not create files (only change timestamps)
touch -c filename
Examples
Create a single empty file
touch file.txt
Create multiple empty files
touch file1.txt file2.txt file3.txt
Update the timestamp of an existing file
touch existing_file.txt
Change only the access time
touch -a file.txt
Change only the modification time
touch -m file.txt
Set a specific date and time
touch -t 202406302359.59 file.txt
Reference another file's timestamps
touch -r reference_file.txt target_file.txt
Do not create files if they do not exist
touch -c file.txt
Advanced Usage
Create a file with a specific timestamp
touch -t 202406302359.59 file.txt
Use the current time as the timestamp
touch -d "now" file.txt
Set the timestamp to a specific date using
-d
touch -d "2023-06-30 23:59:59" file.txt
Practical Tips
Creating Empty Files: Use
touch
to quickly create placeholder files or to create files required by scripts or applications.Updating Timestamps: Use
touch
to update the access or modification times of files without modifying their contents.Setting Specific Timestamps: Use the
-t
or-d
options to set specific timestamps, which can be useful for testing or when working with log files.
Quick Reference
Create an empty file:
touch filename
Update access time:
touch -a filename
Update modification time:
touch -m filename
Set specific date and time:
touch -t [[CC]YY]MMDDhhmm[.ss] filename
Reference another file's timestamps:
touch -r reference_file filename
Do not create files:
touch -c filename
This cheatsheet covers the essential commands and options for using touch
effectively, from creating empty files to updating and setting specific timestamps. Adjust the commands according to your specific requirements and environment.
Subscribe to my newsletter
Read articles from Cloud Tuned directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by