BASH Programming Utilities

Touch Command in Ubuntu 20.04

Touch is a popular command in the Linux system that can be used for performing many tasks, rather than just creating an empty file. Using the touch command, you can change the existing file’s timestamp, the time of last modification, and the time of last access. This article will cover how to use the touch command using several examples in Ubuntu 20.04. All the touch commands discussed in this article will be input into the terminal on Ubuntu 20.04.  Common uses of the touch command are given below.

Create a Single Empty File

The easiest way to use the touch command is to create a single empty file in the terminal. Enter the following command in the terminal to quickly create an empty file:

$ touch ‘filename’

Example

$ touch Newfile

In the following example, we will create an empty file through the terminal with the name ‘Newfile.’  The touch command does not notify through a prompt when a new file is created. So, to verify new file creation, list the files using the ‘ls’ command.

Create Multiple Files

The following syntax is used to create multiple files with the touch command:

$ touch filename1 filename2 filename3 ….

Example

Using the touch command, we will create three files with three different names in the example given below:

$ touch newfile1 newfile2 newfile3

After executing the above command, run the ‘ls’ command to check if newly created files are present.

Avoid Creating a File

Using touch command, you can also avoid creating a new file. To prevent file creation, use the touch command with the ‘-c’ option, and the touch command will not create a new file if the file doesn’t exist already.  Here is the syntax below:

$ touch -c  filename

Example

In this example, we will force the system to avoid creating a new file using the touch command, as follows:

$ touch -c testfile

When using the ‘ls’ command, list that file, then the following output is displayed on my system that shows the file does not exist.

Change File Access Time and Modification Time

Using the touch command, you can change the access and modification time of a file. We will do so in the following example.

Example

In this example, we will create a file named ‘testfile’ using the touch command. Next, we will display the file statistics using the ‘stat’ command.

$ touch testfile
$ stat testfile

To change the access time and modified time of the file ‘testfile,’ execute the following command:

$ touch testfile

In the following output, you can see that the access and modification times of the file have been changed.

Change Access Time or Modification Time

You can change the access time only or modification time only using the touch command. To change the access time only, use the following syntax:

$ touch -a filename

Example

In this example, we will create a file named ‘testfile’ using the touch command with the ‘-a’ option and then print the file statistics using the stat command, as follows:

$ touch –a testfile

Use the following command on your terminal to display the file statistics:

$ touch testfile

In the following output, you will see that the access time has been changed with respect to the above output.

Similarly, you may use the touch command to change the modification time of an existing file using the ‘-m’ option, as follows:

$ touch -m testfile

In the following output, we have changed the modification time of the file ‘testfile.’

Copy Access and Modification Time of One File to Another

Using the touch command, you can copy the access and modification times of one file into another file.

Example

In this example, we will create a file named ‘testfileA’ and another file named ‘testfileB,’ as follows:

Here, we want to copy the access and modification times of testfileA to testfileB. To do so, execute the following touch command:

$ touch testfileA -r testfileB

Using the stat command, you can see that the access and modification times of testfileA have been copied to testfileB.

Creating a File with a Specified Timestamp

Using the touch command, you can also create a file with a specified timestamp, which will have the following basic syntax:

$ touch -t YYMMDDHHMM.SS filename

Example

In this example, we will create a new file with a specified timestamp, as follows:

$ touch -t 1907200830.30 testfile

Changing the Timestamp of a File

Using the touch command, you can also change the timestamp of an existing file, as follows:

$ touch -c -t YYMMDDHHMM.SS filename

Example

In the following example, we will change the timestamp of ‘testfile’ with some new timestamp.

$ touch -c -t 2002180715.15 testfile

This article covered the basic uses of the touch command in Linux. If you wish, feel free to explore more complex uses of the touch command using your Ubuntu 20.04 system. I hope you enjoyed this article.

About the author

Karim Buzdar

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. You can reach Karim on LinkedIn.