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. Here’s 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. 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.

Using the touch command to create a new file from the Linux command line.

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.

Checking the newly created file actually exists, with ls on the Linux command line.

Creating Empty Files

Creating files with touch is child’s play.

The files are created in their requested locations. They’re little more than a filename in the file system at this point,

Our file definitely exists, even though it has a file size of zero bytes.

Using touch on the Linux command line to create multiple files at once.

To create multiple files, provide a list of their names on the command line.

If the files you’re going to create will have sequentially numbered names, you can create them all in one go like this.

Using the topuch command to create sequentially numbered files on the Linux command line.

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 thestat commandto see what its timestamps are.

Its access timestamp is 14:32:47 on Jun 08, 2025, and its modification timestamp is midnight on May 25, 2025.

Using the stat command to display the access and moficiation timestamps of a file, on the Linux command line.

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 May 31, 2025.

Using the touch command to set a file’s access time to the current PC time, on the Linux command line.

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.

Our modiffication timestamp is now showing 10:42:50 on Jun 10, 2025.

Using the touch command to set a file’s modification time to the current PC time, on the Linux command line.

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.

Using the touch -d option to set the access and modifcation times to a user specified time.

The -d (date) option accepts a string formatted in a free format, human-readable style, like ‘Wed, 08 June 2025 16:00:00’ or “2025-06-05 16:00:00”, or even phrases like “next Sunday.”

The -t (stamp) option requires a different, less friendly format. The format is:

Using the touch -t option to set the access and modifcation times to a user specified time.

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.

Using the touch -mt options to set the modifcation time to a user specified time.

Let’s use the -d option first.

We’ll set the access and modify timestamps to midnight, on Halloween this year.

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.

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.

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.

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.

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 may 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.