Touch Command in Ubuntu

The touch command is used for modifying the timestamps of a file. However, it is popularly used for creating new files in Linux.

The touch command is primarily used to modify the timestamp of a file. If the file does not exist, then the default behavior of the touch command is to create the file.

This article covers many aspects of the touch command in Ubuntu.

Creating files using touch command

The touch command is used to change the timestamp of a given file. But what if the specified file does not exist? Well, then a new, empty file is created.

Creating a single file

Creating a single file using the touch command is one of the most popular use case of the touch command.

touch FILE

Let us take a look at file creation by creating a file called 'learn-ubuntu' using the touch command.

$ ls -A

$ touch learn-ubuntu

$ ls -A
learn-ubuntu

As you can see, previously, I did not have any file called 'learn-ubuntu'. Then, I used the touch command with the file name 'learn-ubuntu'.

Since no file with the name 'learn-ubuntu' existed, the touch command created an empty file with this name.

Creating multiple files

You are not limited to creating only one file with the help of the touch command.

Any number of arguments you pass (separated by spaces) is considered a file name (except when it is not an inbuilt option).

The syntax to create multiple files is as follows:

touch FILE1 FILE2 FILE3

Let me create four files named 'test-file-' suffixed by their number.

$ ls -A

$ touch test-file-1 test-file-2 test-file-3 test-file-4

$ ls -A
test-file-1  test-file-2  test-file-3  test-file-4

Don't create new file with touch

To prevent a file from being created, use the --no-create option (or -c for short). This option tells the touch command to not create a file if it does not exist.

touch --no-create FILE

I will use the touch command to modify the timestamps of a file named 'will-not-be-created'. But if the file does not exist, I do not want an empty file.

$ ls -A

$ touch --no-create will-not-be-created

$ ls -A

As you can see here, the file 'will-not-be-created' does not exist. Using the --no-create option, I told the touch command to not create a file if it doesn't already exist.

Changing timestamps of a file

There are three timestamps of a file in Linux:

  • Creation time: "When was this file created?"
  • Access time: "When was this file last opened?"
  • Modification time: "When was this file last edited?"

The touch command is used to modify the 'Access time' (known as atime) and the 'Modification time' (known as mtime). The 'Creation time' timestamp is not modifiable.

You can use the stat command to see the timestamp values of a file.

Change only the access time

When the touch command is used without specifying which timestamp should be changed, in that scenario, both, the Access time and Modification time are changed.

If you only want to change the Access time, use the -a option. The syntax is as given below:

touch -a FILE

Since a specific time is not specified, the current time will be used to replace the atime value.

Let us take a look at this with an example. I have a file called 'old-file'. It was last accessed on 1st March 2002. I want to change that to the current time.

$ stat -c '%x' old-file
2002-03-01 23:30:00.000000000 +0530

$ date
Fri Jul 29 05:45:52 PM IST 2022

$ touch -a old-file

$ stat -c '%x' old-file
2022-07-29 17:45:52.782877829 +0530

As you can see, the atime value changed from 1st March 2002, to 29th July 2022. For the sake of demonstration, I also included the output of date command.

Change only the modification time

To only change the Modification time, use the -m option. Below is the syntax:

touch -m FILE

Currently, a specific time is not being specified, so the current time will be used as the mtime value.

Let me demonstrate this with an example. I have a file that is called 'old-file'. It was last modified on 1st March 2002. I want to change that to the current time.

$ stat -c '%y' old-file
2002-03-01 23:30:00.000000000 +0530

$ date
Fri Jul 29 06:01:29 PM IST 2022

$ touch -a old-file

$ stat -c '%y' old-file
2022-07-29 18:01:29.808703112 +0530

Use specific time (instead of current time)

When no specific time is specified, it is assumed to be the current time.

But what if you want to set it to a very specific time? In that case, use the -t option along with a specific format.

touch -t [[CC]YY]MMDDhhmm[.ss] FILE

To break down this syntax, anything that is between the square brackets is optional. Below is what each cipher-like character stands for:

  • CC - Century (use '20' for '2022') (this field is optional)
  • YY - Year of the century (use '22' for '2022') (this field is optional)
  • MM - Month of the year (use 01 to 12)
  • DD - Day of month (from 01 to 31)
  • hh - Hour of the day (from 01 to 24)
  • mm - Minute of said hour
  • ss - Second of the minute (this field is optional)

So, let's say I want to manually set the modification time of 'old-file' to be 2022-10-24 (24th October 2022) at 10:30 hours, I will use the 202210241030 string to specify date and time.

I will also set the access time to 2022-09-26 (26th September 2022) at 14:28 hours. For this time, I will use the 202209261428 string to specify my custom date and time.

$ stat -c '%y
%x' old-file

2002-03-01 23:30:00.000000000 +0530
2002-03-01 23:30:00.000000000 +0530

$ touch -m -t 202210231030 old-file

$ touch -a -t 202209261428 old-file

$ stat -c '%y
%x' old-file

2022-10-23 10:30:00.000000000 +0530
2022-09-26 14:28:00.000000000 +0530

Previously, the access time and modification time of 'old-file' was set to 2022-03-01 at 23:30 hours. After manually setting the access time and modification time, you can see that both timestamps reflect the intended changes.

Conclusion

Though the primary purpose of the touch command is to modify the timestamps, it is widely used for creating a new empty file. Strange, I know.