The Linux touch command can be used for much more than simply creating an empty file on your system. You can use to change the timestamp of existing files including their access as well as modification times. This article presents 8 scenarios where you can utilize the touch command through your Debian Terminal.
We have run the commands and procedures mentioned in this article on a Debian 10 Buster system. Since the touch command is a command-line utility, we will be using Debian Terminal for this article. You can open the Terminal either through the Application Launcher search as follows:
The Application Launcher can be accessed through the Super/Windows key on your keyboard.
1. Create a single empty file
The simplest and most basic use of the touch command is to create an empty file through the command line. If you are a Terminal-savvy person, you can quickly create a new file in the command line through the following command:
$ touch filename
In the following example, I have created an empty file by the name “samplefile” through the touch command. I have then used the ls command to view the presence of the file on my system as the touch command does not prompt if the file has been created or not.
2. Create multiple files at once
Although the cat command and the standard redirect symbol are also ways to create files through the command line, the touch command takes an edge because you can create multiple files with it at once. You can use the following syntax in order to create multiple files through the touch command:
$ touch samplefile1 samplefile2 samplefile3 ….
In the following example I have created three files simultaneously through the touch command and then used the ls command in order to verify the presence of those files:
3. Force-avoid creating a new file
At times there is a need to avoid creating a new file if it already does not exist. In that case, you can use the ‘-c’ option with the touch command as follows:
$ touch -c filename
In the following example, I have used the touch command to forcefully avoid the creation of the mentioned new file.
When I use the ls command to list that file, the following output verifies that such a file does not exist on my system.
4. Change both ‘access’ and ‘modification’ times of a file
Another use of the touch command is to change both the access time and the modification time of a file.
Let us present an example to show how you can do it. I created a file named “testfile” through the touch command and viewed its statistics through 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 ran the touch command again for the “testfile”. You can see the changed access and modification times in the following image:
5. Change either access time or modification time
Instead of changing both the access and modification times, we can choose to change only one of them through the touch command.
In the following example, I created a file by the name of “samplefile” and viewed it statistics through the stat command:
I can change only the access time of this file by using the ‘-a’ option through the touch command on this file:
$ touch -a samplefile
The output of the stat command now shows that the access time has been changed to the time when I ran the touch command with the ‘-a’ option:
I can change only the modification time of this file by using the ‘-m’ option through the touch command on this file:
$ touch -m samplefile
The output of the stat command now shows that the modification time has been changed to the time when I ran the touch command with the ‘-m’ option:
6. How to copy access & modification time of one file to another existing file’s timestamp
Let us suppose we have a file named samplefileA with the following stats:
And another file named samplefileB with these stats:
If you want to change the access & modification time of samplefileA to that of the samplefileB, you can sue the touch command as follows:
$ touch samplefileA -r samplefileB
The output of the stat command in the above image shows that the samplefileA now has the same access and modify values as that of samplefileB.
7. Create a new file with a specified timestamp
In order to create a new empty file with a specified timestamp instead of the actual time you created it, you can use the following syntax of the touch command:
$ touch -t YYYYMMDDHHMM.SS filename
The following example shows how the stat command on my “oldfile” shows that its access and modification times are based on the timestamp I provided while creating it through the touch command:
8. Change timestamp of a file to some other time
You can change the timestamp of an existing file to another time using the following syntax of the touch command:
$ touch -c -t YYYYMMDDHHMM.SS filename
In the following example, I have changed the timestamp of an existing file through the touch command and then verified the changes through the stat command on that sample file:
Through the basic yet useful scenarios we presented in this article, you can begin to master the touch command and use it for quickly performing some seemingly complex tasks through the Debian command line.