AMP AMP

Touch Command in Linux with Examples

Touch Command

In this article we will discuss the practical examples of touch command. Touch command is a standard program for Unix/Linux operating systems, used to create, change and modify timestamps of a file.

Options for Touch Command

-d is used to update the access and modification times
-r is used to access and modification times of file
-m is used to change the modification time only
-a is used to change the access time only
-t is used to create a file using a specified time
-c checks if the file does not exist, do not create it

To Create an Empty File

The following touch command is used to create an empty (zero byte)

[root@linuxhelp Desktop]# touch testfile
[root@linuxhelp Desktop]# ls -l
total 0
-rw-r--r-- 1 root root 0 Mar 15 10:59 testfile

To Create Multiple Files

The following touch command is used to create multiple files.

[root@linuxhelp Desktop]# touch testfile1 testfile2 testfile3

Now to list the files with ls command

[root@linuxhelp Desktop]# ls -l
total 0
-rw-r--r-- 1 root root 0 Mar 15 11:40 testfile
-rw-r--r-- 1 root root 0 Mar 15 11:48 testfile1
-rw-r--r-- 1 root root 0 Mar 15 11:48 testfile2
-rw-r--r-- 1 root root 0 Mar 15 11:48 testfile3

To Change File Access and Modification Time

-a option is used to change or update the last access and modification times of a file called testfile2.

[root@linuxhelp Desktop]# touch -a testfile4

Now to list the files with ls -l command

[root@linuxhelp Desktop]# ls -l
total 0
-rw-r--r-- 1 root root 0 Mar 15 11:40 testfile
-rw-r--r-- 1 root root 0 Mar 15 11:48 testfile1
-rw-r--r-- 1 root root 0 Mar 15 11:48 testfile2
-rw-r--r-- 1 root root 0 Mar 15 11:48 testfile3
-rw-r--r-- 1 root root 0 Mar 15 12:00 testfile4

It sets the current time and date on a file. If the testfile4 does not exist, then it will create the new empty file with the name of testfile4.

To Avoid Creating New File

-c option with touch command avoids creating new files.

[root@linuxhelp Desktop]# touch -c testfile5


now to list the files with ls -l command

[root@linuxhelp Desktop]# ls -l
total 0
-rw-r--r-- 1 root root 0 Mar 15 11:40 testfile
-rw-r--r-- 1 root root 0 Mar 15 11:48 testfile1
-rw-r--r-- 1 root root 0 Mar 15 11:48 testfile2
-rw-r--r-- 1 root root 0 Mar 15 11:48 testfile3
-rw-r--r-- 1 root root 0 Mar 15 12:00 testfile4

To Change File Modification Time

-m option with touch command is used change file modification time.

[root@linuxhelp Desktop]# touch -m testfile2

Now to list the files with ls -l command

[root@linuxhelp Desktop]# ls -l
total 0
-rw-r--r-- 1 root root 0 Mar 15 11:40 testfile
-rw-r--r-- 1 root root 0 Mar 15 11:48 testfile1
-rw-r--r-- 1 root root 0 Mar 15 12:16 testfile2
-rw-r--r-- 1 root root 0 Mar 15 11:48 testfile3
-rw-r--r-- 1 root root 0 Mar 15 12:00 testfile4

To explicitly set the Access and Modification times

-c and -t option with touch command is used to explicitly set the Access and Modification times.

touch -c -t YYDDHHMM testfile3

Example
The following command sets the access and modification date and time to a file testfile3 as 17:30 (17:30 p.m.) March 19 of the current year (2016).

[root@linuxhelp Desktop]# touch -c -t 03191730 testfile3

To verify the access and modification time of file

To verify the access and modification time of file testfile3, with ls -l command

[root@linuxhelp Desktop]# ls -l
total 0
-rw-r--r-- 1 root root 0 Mar 15 11:40 testfile
-rw-r--r-- 1 root root 0 Mar 15 11:48 testfile1
-rw-r--r-- 1 root root 0 Mar 15 12:16 testfile2
-rw-r--r-- 1 root root 0 Mar 19  2016 testfile3
-rw-r--r-- 1 root root 0 Mar 15 12:00 testfile4

To Use the time stamp of another File

-r option is used to update the time-stamp of file testfile1 with the time-stamp of testfile2 file.

[root@linuxhelp Desktop]# touch -r testfile2 testfile1

To verify the the time stamp of another file

To verify the the time stamp of another file, with ls -l command

[root@linuxhelp Desktop]# ls -l
total 0
-rw-r--r-- 1 root root 0 Mar 15 11:40 testfile
-rw-r--r-- 1 root root 0 Mar 15 12:16 testfile1
-rw-r--r-- 1 root root 0 Mar 15 12:16 testfile2
-rw-r--r-- 1 root root 0 Mar 19  2016 testfile3
-rw-r--r-- 1 root root 0 Mar 15 12:00 testfile4

To create a File using a specified time

touch -t YYMMDDHHMM.SS linuxhelp


-t option gives the linuxhelp file a time stamp of 13:30:55 p.m. on March 15, 2016.

[root@linuxhelp Desktop]# touch -t 201603151330.55 linuxhelp
FAQ
Q
What is the syntax for using touch command ?
A
The touch's syntax is

touch [option] file_name(s)
Q
How to Avoid Creating New File for particular file name?
A
Use the following options "-c" along with "touch" command and followd by the file name
Q
How to create timestamp for specific files ?
A
You can make use of the following command
touch -r testfile2 testfile1 to create a timestamp for the specific file
Q
How to provide specific timestamp for specific file?
A
you can use the below mentioned command
touch -t YYMMDDHHMM.SS "file name"
Q
How to create multiple files via single command?
A
Use the following commands
"touch testfile1 testfile2 testfile3"
Three files will be created in the name testfile1 testfile2 testfile3