How to Create Files and Update Timestamps with touch
The touch command creates empty files and updates file timestamps. touch notes.txt creates the file if it does not exist, and updates its modification time if it does. It is a small command with two handy jobs.
Create an empty file
The simplest use: create a file that does not exist yet. If it exists, touch just refreshes its timestamp. Create several at once by naming them all.
Placeholder files
Empty files are useful as placeholders. A .gitkeep makes Git track an otherwise empty folder, and lock files signal that a process is running.
Set a specific time with -t
Use -t with a timestamp in the form YYYYMMDDhhmm to set the modification time exactly:
Set the timestamp to now
To refresh a file's modification time to the current moment, just run touch on it with no options. This is how you mark a file as recently changed for build systems that watch timestamps.
Create files in a new folder
touch fails if the parent folder is missing. Create it first with mkdir -p. See touch for the full reference.
Keep learning
Related guides: How to Create and Remove Directories with mkdir and rmdir · The Linux ls Command: List Files Like a Pro · How to Copy Files and Directories with the Linux cp Command. Read all tutorials on the Learn Linux blog.