Updated May 29, 2023
Introduction to Linux Touch Command
In UNIX or Linux operating system, touch command is used to create an empty file and/or update the access, modification, change timestamp of the file or directory. The touch command avoids the opening, updating, saving and closing the file. It will directly update the date or timestamp associated with the file or directory. The Linux Touch command line utility was written by Paul Rubin, Arnold Robbins, Jim Kingdon, David MacKenzie and Randy Smith.
Syntax of tee Command:
touch [ OPTION ] FILE NAME
- touch: Using the Touch keyword in the command | syntax. It will take the two sets of arguments as an OPTION and FILE NAME and do the necessary change in timestamp or create the empty file.
- OPTION: We can provide the different flags as an option to touch command.
- FILE: Input file name.
How Linux Touch Command Works?
The Linux touch command simply creates an empty file and it is useful to change the timestamp value of files and directories. Basically, below are the two different variants to create the file in the Linux operating system.
Touch Command: It just creates the file without content. When the user does not have the data to store in the file then the touch file will create.
Cat Command: It will create the file with content data.
Examples to Implement of Linux Touch Command
Below are the example of Linux Touch Command:
Example #1 – Touch Command
With the help of touch command, we are able to create an empty file without any contain.
Command:
pwd
ls
ll
touch file.txt
ll
Explanation:
We are having a sample directory. In the same directory, we don’t have any file in it (refer screenshot 1 (a)). With the help of the touch command, we are creating an empty “file.txt” file. With the help of “ls” or “ll” command we are able to list all the files and check the more information about the file (refer screenshot 1 (b)).
Output:
Screenshot 1 (a)
Screenshot 1 (b)
Example #2 – Touch Command with “-a” Option
In the Touch command, we are able to change the last access timestamp of the file. To change the last access timestamp we can use the “-a” option in touch command.
Command:
touch -a file.txt
stat file.txt
Explanation:
In the sample directory, we have “file.txt” file in it. The access time of the file is 13:24:20 (01:24 PM) (refer the screenshot 2 (a)). Now we need to change the access timestamp of the file. We are using “-a” option with touch command and change the access time stamp. Now the updated access timestamp is (31:31:08) (01:31 PM)(refer the screenshot 2 (b)).
Output:
Screenshot 2 (a)
Screenshot 2 (b)
Example #3 – Touch Command with “-c” Option
In the Touch command, we are having the functionality to avoid duplicated. If the same file name is present in the working directory then the touch command handles duplicate file creation. We need to use the “-c” option in the touch command.
Command:
touch -c file1.txt
ls
Explanation:
In the above command, we can able to avoid duplicate file creation. In the sample directory, we already are having the “file.txt” file. But while creating a new file with same file name the “-c” option will avoid creating the duplicate in the same working directory.
Output:
Example #4 – Touch Command with “-r” Option
In touch command, we are having the functionality to update the timestamp of the other file. We need to use the “-r” option in the touch command.
Command:
touch -r /etc/hosts file.txt
Explanation:
As per the above command, we are modifying the timestamp the same timestamp of (/etc/hosts) file to “file.txt” file. The timestamp of the “/etc/hosts” file is (20:01:32, 2013-06-07) (refer screenshot 3 (a)). With the help of the “-r” option, we are able to update the previous file timestamp to new file timestamp (refer screenshot 3 (b)).
Output:
Screenshot 3 (a)
Screenshot 3 (a)
Example #5 – Touch Command with “-t” Option
In the Linux touch command, we are able to create the older date timestamp files as well. We need to use the “-t” option in the touch command.
Command:
touch -t 1909212108 data_file.txt
Explanation:
As per the above touch command, we are able to the older date file as compare to system time. With the help of the “-t” option and time format “YYMMDDHHMM”, we are able to create the older file.
Output:
Example #6 – Touch Command with “-m” Option
In the Linux touch command, we are able to change the file modification timestamp only. We need to use the “-m” option in the touch command.
Command:
touch -m data_file.txt
Explanation:
As per the above touch command, we are able to modify the file modification time only. As per the output, the older timestamp of “data_file.txt” file is (2019-09-21). Now with the help of “-m” option, we are able to change the modification timestamp of “data_file.txt” file.
Output:
Example #7 – Touch Command to Create the Multiple Files
In Linux or UNIX operating system, we are able to create multiple files with single touch command. Just we need to provide the multiple files or format of multiple files.
Command:
touch f1 f2 f3 f4 f5
touch f{1..5}
ls
Explanation:
As per the requirement, we are able to create multiple files. There are different ways to create multiple files. As per the above touch command, we have select two different ways to create multiple files.
Output :
Example #8 – Touch Command with “-c -d” Option
In touch command, we are able to change both the access timestamp and file modification timestamp. We need to use both “-c” and ”-d” option in the touch command.
Command:
touch -c -d 10081009 file.txt
stat file.txt
Explanation:
We are able to change both the access and modification timestamp of the file.
Output:
Conclusion
We have seen the uncut concept of “Linux Touch Command” with the proper example, explanation, and command with different outputs. The default touch command is used to create the empty or no contain the file. On runtime, we are able to create the touch file and write the files as well.
Recommended Article
We hope that this EDUCBA information on “Linux Touch Command” was beneficial to you. You can view EDUCBA’s recommended articles for more information.