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 filenameCreate multiple empty files
touch file1 file2 file3Update the timestamp of an existing file
touch filename
Common Options
Change only the access time
touch -a filenameChange only the modification time
touch -m filenameUse a specific date and time
touch -t [[CC]YY]MMDDhhmm[.ss] filenameReference another file's timestamps
touch -r reference_file filenameDo not create files (only change timestamps)
touch -c filename
Examples
Create a single empty file
touch file.txtCreate multiple empty files
touch file1.txt file2.txt file3.txtUpdate the timestamp of an existing file
touch existing_file.txtChange only the access time
touch -a file.txtChange only the modification time
touch -m file.txtSet a specific date and time
touch -t 202406302359.59 file.txtReference another file's timestamps
touch -r reference_file.txt target_file.txtDo 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.txtUse the current time as the timestamp
touch -d "now" file.txtSet the timestamp to a specific date using
-dtouch -d "2023-06-30 23:59:59" file.txt
Practical Tips
Creating Empty Files: Use
touchto quickly create placeholder files or to create files required by scripts or applications.Updating Timestamps: Use
touchto update the access or modification times of files without modifying their contents.Setting Specific Timestamps: Use the
-tor-doptions to set specific timestamps, which can be useful for testing or when working with log files.
Quick Reference
Create an empty file:
touch filenameUpdate access time:
touch -a filenameUpdate modification time:
touch -m filenameSet specific date and time:
touch -t [[CC]YY]MMDDhhmm[.ss] filenameReference another file's timestamps:
touch -r reference_file filenameDo 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.




