Related
Quick Links
Summary
The touch command does more than make empty files.
It updates access and modification times, ensuring routines like make scripts and backups include the files you want.
Heres how to use it.
What Is the touch Command?
The touch command is part of theGNU core utilities, and should be present on all Linux distributions.
Its an old tool, dating back to the late 1970s and the release of Unix version 7.
Lucas Gouveia / How-To Geek
It allows you to set the access and modification times of files.
It just so happens that its default action is to create the file too, if it doesnt exist.
It gives you quite a selection of ways to specify the times.
Still, Id guess its most common use probably leverages its happy side-effect of creating empty files.
Creating Empty Files
Creating files with touch is childs play.
The files are created in their requested locations.
To create multiple files, provide a list of their names on the command line.
We can use thestat commandto see what its timestamps are.
To set the access time to the current PC time, we use the -a (access) option.
The access timestamp has been changed to 10:42:00 on Aug. 6, 2024.
Note that the change timestamp has been updated too.
This is the time that the file was last changed, by any means.
Setting new file permissions, for example, would be enough to update the change timestamp.
Updating the access timestamp is a change, so the change timestamp gets refreshed.
Our modiffication timestamp is now showing 10:42:50 on Aug. 6, 2024.
The only difference between them is the format you supply the time and date on the command line.
The -t (stamp) option requires a different, less friendly format.
Seconds, century, and year figures are optional.
is used to separate the seconds from the minutes.
If you provide a year, the century figures are optional, and this century is assumed.
Lets use the -d option first.
Well set the access and modify timestamps to midnight, on Halloween this year.
The -a and -m set a single timestamp, but to the current time, not a user-specified time.
What about the case where you want to set a single timestamp to an arbitrary time?
We can achieve that too.
Its simple, but a little counter-intuitive.
Using the -t format string, we can write this as 12211145.00.
Were not providing the CC or YY components, so touch will assume we mean the current year.
Note that we’re using both the -m and the -t options here.
The -m option must come first.
We could do the same thing with the access timestamp, by using -at in the command.
We have a file called reference-file.txt.
These are its timestamps.
Well apply these timestamps to our target file, and create another file called sample-2.txt at the same time.
it’s possible for you to override its default action by including the -c (no create) option.
We can see that the modification timestamp of sample-file.txt has been changed, but the missing-in-action.txt hasnt been created.
And only touch can set the file timestamps to whatever you want.
It’s that level of finesse that sets touch apart.