How to Use the touch Command on Linux
Linux & macOS Terminal
Quick Links
-
What Is the touch Command?
-
Creating Empty Files
-
Set the Access Time to the Current Time
-
Set the Modification Time to the Current Time
-
Setting Access and Modification Times to Specific Times
-
Set Only One Time to a Specific Value
-
Set the Access and Modification Times to Those of Another File
-
Don’t Create a File, Only Modify Existing Files
-
A touch of Class
Key Takeaways
- The touch command updates access and modification times of files, useful for ensuring files are selected by backup routines and other processes.
- To create an empty file with touch, use a command like “touch new-file.txt”.
- Use the -m flag to set the modification time to the current time, or the -a flag to set the access time to the current time.
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. Here’s how to use it.
What Is the touch Command?
The touch command is part of the GNU core utilities, and should be present on all Linux distributions. It’s an old tool, dating back to the late 1970s and the release of Unix version 7.
I use it mostly as a quick and easy way to create files, but that’s actually a side effect of its main purpose. 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 doesn’t exist.
touch overwrites a file’s metadata so that it looks like the file was accessed (opened and read), modified (had its contents changed), or both, at a certain time. It gives you quite a selection of ways to specify the times.
Still, I’d guess its most common use probably leverages its happy side-effect of creating empty files.
Creating Empty Files
Creating files with touch is child’s play.
touch new-file.txt
touch ~/Documents/outlines/rough-outline.txt
ls *.txt
ls ~/Documents/outlines/*.txt
The files are created in their requested locations. They’re little more than a filename in the file system at this point,
ls -hl new-file.txt
Our file definitely exists, even though it has a file size of zero bytes.
To create multiple files, provide a list of their names on the command line.
touch new-file-2.txt new-file-3.txt new-file-4.txt
ls *.txt
If the files you’re going to create will have sequentially numbered names, you can create them all in one go like this.
touch even-more-files-{1..6}.txt
ls even*
Set the Access Time to the Current Time
We’re going to be using a sample file to demonstrate the use of touch. We can use the stat command to see what its timestamps are.
stat sample-file.txt
Its access timestamp is 14:32:47 on Aug. 5, 2024, and its modification timestamp is midnight on July 20, 2024.
To set the access time to the current PC time, we use the -a (access) option.
touch -a sample-file.txt
stat sample-file.txt
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.
Set the Modification Time to the Current Time
Setting the modification time is just as straightforward, but we use the -m (modify) option.
touch -m sample-file.txt
stat sample-file.txt
Our modiffication timestamp is now showing 10:42:50 on Aug. 6, 2024.
Setting Access and Modification Times to Specific Times
There are two ways to set both the access and the modification timestamps to a time of your choosing. That is, they don’t get set to the current PC time, they get set to the time and date value you provide on the command line.
The only difference between them is the format you supply the time and date on the command line.
The -d (date) option accepts a string formatted in a free format, human-readable style, like ‘Wed, 7 August 2024 16:00:00’ or “2024-7-11 16:00:00”, or even phrases like “next Sunday.”
The -t (stamp) option requires a different, less friendly format. The format is:
[CC]YY]MMDDhhmm[.ss]
That’s century, century, year, year, then month, day, hour, minute, and seconds. Seconds, century, and year figures are optional. A period “.” is used to separate the seconds from the minutes.
If you provide a year, the century figures are optional, and this century is assumed.
Let’s use the -d option first.
touch -d "Wed, August 7 2024 16:00:00" sample-file.txt
stat sample-file.txt
We’ll set the access and modify timestamps to midnight, on Halloween this year.
touch -t 2410310000.00 sample-file.txt
stat sample-file.txt
Set Only One Time to a Specific Value
The -t and -d options work on both timestamps at once. 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. It’s simple, but a little counter-intuitive.
Combining either the -a or -m options with one of the -t or -d options allows you to provide a time on the command line that applies to only the access or modification timestamps.
Here’s an example. We’re going to set the modification timestamp to 1145 on December 21st of this year, 2024, which happens to be the shortest day of the year.
Using the -t format string, we can write this as 12211145.00. We’re 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.
touch -mt 12211145.00 sample-file.txt
stat sample-file.txt
We could do the same thing with the access timestamp, by using -at in the command.
Set the Access and Modification Times to Those of Another File
We can tell touch to take the timestamps from an existing file and replicate them on a target file.
This let’s you set the timestamps of a file or group of files to a existing file that you know has the timestamps you want. It saves you figuring out how to write the timestamps in either the -d or -t formats, and typing the format string on the command line.
We have a file called reference-file.txt. These are its timestamps.
stat reference-file.txt
We’ll apply these timestamps to our target file, and create another file called sample-2.txt at the same time. Our new file will have the timestamps from reference-file.txt applies to it too, instead of its actual creation time.
touch -r reference-file.txt sample-file.txt sample-2.txt
stat sample-file.txt sample-2.txt
We can see that touch applies the access and modification timestamps from the reference file to our existing and new files.
Don’t Create a File, Only Modify Existing Files
Sometimes, you don’t want touch to create a file if it doesn’t exist. You can override its default action by including the -c (no create) option.
ls missing-in-action.txt
touch -c -mt 202409170900.00 sample-file.txt missing-in-action.txt
stat sample-file.txt
ls missing-in-action.txt
We verify that the missing-in-action.txt file doesn’t exist, then ask touch to update the modification timestamps of sample-file.txt and missing-in-action.txt, but only if they exist.
We can see that the modification timestamp of sample-file.txt has been changed, but the missing-in-action.txt hasn’t been created.
A touch of Class
You can create files using other techniques, such as redirection or cat, but those methods don’t let you easily create multiple files like touch does. And only touch can set the file timestamps to whatever you want.
It’s that level of finesse that sets touch apart.