The Unix or Linux command touch is a way to create empty files. Here now you can update the modification and access time of each file with the help of touch command.
Example of touch command syntax below :
$ touch test1 test2
Now we have created two files namely ‘test1′ and ‘test2‘ through touch command. You can create multiple files just type all the file names with a single touch command followed by Enter key.
For example, if you would like to create ‘test1’, ‘test2’ and ‘test3’, then your command will be:
$ touch test1 test2 test3
Common Options of touch Command:
touch -aonly changes/updates the access time
touch -mchanges the modification time
touch -rThis command update time of one file with reference to the other file.
touch -tIt is used to create a file by specifying the time.
touch -cdoesn’t create any empty files.
Explanation of touch Command Options:
Linux touch -a command
The Linux of touch command with option ‘a’ is used to change the access time of a file. By default, it will take the current time of your system.
Syntax example of touch -a command :
$ touch -a <file name>
example of touch -a :
$ touch -a usr
You can see that access time of your file changed. You need to use stat command. Here “stat” means status.
Linux touch -m command
Unix and Linux system touch -m option will help you to change only the modification time of a file.
Sample syntax of touch -m command :
$ touch -m <file name>
Example of touch -m command :
$ touch -m infolinux.pdf
See your terminal after run the command, only modification time has been changed.
Linux touch -r command
This command will update time with reference to other mentioned file. There are two ways to use this command. Both does the samething.
We want to change the time of ‘test1′ with reference to ‘Test1’. You can do:
$ touch -r test1 Test1
$ touch Test1 –r test1
The time of both files is same!
Linux touch -t command
Now with this command, you can change the access time of a file by mentioning specified time to it.
You can modify the time by specified time instead of default time.
Format of time is
Linux touch -c command
If you can use -c option with touch command will not create an empty file, if that file doesn’t exist.
Syntax of touch -c command :
$ touch -c <file name>
Example of touch -c command :
$ touch -c test3
You can see, if we wanted to create file ‘test3’ but with ‘-c’ option no file has been created.