The Linux touch command can be used not only to create an empty file on your system. You can use existing timestamps to change the timestamp, including their access, as well as modification time. This article presents 8 scenarios in which you can use the touch command through your Debian terminal.
We ran the commands and procedures mentioned in this article on the Debian 10 Buster system. Since the touch command is a command line utility, we will use the Debian terminal for this article. You can open the Terminal through a search in the Application Launcher as follows:
Access to the Application Launcher can be obtained using the Super / Windows key on the keyboard.
1. Create one empty file
The simplest and most basic use of a touch command is to create an empty file through the command line. If you are good at terminals, you can quickly create a new file on the command line with the following command:
$ touch filename
In the following example, I created an empty file called “samplefile” using the touch command. Then I used the ls command to see if the file was available on my system, since the touch command does not ask if the file was created or not.
2. Create multiple files at once
Although the cat command and the standard redirection character are also ways to create files through the command line, the touch command has the advantage because you can create multiple files at once. You can use the following syntax to create multiple files using the touch command:
$ touch samplefile1 samplefile2 samplefile3 ….
In the following example, I created three files at the same time using the touch command, and then used the ls command to check for the presence of these files:
3. Forcedly avoid creating a new file
Sometimes it is necessary to avoid creating a new file if it no longer exists. In this case, you can use the “-c” option with the touch command as follows:
$ touch -c filename
In the following example, I used the touch command to forcibly avoid creating the mentioned new file.
When I use the ls command to output this file, the following output verifies that such a file does not exist on my system.
4. Change the access time and file changes
Another use of the touch command is to change both access time and file modification time.
Let’s provide an example to show how you can do this. I created a file called “testfile” using the touch command and looked at its statistics using the stat command:
Then I entered the following touch command:
$ touch testfile
This touch command changed the access and modification time to the time when I again ran the touch command for the “test file”. You can see the changed access time and changes in the following image:
5. Change either access time or modification time
Instead of changing the access and change times, we can only change one of them with a touch command.
In the following example, I created a file called “samplefile” and looked at its statistics using the stat command:
I can change only access time of this file using the “-a” option using the touch command for this file:
$ touch -a samplefile
The output of the stat command now shows that the access time was changed to the time when I ran the touch command with the ‘-a’ parameter:
I can change only modification time of this file using the “-m” option with the touch command for this file:
$ touch -m samplefile
The output of the stat command now shows that the change time was changed to the time when I ran the touch command with the “-m” option:
6. How to copy access time and changes of one file to another existing timestamp
Suppose we have a file called samplefileA with the following statistics:
And another file called samplefileB with the following statistics:
If you want to change the access and change times of samplefileA to the time of sampleB, you can issue the touch command as follows:
$ touch samplefileA -r samplefileB
The output of the stat command in the image above shows that samplefileA now has the same access and changes the values as samplefileB.
7. Create a new file with the specified time stamp
To create a new empty file with the specified timestamp instead of the actual time at which you created it, you can use the following touch command syntax:
$ touch -t YYYYMMDDHHMM.SS filename
The following example shows how the stat command in my “old file” shows that its access and change time is based on the timestamp that I provided when I created it using the touch command:
8. Change the timestamp of the file to another time
You can change the timestamp of an existing file to another time using the following touch command syntax:
$ touch -c -t YYYYMMDDHHMM.SS filename
In the following example, I changed the timestamp of an existing file using the touch command, and then checked the changes using the stat command in this example file:
Using the basic but useful scripts that we presented in this article, you can learn the touch command and use it to quickly complete some seemingly complex tasks through the Debian command line.
8 Common use of the Debian touch command