Beginners Guide for Touch Command in Linux

Linux TLDR
Last Updated:
Reading time: 4 minutes

The Linux user often uses the touch command as a way to create a text file in the current working directory, but this tool is not limited to that; it’s way bigger than creating a text file.

The official description for touch command is β€œa tool that can change a file’s access and modification times (atime, mtime),” and that is what it does and is built for.

In this article, you will learn how to use the touch command for changing file access time, file modification time, replicating another file timestamp, and much more.

Tutorial Details

DescriptionTouch
Difficulty LevelLow
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisitestouch
Internet RequiredNo

Syntax of the Touch Command

The touch command takes two arguments: one is the option, and the other is the file name.

$ touch [OPTION] [FILE]

The following are known options for this command:

OptionsDescription
-aChange only the access time.
-cDo not create a file if one does not exist.
-dChange a timestamp using the date string.
-fdeprecated (it used to force changes in older versions of BSD).
-hChange the timestamp to a symbolic link instead of a referenced file.
-mChange only the modification time.
-rReplicate the existing file timestamp to the referenced file.
-tSpecify a custom date and time for the referenced file.

Key Note of Touch Command

While working with this command, you must be aware of the following things:

  • Access time or atime changes when a command reads the file content, like grep and cat do. To view the access time changes, use the ls command with the β€œ-lu” flag.
  • Change time or ctime changes when a modification in file property happens, like renaming the file, changing the permissions, or moving the file. Use the β€œls -lc” command to view the ctime of the file.
  • Modification time or mtime is changed while modifying the content of the file; to view the mtime, use the β€œls -l” command.

Keeping that in mind, let’s move to the examples.

Creating a New File

The touch command without any options will create an empty file with the specified name.

$ touch data.txt

Output:

Creating a single file using the touch command

If the specified file is already present, the touch command will only update the timestamp, keeping the permissions and file content unchanged.

Testing an already existing file

Creating Multiple Files

Specify multiple filenames, using a space as a separator, to create them in your working directory.

$ touch data1.txt data2.txt data3.txt

Output:

Creating multiple files using the touch command

Creating a Range of Files

Instead of specifying multiple filenames, you can specify the range of files using an integer as order inside the β€œ{}” curly bracket with β€œ..” two dots specifying the start and end of the range, as shown.

$ touch data{1..5}.txt

The above command will create five files in β€œdata1.txtβ€œ, β€œdata2.txtβ€œ, .., β€œdata5.txt” order.

Creating a series of files using the touch command

There are a few more patterns that you can follow to create a sequence of files.

$ touch data{5..10}.txt                                                                      #"data5.txt", "data6.txt", .., "data10.txt"
$ touch data{20..15}.txt                                                                    #"data20.txt", "data19.txt", .., "data15.txt"
$ touch data{a..j}.txt                                                                          #"dataa.txt", "datab.txt", .., "dataj.txt"
$ touch data{j..z}.txt                                                                          #"dataj.txt", "datak.txt", .., "dataz.txt"
$ touch data_{1..10}.txt                                                                      #"data_1.txt", "data_2.txt", .., "data_10.txt"
$ touch data_{a..j}.txt                                                                         #"data_a.txt", "data_b.txt", .., "data_j.txt"

Note that you cannot combine the numbers and letters to create a sequence of files, and special characters like β€œ$β€œ, β€œ/β€œ, β€œ\β€œ, etc., are not allowed to be used as a sequence of files.

How to Avoid Creating a New File (if it does not exist)

The β€œ-c” flag can be used with other flags (that modify the timestamp of a target file) to modify the changes only for an existing file; if it does not exist, do not create a new file.

$ touch -c data.txt

Output:

Do not create the file if it does not exist

Changing the File Timestamp Using a Date String

The β€œ-d” flag will make it easier for you to change the timestamp of a referenced file using the date string.

$ touch -d "-1 hour" data.txt

The above command sets the time on a referenced file to one hour in the past.

Changing the referenced file timestamp using the date string

The following are a few examples (accepted in human-readable format) that you can use as a date string.

  • Set the timestamp to 1 hour in the past using β€œ-1 hourβ€œ.
  • Calendar dates, such as 20 November 2022.
  • Time of day, such as 5:20am or 10:30pm.
  • Days of the week, such as Sunday, Monday, etc.
  • Relative times, such as 6 years ago, yesterday, next monday, etc.

Changing the File Access and Modification Time

The β€œ-a” flag will modify the last access and modification times of a referenced file with the current date and time; if the file does not exist, it will create a new one (use the β€œ-c” flag to avoid this).

$ touch -a data.txt

Output:

Modifying the file access and modification time

Explicitly Changing the Access and Modification Time

Unlike the previous command, use the β€œ-ct” flag to explicitly set your desired time instead of the current for the referenced file.

For example, the following command will set the access and modification times for the β€œdata.txt” file as β€œ16:30 pm” on November 20 of the current year (2022).

$ touch -at 11201630  data.txt

Output:

Explicitly setting the file access and modification times

The following is a breakdown of the (11201630) timestamp format used in the above command:

  • 11: Referring to the month in its numeric value (11 = November).
  • 20: It refers to day 20.
  • 16: It will be used as an hour in a 24-hour format.
  • 30: It will set the minute.

Changing the File Modification Time

The β€œ-m” flag will only change the modification time (not access time) of the referenced file.

$ touch -m data.txt

Output:

Modifying only the modification time

Explicitly Changing the File Modification Time

Use the β€œ-mt” flag to explicitly set the custom modification time for the referenced file instead of the current date and time.

For example, the following command will set the modification time for the β€œdata.txt” file as β€œ16:30 pm” on November 20 of the current year (2022).

$ touch -mt 11201630 data.txt

Output:

Explicitly setting the modification time

The following is a breakdown of the (11201630) timestamp format used in the above command:

  • 11: refers to the month in its numeric value (11 = November).
  • 20: refers to day 20.
  • 16: It will be used as an hour in a 24-hour format.
  • 30: It will set the minute.

Replicating the Existing File Timestamp to the Referenced File

The already existing file timestamp can be copied to the referenced file using the β€œ-r” flag.

$ touch -r olddata.txt data.txt

The above command will set the β€œolddata.txt” file timestamp to the β€œdata.txt” file.

Replicating the existing file timestamp on the referenced file

Updating the Symbolic Link Timestamp

The β€œ-h” flag will change the timestamp of the symbolic link without affecting the original file.

$ touch -h symbolic_data.txt

Output:

Modifying the symbolic link timestamp

Here comes the end of this article.

I hope this article will help you understand how useful this command is.

If you have any question or query regarding this topic, feel free to ask it in the comment section.

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.