If you experience any difficulty in accessing content on our website, please contact us at 1-866-333-8917 or email us at support@chicagovps.net and we will make every effort to assist you.

By
August 15, 2024

Mastering the touch Command: A Beginner’s Guide to Creating Files and Managing Timestamps on Linux

 

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.

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

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.

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.

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.

The access timestamp has been updated to 10:42:00 on Aug. 6, 2024.

It is important to note that the change timestamp has also been adjusted accordingly. This timestamp indicates the last moment the file was altered in any way. Even minor actions such as modifying file permissions trigger an update to this timestamp. Consequently, when the access timestamp is altered, the change timestamp is also updated.

To adjust the modification time, we utilize the -m (modify) option.

The modification timestamp now reflects 10:42:50 on Aug. 6, 2024.

There are two methods to manually adjust both the access and modification timestamps to a specified time, rather than using the current time on your computer. These methods differ only in the format of the time and date input you use on the command line.

Using the -d (date) option, you can input the time and date in a more intuitive, freeform style. Examples include ‘Wed, 7 August 2024 16:00:00’, ‘2024-7-11 16:00:00’, or more general terms like ‘next Sunday’.

Alternatively, the -t (stamp) option demands a stricter, less intuitive format for the date and time input, differing distinctly from the -d option.

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.

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

The options -t and -d modify both timestamps simultaneously. In contrast, the -a and -m options update only a single timestamp to the current time, not allowing for user-specific time adjustments.

However, what if you need to set a single timestamp to a specified time? This can also be accomplished, albeit in a slightly non-intuitive way.

By combining the -a or -m options with either the -t or -d options, you can set the desired time for either the access or modification timestamp alone on the command line.

For instance, let’s consider a scenario where we set the modification timestamp to 1145 on December 21st, 2024, which is notably 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.

We can tell touch to take the timestamps from an existing file and replicate them on a target file.

This method allows you to update the timestamps of one or multiple files to match the timestamps of an existing file without manually entering the timestamp data in -d or -t formats. This approach eliminates the need to create a format string for command input.

Consider a file named reference-file.txt with specific timestamps.

The timestamps of this file will be copied to another file called target-file, and a new file named sample-2.txt will also be created with these same timestamps. This effectively assigns the reference file’s timestamps to the new file, overwriting its original creation time.

This demonstrates that using touch transfers the access and modification timestamps from a reference file to both pre-existing and newly created files.

Sometimes, you may prefer not to create a file using the touch command if it does not exist. This can be achieved by using the -c option, which prevents the creation of non-existent files.

We check first to confirm that the file named missing-in-action.txt does not exist. Then, we command touch to update the timestamps of existing files, specifically sample-file.txt and missing-in-action.txt, without creating any new files.

The result shows that the timestamp of sample-file.txt has been updated, whereas the file missing-in-action.txt has not been created.

While you can create files using other methods, such as using redirection or the cat command, these methods do not provide the ease of creating multiple files simultaneously like touch does. Furthermore, touch uniquely allows you to adjust file timestamps as desired.

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


ChicagoVPS is your gateway to unparalleled hosting solutions. Our state-of-the-art datacenters and powerful network ensures lightning-fast speeds and uninterrupted connectivity for your websites and applications. Whether you’re a startup looking for scalable resources or an enterprise in need of enterprise-grade hosting, our range of plans and customizable solutions guarantee a perfect fit. Trust in ChicagoVPS to deliver excellence, combining unmatched reliability and top-tier support.

For Inquiries or to receive a personalized quote, please reach out to us through our contact form here or email us at sales@chicagovps.net.

Subscribe Email

Top