touch Command in Linux


While dealing with file management, Linux has a list of commands such as cp, mv, zip, touch, and so on. Among these, the touch command is used to create the empty files, that most of the users know. However, its primary purpose is to modify the timestamp of the existing files and create new files as well. Timestamp represents the additional information of the file, i.e., metadata.

All these timestamps are dealt with using the touch command. Let’s discuss the touch command in detail.

Outline:

Timestamps and the touch Command

As discussed, timestamps are the metadata of the files usually managed using the touch command. Thus, before starting to practice the touch command, we are here with some basics of the timestamp on Linux.

Types of Timestamps

Three possible timestamps are associated with each file, i..e, Access Timestamp, Modified Timestamp, and Changed Timestamp.

  • Access Timestamp (atime): The last time when the file was accessed (read-only but not modified).
  • Modified Timestamp (mtime): When the content of the file was changed.
  • Changed Timestamp (ctime): When the metadata (permissions or filename type data) was changed.

 

Let’s have a look at the timestamps of a file, i.e., using the stat command with the file name:

stat <file-name>

You can see the Access, Modify, and Change timestamps. Moreover, the Birth timestamp is also listed:

From the output, you might be thinking about how the timestamps are represented. Let’s understand it.

Representation of Timestamps in Linux

The timestamps are actually stored in seconds but represented in the human-readable date-time format. The seconds are calculated since “Jan 01, 1970 midnight” and then converted into the date-time of the timezone set by the user.

The maximum value of the timestamp is 2147483647 (in seconds) which is “Jan 19, 2038”. After this time, the computing will struggle to calculate time (timestamps). This limitation is known as the “year 2038 problem”.

Now, let’s dig into the touch command.

touch Command in Linux

Apparently, the touch command has a very limited yet significant role in the Linux filesystem. While using the touch command, a user can achieve the following:

  • Backup/Auditing: When the touch command updates the timestamp, it helps in auditing the recent changes which is important when you have to perform changes in sensitive files.
  • Synchronization: The timestamps updated using the touch command help in synchronizing the files, i.e., how many files were edited yesterday or which file needs updating.
  • Version Control System: It is essential when you edit a source code file and keep a record of when the file was changed last time.

That’s how crucial touch command is. Now, let’s dig more about the touch command.

Syntax of the touch Command

Like any Linux command, the touch command follows a syntax which is:

touch [options] [file-name]

The file name is the mandatory parameter in the above syntax. However, you can get the list of supported options using the command:

touch --help

You can see the syntax and the list of supported options. Now, let’s demonstrate a few use cases of the touch command.

Example 1: Create New/Empty File(s)

Primarily, the touch command allows you to create a new file (basic usage but is secondary one). Let’s discuss the possible scenarios while creating a new file:

  • Example 1.1: Creating a Single File

To create a single file, use the file name (without any options), as shown below:

touch [File-Name]

The stat command is used to check the timestamps:

The file is created and if you look at the timestamp, all the timestamps are set to the time when the file is created. These timestamps will change when you change the content or modify the name/permissions.

  • Example 1.2: Creating Multiple Files

Use the touch keyword and write the names of the files you want to create:

touch [file-1] [file-2] [file-3]

Here we have created two text files and a shell script:

Example 2: Update the Timestamps of the Files

The touch command updates all three timestamps of the existing files or you can update a specific timestamp (access time or modification time). Let’s elaborate on both scenarios:

  • Example 2.1: Update All the Timestamps

Primarily, a simple touch command will update all three timestamps, as follows:

touch [filename]

Just to verify, we have used the “stat” command before and after updating the timestamps:

The output shows that the timestamps are changed after using the touch command.

  • Example 2.2: Update a Specific Timestamp (Access or Modified)

To update the access or modified timestamp, use the “a” or “m” flags. For instance, the command below will update the “AccessTimestamp” of the file:

touch -a [filename]

  • Example 2.3: Just Update the Timestamp and Do not Create a File

The above commands create the file, if it is not present. To stop the creation of the new file, you can use the “-c” flag. By doing so, this command will not create the new file:

touch -c [filename]

This example has updated the timestamp as per the command time (when the command was executed). However, you can also update the timestamp as per your requirement, by following the upcoming example:

Example 3: Create or Update a File With a Specific (User Defined) Timestamps

You can create a file with a specific timestamp or if the file already exists, the timestamp of that file will be updated. Here’s the syntax:

touch -t [timestamp] [filename]

The timestamp should be in the following format, i.e., “YYYYMMDDHHMM.SS”:

  • YYYY”, “MM”, and “DD” represent the year, month, and day of the month.
  • HH”, “MM”, and “SS” denote the hours, minutes, and seconds on that day.

As an example, the command below updates the timestamp of the “linuxgenie.txt” file to “202404221420.30”, i.e., “April 22, 2024, and at 14:20:30”:

touch -t 202404221420.30 linuxgenie.txt

The output shows the timestamps are updated as per the command.

Example 4: Create a File or Update the Timestamps as Per the Reference File

You can choose one file and can map its timestamps to other files. With this, you can set the same timestamp for multiple files. For this, you need to use the “r” flag as follows:

touch -r [reference file] [target file]

Here we are updating the timestamp of the “linuxgenie.txt” as of “genie.txt”:

These are all the possible use cases of the touch command. For more details and usages, you can get the help from:

touch --help

That’s how effectively you can use the touch command.

Bottom Line

In Linux, the touch command is all about dealing with the timestamps of files. It is commonly used to create files but is very effective for timestamps. You can create a file with the current timestamp, with a specific time, or can update timestamps as well.

You have learned the working and implementation of touch commands in Linux.

Print Friendly, PDF & Email
Categories