How to Use the touch Command on Linux

In this guide, we will be showing you how to use the touch command on your Linux system.

touch command on Linux

The Linux file system stores a wealth of metadata about every file and directory. This metadata includes data such as when it was last accessed or modified.

Using the touch command on Linux, we can perform various actions, such as changing the file’s access or modification time.

Additionally, this tool has built-in functionality to create empty files. This ability is unlike the cat command, which is designed to create files with content.

Below we will walk you through some of the numerous ways to use the touch command.

Table of Contents

Syntax of the touch Command

The touch command on Linux has a very straightforward syntax that you can see below.

Using the options field ([OPTIONS]), you can control the behavior of the touch command. For example, you can use touch only to modify the access time or modification time instead of both.

The options field is entirely optional. The command will simply create an empty file when used without any options.

To use touch, you will need to, at the very least, specify a filename. Of course, you can specify as many files as you want, and they will all be modified by the touch command.

touch [OPTIONS]... FILENAME...

Using the touch Command

The simplest way to use the touch command is to specify the files you want to touch.

This showcases some of the interesting behaviors of the touch command. If the file already exists, it will update both the access and modification times to the current time.

If the file doesn’t exist, the touch command will create a new empty file. Using the command in this way is useful when you need a file to exist but don’t necessarily need to add data yet.

The syntax is as simple as using the “touch” command followed by the file’s name we want to “touch”.

touch FILENAME

Example of Using the touch Command on an Existing File

For this example, we will be updating the access and modification time of an existing file called “examplefile“. Before running this command, you can use “stat” to see your file’s current timestamps.

touch examplefile

Using the stat command, you can verify that the file’s times have been changed.

Example of Creating an Empty File Using touch

For example, let us say that we wanted to create a file called “pimylifeup“, all we would be required to do is use the following command.

You can see that we used the “touch” command followed by our filename, “pimylifeup“.

touch pimylifeup

Using the touch Command on Multiple Files

The touch command can touch multiple files. All you need to do is specify multiple file names.

The syntax for this is straightforward. While we only mention two filenames, you can specify as many as you want.

touch FILENAME1 FILENAME2

Example of Using touch on Multiple Files

With this example, we will touch three empty files named “pimylifeup“, “touchcommand” and “tutorial“.

As you can see with the command below, this is as straightforward as specifying the three filenames after the “touch” command.

If any of these files don’t exist when the touch command runs, it will create them.

touch pimylifeup touchcommand tutorial

Telling touch Not to Create an Empty File

By default, the touch command will always create a file if it doesn’t exist. However, it does allow you to disable this behavior by utilizing the “-c” option.

The touch command will only modify an existing file when using the “-c” option. If that file doesn’t exist, then it won’t do anything.

touch -c FILENAME...

This option is useful when you only need to change a file’s access or modification time if it currently exists.

Example of touch Not Creating an Empty File

To show you how this works, let us use touch again with a filename we have not used previously.

For this example, we will be using the touch command with the “-c” option, alongside the filename “uplifemypi“.

touch -c uplifemypi

Using the ls command after running the previous command, you will see that our file wasn’t created.

Modifying the Access Time using the touch Command

One of the core features of the touch command is its ability to modify the access time. To achieve this, we can utilize the “-a” option.

When used by itself, this option will set the access time of the specified files to the current time. If you want to set a specific date, you must use the “-d” or “-t” options.

The syntax for setting the access time to the current time is the following.

touch -a FILENAME...

We will also use the touch command to set the access time to a specific date in our examples below.

Example of Changing the Access Time to the Current Time

Below, we will give you an example of how you can change the access time of a file.

Using just the “-a” option alongside the touch command will set the access time to the current time.

In our case, we will be modifying the access time of the file called “pimylifeup” to the current time.

touch -a pimylifeup

Example of Modifying the Access Time to a Specific Time

To set a specific timestamp for a files access time, we will need to use the “-t” option alongside the “-a” option.

The “-t” option allows us to specify a specific timestamp to set for our access time. This timestamp must follow the format of “[[CC]YY]MMDDHHMM[.SS]“.

touch -a -t [[CC]YY]MMDDHHMM[.SS] FILENAME

For our example, we will be using the touch command to modify the access time of our file called “pimylifeup“. The time that we want to assign to the file is “202201010222.32

touch -a -t 202201010222.32 pimylifeup

If you want to learn more about these timestamps, follow our “Setting a Specific Timestamp Using the touch Command” section.

Changing the Modification Time of a File using touch

Another ability of the touch command is to change the modification time on a file.

Changing the modification time only requires using the “-m” option. touch will set the modification time to the current time when used without the “-t” or “-d” options.

The syntax for using this functionality is as straightforward as what we have shown below

touch -m FILENAME

Example of Changing the Modification Time to the Current Time

Let us change the modification time for our example file called “pimylifeup” to show you how this works.

Changing the modification time is as simple as using “touch” followed by the “-m” option, then your filename.

touch -m pimylifeup

After running this command, your file will now have the same modification time as the current date.

Example of Setting the Modification Time to a Certain Time

By combining the timestamp option (-t) alongside the modify time option (-m) we can set a specific date and time for the file.

When the touch command is used with the “-t” option, you need to include a timestamp using the format “[[CC]YY]MMDDHHMM[.SS]“.

touch -m -t [[CC]YY]MMDDHHMM[.SS] FILENAME

For this example, we will be setting the modification time of our filled called “pimylifeup” to “202103221213.54

touch -m -t 202103221213.54 pimylifeup

If you would like an explanation of the timestamp format, skip down to our section dedicated to it.

Copying the Timestamp from Another File using touch

The touch command also can copy a timestamp from an existing file. This ability is useful when you need a files modification or access times to match.

All we need to do is use the reference option, “-r” (--reference) followed by the filename we want to copy the timestamp from.

touch -r=SOURCEFILENAME FILENAME

You can safely use this option alongside other options if you only want to copy the modification or access time.

Example of Copying the Timestamp From a Reference File

For this example, we will copy the timestamp data from a file called “upmylifeup” to one called “pimylifeup“.

All we need to do is use the “-r=” option, specifying the name of the file we want to copy from, then the name we want to copy them to.

touch -r=upmylifeup pimylifeup

If you use the stat command to check these two files, your timestamps now match up.

Setting a Specific Timestamp Using the touch Command

The touch command allows you to assign a file a specific timestamp. You need to utilize the “-t” option to achieve this.

When using this option, you need to specify a timestamp using the format of “[[CC]YY]MMDDHHMM[.SS]“.

Below you can see the syntax for using the touch command to set a specific timestamp for a specific file.

touch -t [[CC]YY]MMDDHHMM[.SS] FILENAME

Below we will quickly break down the timestamp format so that you can get a better understanding.

  • [[CC]YY] – The first four digits represent the year. You can either use the full year (four-digit) or shorthand (two-digit).

    For example, you could use “2022“, or “22” for this part of the timestamp.

    This value is entirely optional. If not specified, the touch command will use the current year.
  • MM – The next two digits represents the month. This value is always expressed with two digits.

    For example, January would be written as “01” when used within this value.
  • DD – Afterwards, you need to specify the day of the month. Like the month, this value is expressed using two digits.

    For example, you would write the ninth day as “09“.
  • HH – You also are required to specify the hour for the timestamp. The hour is expressed in 24 hour time.

    Like most timestamp values, this is also expressed as two digits.

    For example, 8 PM would be written as “20“.
  • MM – The last value that is required is the minute. Like the rest of the values, this is also expressed using two digits.

    For example, if you wanted to set the value to 9 minutes, you would use the following value “09“.
  • [.SS] – The timestamp also allows you to specify the seconds. However, this value is entirely optional.

    This timestamp part is slightly different as the value must start with a dot (.).

    For example, for 30 seconds past, you would use the value “.30“.

Putting all of our example values together, you will end up with the value “202201092009.30“.

Example of Setting a Specific Timestamp

For this example, we will be changing the modification and access time for our file called “pimylifeup“. We will be setting these values to “202201092009.30“.

All we need to do is use “touch” followed by the “-t” option, the timestamp, and finally, our filename.

touch -t 202201092009.30 pimylifeup

Conclusion

Hopefully, you will now have an idea of how you can use the touch command on your Linux system.

The touch command has a variety of use cases. However, its main purpose is to modify the files’ access or modification times.

It also allows you to create empty files easily. This functionality can be helpful when you need a file to exist on your system but don’t need it to contain data.

If you have any questions about using the touch command, feel free to comment below.

Be sure to check out our other command guides or Linux tutorials. These guides will help you improve your knowledge of Linux.

Leave a Reply

Your email address will not be published. Required fields are marked *