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.

Terminal window showing the manual page for the Mutt email client on Linux

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.

The Linux mascot touching the screen with the Linux terminal in the background.

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.

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

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.

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

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.

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

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.

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

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.

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

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.

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

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.

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

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?

Using the touch -d option to set the access and modifcation times to a user specified 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.

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

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.

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

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.

Using the stat fil;command to display the timestamps of a reference text file.

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.

Using the touch command to copy the timestamps from a reference file to an existing file, and a newly created file, from the Linux command line.

And only touch can set the file timestamps to whatever you want.

It’s that level of finesse that sets touch apart.

Using the -c option to supress the touch command’s default action of creating missing files, from the Linux command line.