Linux Touch Command: What It Is + Examples of How to Use It to Modify Timestamps

People usually associate the touch command in Linux with creating files. However, like many other Linux commands, it offers much broader functionality.

For example, Linux VPS users can use the touch command to change the timestamps of files or folders.

In this tutorial, we’ll go over the Linux touch command and talk about its options and use cases.

Linux Touch Command Options

Linux Touch Command Syntax

Important! Before proceeding, make sure to connect to your VPS via an SSH client such as Putty.

The syntax of the touch command is:

touch [options] [file_name]

Check out the table below for all the touch command options, also known as flags:

Flag Use Case
-aChange the access time
-mChange the modification time
-cPrevent creating a new file
-hChange the symbolic link timestamp
-hChange the timestamp for symbolic links
-t <stamp>Modify the timestamp. In this case, <stamp> follows the date-time format
-d=<string>Change the timestamp based on the date string
-r=<file>Change the timestamp based on the reference file
-v or –versionDisplay the touch command version
–helpDisplay the help menu

File Timestamps

In Linux, every file and folder has a timestamp that shows when a file’s content or attributes were modified. There are three types of timestamps:

  • Access time (atime) – last time a file was read.
  • Modification time (mtime) – last time a file’s content was modified. Like access time, it is also part of the file status metadata.
  • Changed time (ctime) – last time a file’s metadata was changed. For example, permissions.

As a result, the Linux touch command is mainly used to manipulate file or folder access and modification time.

Keep in mind that there is no way to set or change ctime manually. Since atime and mtime are part of a file’s status metadata, changing atime or mtime of the file results in ctime, which is automatically set to the current time.

Linux touch Command Examples

Here are a few useful examples of the touch command on a Linux system.

Using touch to Create a File

If you use the touch command without any options, it will simply create a new empty file. If the file already exists, the touch command will update the access and modification times to the current time without changing the file contents.

touch file_name.txt
The terminal window showcases the touch command to create a new file. Ls -l command shows the file creation time stamp

Using touch to Create Multiple Files

It is also possible to create multiple files using a single touch command. To do that, specify the names of the files with spaces between them. It would look like this in the command line:

touch file_name1.txt file_name2.txt file_name3.txt
The terminal window shows the touch command to create empty files. Touch creates three empty files as a result

You can auto-generate file names using curl braces while creating multiple files, like in the following example:

touch file_name{1..3}.txt

The above touch command will create three files named file_name1.txt, file_name2.txt, and file_name3.txt.

Using touch to Change Access Time

To change the access time of a file to the current time, use the a option followed by the file name with the touch command like in the following example:

touch -a file_name.txt

Then, check the access time with the following command:

ls -lu file_name.txt
The terminal window shows the touch command to change only file access time for a single file

Using touch to Change Modification Time

The m option, along with the touch command, changes the modification time of a file to the current time:

touch -m file_name.txt
The terminal window shows the touch command to change only modification time of a given file. While ls -l command displays modify time

Using touch to Change Access and Modification Time

To change both access time and modification time with a single command, use the options a and m together:

touch -am file_name.txt

Now, check the date with both of these commands:

ls -l file_name.txt
ls -lu file_name.txt
The terminal window shows the touch command to change modification and access time for a given file. While ls -l and ls -l displays the modification date along with the last access time

Using touch to Change Access Time Without Creating a New File

In some situations, you want to change an existing file’s access and modification time to the current time without actually creating a new one. To do that, use the c option followed by the file name with the touch command.

touch -c new_file.txt

Using touch to Set Specific Access and Modification Time

It is also possible to set a file’s access and modification time to a particular date by using the t option followed by a date-time. It would look like this:

touch -t 202203081047.30 file_name.txt

Make sure to check whether the date changed with the following command:

ls -lu file_name.txt
The terminal window shows the touch command to set a specific timestamp for modification and access times for a given file. While ls -lu displays the specified time

Remember that the date-time format must follow the CCYYMMDDhhmm.ss style:

  • CC – the first two digits of the year
  • YY – the second two digits of the year
  • MM – the month of the year (01-12)
  • DD – the day of the month (01-31)
  • hh – the hour of the day (00-23)
  • mm – the minute of the hour (00-59)
  • ss – the second of the minute (00-59)

When you use a symbolically linked file name with the Linux touch command, the timestamp information for the original file, that was pointed by the link file, gets modified. To change the access and modification time to the current time for a symbolically linked file, use the h option:

touch -h SymbolicLinkFile

Using touch to Set Timestamp Using a Reference File

The Linux touch command can also set a file’s access and modification time by reading the timestamp information from another file. For example, the following touch command with the r option will scan the timestamp information from reference.txt and set these timestamp values to file_name.txt. Here’s an example of the command:

touch -r reference.txt file_name.txt
The terminal window shows the touch command to modify the timestamp format using a referenced file. While ls -l command displays the modification timestamps for the existing files

Using touch to Specify the Date and Time as a String

You can also specify the date and time as a string by using the d option. The following Linux touch command example sets the date to the 8th of March, and the time is automatically set to 00:00.

touch -d '8 Mar' file_name.txt

Instead of specifying the date as a string, you can select the time as one. In that case, the date will be set to the current date automatically:

touch -d '20:10' file_name.txt

Conclusion

Manipulating access, modification, and changing times for files and folders can be helpful for any Linux user. That is where the touch command comes in.

In this tutorial, we’ve covered the usage of the Linux touch command and included the most commonly used options for it. We’ve also provided a few use cases for this command that you can try on your system.

If you have any questions, let us know in the comments section below.

Author
The author

Edward S.

Edward is a content editor with years of experience in IT writing, marketing, and Linux system administration. His goal is to encourage readers to establish an impactful online presence. He also really loves dogs, guitars, and everything related to space.

Author
The Co-author

Ignas R.

Ignas takes great satisfaction in helping people tackle even the most complex technical issues. His current goal is to write easy-to-follow articles so that these issues will not happen at all. During his free time, Ignas likes to play video games and fix up things around his house.