In Linux distributions, each document that is created on the system is provided with some timestamps. Each document must store the information about the last access time, the last modification time, and the last modification time. So all the timestamp information is updated when you create, access or modify a file.
The “touch” command is a program used in Linux operating systems to create, edit, and modify timestamps of a particular file. In this article, we will examine some common examples of the Linux touch command in Rocky Linux 8
Using the touch Command on Rocky Linux
To use touch command on Rocky Linux, you need to open the terminal window to press ‘Ctrl + Alt + t’.
Create an empty file on Linux
The touch command is used to create a new empty file. To create a new file you will execute the following command on the terminal:
Create multiple files on Linux
The touch command is also used to create more than one file. The following command is used to create multiple files:
$touch testfile1 testfile2 testfile3
Change file modification and access time
You can update or change the last modification and access time of a file. The -a option is used with touch command as follows.
$ touch -a workplace
The above-mentioned command changes the date and current time on a file. If the ‘workplace’ file does not exist, then, in this case, a new file will create with a similar name.
Use -c option to avoid creating new file
The touch command with -c option is used to avoid creating a new file. For example, an empty file with ‘testdata’ name will not create on the system, if the file doesn’t exist. Type the following command on the terminal:
$ touch -c testdata
Change the file modification time
If you want to change only the modification time instead of the last access time of ‘testfile’ then you will use touch command with -m option. Use the following command on the terminal to perform this action:
$ touch -m testfile
Explicitly set modification and access time of files
You can explicitly change the modification and access time of a file. The basic format of this command is given below:
$ touch -c -t YYDDHHMM testfile
For example, if you want to set the modification date and access time to a file ‘testfile’ as 17:30 (17:30 p.m.) 2 January of the current year (2022).
$touch -c -t 01021730 testfile
To verify this command, use the following command:
$ ls -l
Copy the timestamp of other files
The touch command with -r option is used to update the timestamp of ‘testfile1’ and replace it with the timestamp of ‘testfile’ file. Both files will hold the same timestamp:
$ touch -r testfile testfile1
File creation with specified time
If you want to create a file with a specified time instead of assigning the current time then you will use the following format of the command:
$ touch -t YYMMDDHHMM.SS filename
For example, if you want to use touch command with option -t will give the ‘testfile’ file a timestamp of 17:30:40 p.m. on January 03, 2020.
$ touch -t 202001031730.40 testfile
Some useful touch command options with their purpose are given below:
- -a – is used to only update the access time
- -c – is used when the file does not exist, then do not make a new file
- -d – is used to change both modification and access time
- -m – is used to only update the modification time
- -r – is used to make the access and modification times similar to the other file
- -t – is used to create a file using a specified time
In this article, you learned about different uses of the touch command in Rocky Linux 8. You also learned several commands that will be useful to you in the future. I hope you enjoyed this article and learned new things.