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:
Example
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:
Example
Using the touch command, we will create three files with three different names in the example given below:
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:
Example
In this example, we will force the system to avoid creating a new file using the touch command, as follows:
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.
$ stat testfile
To change the access time and modified time of the file ‘testfile,’ execute the following command:
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:
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:
Use the following command on your terminal to display the file statistics:
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:
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:
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:
Example
In this example, we will create a new file with a specified timestamp, as follows:
Changing the Timestamp of a File
Using the touch command, you can also change the timestamp of an existing file, as follows:
Example
In the following example, we will change the timestamp of ‘testfile’ with some new timestamp.
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.
from Linux Hint https://ift.tt/3hNnbe3
0 Comments