How To Use a Touch Command in Linux
In this article we are knowing about how to use a Touch command and what are the use of this command. In Linux every single file is associated with timestamps, and every file stores the information of last access time, last modification time and last change time. So, whenever we create new file, access or modify an existing file, the timestamps of that file automatically updated.
Touch Command Options
- -a : change the access time only
- -c : if the file does not exist, do not create it
- -d : update the access and modification times
- -m: change the modification time only
- -r : use the access and modification times of file
- -t : creates a file using a specified time
1. How to Create an Empty File
The following touch command creates an empty (zero byte) new file called Tom.
2. How to Create Multiple Files
By using touch command, you can also create more than one single file. For example the following command will create 3 files named, Tom, Dom and Kom.
#touch Tom Dom Kom
3. How to Change File Access and Modification Time
To change or update the last access and modification times of a file called Dom, use the -a option as follows. The following command sets the current time and date on a file. If the Dom file does not exist, it will create the new empty file with the name.
#touch -a Dom
4. How to Avoid Creating New File
Using -c option with touch command avoids creating new files. For example the following command will not create a file called Dom if it does not exists.
#touch -c Dom
5. How to Change File Modification Time
If you like to change the only modification time of a file called Dom, then use the -m option with touch command. Please note it will only updates the last modification times (not the access times) of the file.
#touch -m Dom
6. Explicitly Set the Access and Modification times
You can explicitly set the time using -c and -t option with touch command. The format would be as follows.
#touch -c -t YYDDHHMM Dom
For example the following command sets the access and modification date and time to a file Dom as 19:30 (19:30 p.m.) August 23 of the current year (2017).
#touch -c -t 12101730 Dom
Next verify the access and modification time of file Dom, with ls -l command.
#ls -l total 2 -rw-r--r--. 1 root root 0 Aug 10 19:30 Dom
7. How to Use the time stamp of another File
The following touch command with -r option, will update the time-stamp of file Tom with the time-stamp of Kom file. So, both the file holds the same time stamp.
#touch -r Tom Kom
8. Create a File using a specified time
If you would like to create a file with specified time other than the current time, then the format should be.
#touch -t YYMMDDHHMM.SS Tom
We’ve almost covered all the options available in the touch command for more options use “man touch”.