The touch command in Linux (UNIX) refers to a standard operating system used to change, create and modify the timestamps on a file. Its primary function is modifying a timestamp. Although commonly used for file creation, this isn't its primary function. You can use the touch command to change any modification and access the timestamp for any file. The only time it creates a file is if the said file doesn't already exist.

There are mainly two commands used for creating a file in the Linux OS. They are:

  • Cat command: For creating a file with content
  • Touch command: For creating a file without content. The touch command in UNIX creates empty files. It can be utilized when the user doesn't have any data to store during its creation.

Changing File Information in Linux

 Managing time stamps is much easier compared to how it was before the advent of the touch command. If you intend to create a new file on UNIX, you can use the cat command, touch command, etc. The only way to best utilize these options is if you already have experience working with any Linux-based OS in the past.

If you are uncomfortable using a command line, you can still use various file applications to successfully create new files on Linux. That means you have different options.

Linux Files Timestamps 

Before exploring the different examples of applications of the touch command, it is necessary to understand the available time stamps on Linux.

Every Linux file possesses three timestamps:

  • atime – Also known as access file, it is the last time a specific file was opened or accessed by an application or command, for instance, cat or vim.
  • mtime – Modify time refers to the last time a file's content was modified.
  • ctime – Ctime is change time in full and refers to the last time content, or attribute was altered in a file. The attribute includes file ownership, file location, or file permissions.

Touch command options

-a: Only alters the access time. 

-c: Don't create a file if it doesn't already exist.

-d: To update the modification and access times.

-m: Only alter the modification time.

-r: Utilize the file's modification and access times

-t: Uses specified time to create a file

How to utilize the touch command

At the basic level, the touch command's purpose is to create entirely new empty files. It does so without a prompt to fill the file, unlike the cat command, which instructs the user to add content to a file when creating it.

This feature of the touch command benefits software developers who are constantly creating new files either to validate the existence of a particular file or writing code.

Examples of touch commands

1.   Using the touch command to create a zero byte file

If you want to create a new, empty (zero-byte) file called Gina, follow the following command.

# touch Gina

2.   Using touch to create several files

It is possible to create multiple files using the touch command. For instance, the command below will help you create three files Gina, Lina, and Dina.

# touch Gina Lina Dina

3.   Changing your file's modification and access times

Apply the -a option as demonstrated below when using touch to update or change your file's last modification and access times; apply the -a option as demonstrated below. It allows you to set the current date and time on a specific file. If the Gina file doesn't already exist, the command will create a new file called Gina.

# touch -a Gina

One of the most used Linux commands, for instance, is the Is command and find command, which uses timestamps to list and locate files.

4.   How to avoid creating a new file using touch

To avoid creating new files on Linux, you should use the c-option together with the touch command. When you use the command below, your system won't create a file Gina if it isn't already existing.

# touch -c Gina

5.   Changing the file modification timeframe

If you only intend to alter the modification time, Gina, use the touch command together with -m. This example will only update the previous modification time and not the file's access time, which is essential to note.

# touch -m Gina

6.   Explicitly setting the modification and access times using the touch command

With touch, programmers can set times explicitly using the -t and -c options. The format will appear as shown here.

# touch -c -t YYDDHHMM Gina

For instance, the outlined command will set the modification and access time and date to the file Gina as 15:30 (3:30 p.m.) December 11 of the year 2022.

# touch -c -t 22111530 Gina

For the next step, verify the modification and access time of the file Gina with the Is-I command.

Lastly, substantiate file Gina's modification and access time with the ls -l command.

# ls -l

total 2

-rw-r--r--. 1 root root 0 Dec 11 15:30 Gina

7.   Using another file's timestamp

To update the timestamp of the file Lina with the timestamp of the Gina file, use the touch command with the r-option. Both files will subsequently bear a similar time.

# touch -r Gina Lina

8.   Creating a file with an enumerated time

The format will be as follows to create a file with a specified time that is not the present time.

# touch -t YYMMDDHHMM.SS tecmint

For instance, using the touch command with the t-option will give the tecmint file the timestamp at 17:30:45 p.m. on December 11, 2022.

The touch command with the -t option will give the tecmint file a time of 18:30:55 p.m. on December 10, 2012.

# touch -t 202212111730.45 tecmint

Take away

The touch command in Linux is an integral terminal program when working with files in the Linux OS. The examples provided above demonstrate some of the general uses of this command. Options covered in this article deal with almost all of the commands available. If there are some missing, you can still find information online.

When you buy linux server, it is important to consider factors such as the level of technical support provided, the security measures in place, and the cost of the hosting plan. we offer you best services.

People also read: