https://vitux.com/8-common-uses-of-the-linux-touch-command
The Linux touch command can be used for much more than simply creating an empty file on Linux. You can use it 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 Linux Terminal.
We have run the commands and procedures mentioned in this article on a Ubuntu 18.04 LTS system. Since the touch command is a command line utility, we will be using the Ubuntu Terminal for this article. You can open the Terminal either through the system Dash or the Ctrl+Alt+T shortcut.
$ touch “filename”
Example:
When I use the ls command to list that file, the following output verifies that such a file does not exist in my system.
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:
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:
I can change only the modification time of this file by using the ‘-m’ option through the touch command on this file:
And another file named samplefileB:
If you want to change the access & modification time of samplefileA to that of the samplefileB, you can sue the touch command as follows:
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.
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 Linux command line.
The Linux touch command can be used for much more than simply creating an empty file on Linux. You can use it 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 Linux Terminal.
We have run the commands and procedures mentioned in this article on a Ubuntu 18.04 LTS system. Since the touch command is a command line utility, we will be using the Ubuntu Terminal for this article. You can open the Terminal either through the system Dash or the Ctrl+Alt+T shortcut.
1. Create a single empty file with the touch command
The simplest and the 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”
Example:
$ touch samplefileIn the following example, I have created an empty file with 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 with touch command
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 view the presence of those files:
3. Force avoid creating a new file with touch command
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 in 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 testfileThis 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 samplefileThe 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 samplefileThe 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 from one file to another file
Let us suppose we have a file named samplefileA:And another file named samplefileB:
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 YYMMDDHHMM.SS “filename”The following example shows how the stat command on my samplefile 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 some other time using the following syntax of the touch command:$ touch -c -t YYMMDDHHMM.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 Linux command line.
No comments:
Post a Comment