CentOS Debian Mint openSUSE Red Hat Ubuntu

Linux Touch Command Examples

Linux Touch Command Examples

The touch command is a command-line utility used to create new and empty files. Aside from that, the command can be used in updating certain file properties such as access times and timestamps. In this article, we will go through practical examples of the Linux touch command

Basic syntax

The touch command takes the following syntax.

$ touch [OPTION] [FILE]

With that in mind, now let’s check out a few touch command example usages.

Creating an empty file

In its simplest form without any command options, the touch command is used to create an empty file.

Syntax:

$ touch [filename]

In the example below, we have created a file named file1.txt. Using the ls command we can see that the file has been created.

$ touch file1.txt

Creating Multiple files

Additionally, you can create multiple files at a go by specifying the file names in one command as follows

$ touch [filename1] [filename2] [filename3] [filename4]

For example, the common below creates four new files.

$ touch file1.txt file2.txt file3.txt file4.txt

We have created files named file1, file2, file3, and file4.

Changing file access time

If you would like to update the last access time of a file, use the touch -a command.

Syntax:

$ touch -a [filename]

Here, we can check the access time of file2 before and after running the touch -a command. You will notice the access time has been modified.

We are using the stat command to check the status of our directory.

Avoid creating new files

The touch command with the c option can be used to check whether a certain file exists. If that file does not exist, touch will not create it. You will have avoided creating a new file.

Syntax:

$ touch -c [filename]

Referring to the image above, the file named sample was not created.

Changing access and modification time

To update both the access and modification time use the touch command below.

Syntax:

$ touch -c -t YYMMDDHHMM fileName

For example:

$ touch -c -t 202106121830 file4.txt

Just to be cocksure, confirm the changes using the stat command as follows.

From the output, the access and modification time for file4.txt have both been changed to 2021-06-12 18.30:30.

Changing the modification time of a file

If you want to change only the modification time of a file, use the touch -m command.

Syntax:

$ touch -m [filename]

From the image above, we can check the modification time of file3 before and after running the touch command. The time has been updated.

Use the timestamps of another file

The touch command with the -r option is used to apply the timestamp of one file to that of another file.

Syntax:

$ touch -r second_file_name first_file_name

You can confirm the modification times using the stat command as follows.

Here, the timestamps of file2 match that of file1.

Creating a file using a specified time

The touch -t command is used to specify time when creating a file. The syntax for the command is:

$ touch -t YYMMDDHHMM fileName

For example,

$ touch -t 202106162228.30 file.txt

File.txt has been created with the specified time of 2021-06-16 22.28:30.

Conclusion

We have covered basic examples of the touch command. For more information use ‘man touch’ to view the manual page.

Similar Posts