The major application of the touch
command is to create files without any content. Creating empty files may seem to be a wastage of the system’s memory but there is a catch here. touch
command comes at your disposal when you wish to create time stamps or logs whilst working on a large project which requires log keeping and monitoring. You can simply create an empty file using touch
command and the file will have the time and date stamp when it was created. (In the file information and not as the content of the file).
The second important usage of the touch
command is to modify the access and modification time for a file. Every file/directory has a unique timestamp added to it. It shows the Access and Modification time of the file/directory. But this information can be changed easily using the touch
command.
Let us see the use of touch
command in more details with self-explanatory examples.
Options used with touch
command
There are various options available to be used with the touch
command.
Option | Description |
---|---|
-a | change access time of the file |
-m | change the modified time of the file |
-c | change access time of the file without creating a new one |
-r | use a reference file for setting the timestamps of file or directory |
-d | setting the same arbitrary timestamp for access and modification time using free format human readable date |
-t | create a file using a specified time |
Using touch
Command
The touch
command can be used to create empty files without using any option. Follow the examples given below to create single as well as multiple empty files.
To Create An Empty File
You can use the touch
command without any option to create a single empty file.
Syntax:
touch [file-name]
Example:
gaurav@ubuntu:~/workspace$ touch iift.txt
Here I have created a file named iift.txt using touch
command. Use the ls
command to see if the file is created.
gaurav@ubuntu:~/workspace$ ls
iift.txt init1 init2 init3 init4
To see the file information you can use the stat
command as follows.
gaurav@ubuntu:~/workspace$ stat iift.txt
File: iift.txt
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 808h/2056d Inode: 1990385 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ gaurav) Gid: ( 1000/ gaurav)
Access: 2020-09-14 10:18:43.318160860 +0530
Modify: 2020-09-14 10:18:43.318160860 +0530
Change: 2020-09-14 10:18:43.318160860 +0530
Birth: -
gaurav@ubuntu:~/workspace$
To Create Multiple Empty Files
If you wish to create multiple empty files at once you can use the touch
command in the following way.
Syntax:
touch [filename-1] [filename-2] [filename-3]
Example:
gaurav@ubuntu:~/workspace$ touch log1.txt ssh.txt filemove.c
gaurav@ubuntu:~/workspace$ ls -l
total 36
-rw-r--r-- 1 gaurav gaurav 0 Sep 14 10:35 filemove.c
-rw-r--r-- 1 gaurav gaurav 0 Sep 14 10:18 iift.txt
-rw-r--r-- 1 gaurav gaurav 0 Sep 14 10:35 log1.txt
-rw-r--r-- 1 gaurav gaurav 0 Sep 14 10:35 ssh.txt
Here I have created three files ‘log1.txt’, ‘ssh.txt’ and ‘filemove.c’. These have different file extensions.
You can also create empty files in bulk using the following way.
Syntax:
touch [filename]-{1..n}.txt
Example:
We will create 10 files viz. sheldon_log-1.txt, sheldon_log-2.txt and so on up to sheldon_log-10.txt
gaurav@ubuntu:~/workspace$ touch sheldon_log-{1..0}.txt
gaurav@ubuntu:~/workspace$
gaurav@ubuntu:~/workspace$ ls
filemove.c iift.txt sheldon_log-10.txt sheldon_log-2.txt sheldon_log-4.txt sheldon_log-6.txt sheldon_log-8.txt
gsy.c log1.txt sheldon_log-1.txt sheldon_log-3.txt sheldon_log-5.txt sheldon_log-7.txt sheldon_log-9.txt
gaurav@ubuntu:~/workspace$
Change or Modify Access Time of File or Directory
The access timestamp is the last time a file was read. Whenever a user reads a file, the access timestamp for that file gets updated. Access timestamp denotes the time when the file was last accessed. There is no modification taking place with this file or directory but it’s only being referenced or read.
Using touch
command with option -a
modifies the ‘Access Time’ of a particular file. The new ‘Access Time’ will be set to the current date and time. To check the current Access time and Modification time use the stat
command.
Modifying File
Syntax:
touch -a [filename]
Example:
We will see the current access time of the file to be modified.
gaurav@ubuntu:~/workspace$ stat log1.txt
File: log1.txt
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 808h/2056d Inode: 2001385 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ gaurav) Gid: ( 1000/ gaurav)
Access: 2020-09-13 23:52:19.305416141 +0530
Modify: 2020-09-13 23:52:00.215090207 +0530
Change: 2020-09-13 23:52:19.305416141 +0530
Birth: -
gaurav@ubuntu:~/workspace$
Using touch -a
to change the access Time of the file log1.txt to the current date and time.
Output:
gaurav@ubuntu:~/workspace$ touch -a log1.txt
gaurav@ubuntu:~/workspace$
gaurav@ubuntu:~/workspace$ stat log1.txt
File: log1.txt
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 808h/2056d Inode: 2001385 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ gaurav) Gid: ( 1000/ gaurav)
Access: 2020-09-14 10:59:24.972855176 +0530
Modify: 2020-09-13 23:52:00.215090207 +0530
Change: 2020-09-14 10:59:24.972855176 +0530
Birth: -
gaurav@ubuntu:~/workspace$
You can observe that the access time of the file log1.txt is now modified.
Modifying Directory
touch -a
can also be used to modify the access time of a directory in the same way as done for a file.
Syntax:
touch -a [directory_path]
Example:
gaurav@ubuntu:~$ stat ./workspace/tomcat
File: ./workspace/tomcat
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: 808h/2056d Inode: 2039942 Links: 3
Access: (0775/drwxrwxr-x) Uid: ( 1000/ gaurav) Gid: ( 1000/ gaurav)
Access: 2018-03-30 11:18:28.912666800 +0530
Modify: 2018-03-30 00:13:02.452194000 +0530
Change: 2020-09-14 11:18:28.912666800 +0530
Birth: -
Using touch -a
to modify the access time for the directory tomcat.
gaurav@ubuntu:~$ touch -a ./workspace/tomcat
gaurav@ubuntu:~$ stat ./workspace/tomcat
File: ./workspace/tomcat
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: 808h/2056d Inode: 2039942 Links: 3
Access: (0775/drwxrwxr-x) Uid: ( 1000/ gaurav) Gid: ( 1000/ gaurav)
Access: 2020-09-14 11:21:10.638538949 +0530
Modify: 2018-03-30 00:13:02.452194000 +0530
Change: 2020-09-14 11:21:10.638538949 +0530
Birth: -
gaurav@ubuntu:~$
Here, the access time of the directory ‘tomcat’ is now changed to the current date and time.
Change Modification Time Of File/Directory
Modification time signifies the last time the contents of a file were modified. A program or process either edited or modified the file. “Modified” means something inside the file was amended, deleted or new data was added.
Using touch
command along with the option -m
modifies the ‘Modification Time’ of a file or a directory. Following methods can be deployed for file and directory.
For File
touch -m [filename]
Example:
Firstly, using stat
command to display the Modification time for the file.
gaurav@ubuntu:~/workspace$ stat iift.txt
File: iift.txt
Size: 66 Blocks: 8 IO Block: 4096 regular file
Device: 808h/2056d Inode: 1990385 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ gaurav) Gid: ( 1000/ gaurav)
Access: 2020-09-14 11:33:36.927262587 +0530
Modify: 2020-09-12 08:33:28.339190370 +0530
Change: 2020-09-12 08:33:28.339190370 +0530
Now use the touch -m
command to modify the Modification time.
gaurav@ubuntu:~/workspace$ touch -m iift.txt
gaurav@ubuntu:~/workspace$ stat iift.txt
File: iift.txt
Size: 66 Blocks: 8 IO Block: 4096 regular file
Device: 808h/2056d Inode: 1990385 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ gaurav) Gid: ( 1000/ gaurav)
Access: 2020-09-14 11:33:36.927262587 +0530
Modify: 2020-09-14 11:34:34.719723531 +0530
Change: 2020-09-14 11:34:34.719723531 +0530
Birth: -
gaurav@ubuntu:~/workspace$
The ‘Modification time’ of the file iift.txt
is now changed to the current date and time.
For Directory
touch -m [directory_path] OR [directory_name]
Example:
We will change the Modification Time of the directory named ‘tomcat’ using the touch -m
command. Let us find out its current ‘Modification Time’ using the stat
command.
gaurav@ubuntu:~/workspace$ stat tomcat
File: tomcat
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: 808h/2056d Inode: 2039942 Links: 3
Access: (0775/drwxrwxr-x) Uid: ( 1000/ gaurav) Gid: ( 1000/ gaurav)
Access: 2020-09-14 11:21:10.638538949 +0530
Modify: 2019-07-12 11:43:22.482485281 +0530
Change: 2019-07-12 11:43:22.482485281 +0530
Birth: -
Output:
gaurav@ubuntu:~/workspace$ touch -m tomcat
gaurav@ubuntu:~/workspace$ stat tomcat
File: tomcat
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: 808h/2056d Inode: 2039942 Links: 3
Access: (0775/drwxrwxr-x) Uid: ( 1000/ gaurav) Gid: ( 1000/ gaurav)
Access: 2020-09-14 11:21:10.638538949 +0530
Modify: 2020-09-14 11:43:22.482485281 +0530
Change: 2020-09-14 11:43:22.482485281 +0530
Birth: -
gaurav@ubuntu:~/workspace$
Here, I have changed the Modification timestamp for the directory ‘tomcat’.
Changing Access Time Without Creating New File
touch
command when run, creates a new empty file. But there may occur some instances where the file is already created and you just want to modify the Access Time of that file without creating a new one.
In this situation, you can use touch
command with the -c
option which avoids creating a new file and still allows you to modify the Access timestamp.
Syntax:
touch -c [filename]
Example:
gaurav@ubuntu:~/workspace$ touch -c iift.txt
gaurav@ubuntu:~/workspace$ ls
demo gsy.c log1.txt sheldon_log-1.txt sheldon_log-3.txt sheldon_log-5.txt sheldon_log-7.txt sheldon_log-9.txt
filemove.c iift.txt sheldon_log-10.txt sheldon_log-2.txt sheldon_log-4.txt sheldon_log-6.txt sheldon_log-8.txt tomcat
gaurav@ubuntu:~/workspace$
Similarly, you can change the access time of the already existing directory. You can use the following method to do so.
Example:
Displaying the current access timestamp of the directory tomcat.
gaurav@ubuntu:~/workspace$ stat tomcat
File: tomcat
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: 808h/2056d Inode: 2039942 Links: 3
Access: (0775/drwxrwxr-x) Uid: ( 1000/ gaurav) Gid: ( 1000/ gaurav)
Access: 2020-09-14 11:21:10.638538949 +0530
Modify: 2020-09-14 11:43:22.482485281 +0530
Change: 2020-09-14 11:43:22.482485281 +0530
Birth: -
gaurav@ubuntu:~/workspace$
gaurav@ubuntu:~/workspace$ touch -c tomcat
Here, I have changed the access time of the already existing directory ‘tomcat’.
Output:
gaurav@ubuntu:~/workspace$ stat tomcat
File: tomcat
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: 808h/2056d Inode: 2039942 Links: 3
Access: (0775/drwxrwxr-x) Uid: ( 1000/ gaurav) Gid: ( 1000/ gaurav)
Access: 2020-09-14 20:18:52.625031128 +0530
Modify: 2020-09-14 20:18:52.625031128 +0530
Change: 2020-09-14 20:18:52.625031128 +0530
Birth: -
gaurav@ubuntu:~/workspace$
Cross checking the output:
gaurav@ubuntu:~/workspace$ touch -c temp.cpp
gaurav@ubuntu:~/workspace$ ls temp.cpp
ls: cannot access 'temp.cpp': No such file or directory
gaurav@ubuntu:~/workspace$
Here, I tried to use touch -c
with temp.cpp
. This file doesn’t exist. Hence, we can say that using touch
with option -c
avoids creating new empty files and modifies the timestamp for the existing file only.
Modifying Access and Modification Timestamp To a Specific Date And Time
In the previous blocks, you must have observed that whenever we use the touch
command to change the timestamps, it is changed to the current time and date.
But, there may be some occurrences where you may need to use a customized time and date. Using touch
command with -c
and -t
options can serve the purpose.
Example:
I’ll change the Access and Modify timestamp in the file iift.txt to a customized time and date.
gaurav@ubuntu:~/workspace$ stat iift.txt
File: iift.txt
Size: 66 Blocks: 8 IO Block: 4096 regular file
Device: 808h/2056d Inode: 1990385 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ gaurav) Gid: ( 1000/ gaurav)
Access: 2020-09-14 12:04:07.091786565 +0530
Modify: 2020-09-14 12:04:07.091786565 +0530
Change: 2020-09-14 12:04:07.091786565 +0530
Birth: -
Date and Time can be specified in the format: {CCYY}MMDDhhmm.ss
Parameter | Description |
CC | First two digits of a year |
YY | Second two digits of a year |
MM | Month of the Year (01-12) |
DD | Day of the Month (01-31) |
hh | Hour of the day (00-23) |
mm | Minutes of the hour (00-59) |
I am using the date stamp as 203011051820 (i.e. 5-November-2030, 18:20 hours).
gaurav@ubuntu:~/workspace$ touch -c -t 203011051820 iift.txt
This command will change the Access and Modify timestamp for the file iift.txt as follows.
gaurav@ubuntu:~/workspace$ stat iift.txt
File: iift.txt
Size: 66 Blocks: 8 IO Block: 4096 regular file
Device: 808h/2056d Inode: 1990385 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ gaurav) Gid: ( 1000/ gaurav)
Access: 2030-11-05 18:20:00.000000000 +0530
Modify: 2030-11-05 18:20:00.000000000 +0530
Change: 2020-09-14 20:39:55.641781140 +0530
Birth: -
gaurav@ubuntu:~/workspace$
Using a Reference File To Set Timestamp
touch
command can also be used with the option -r
to use the timestamp of a reference file of your choice on your current file.
Syntax:
touch -r [reference_file] [actual_file]
Example:
We will use gsy.c
file as a reference file. So the timestamps of gsy.c
will be used for the file iift.txt
. Let us check the current timestamps of both these files first with stat
command.
gaurav@ubuntu:~/workspace$ stat gsy.c
File: gsy.c
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 808h/2056d Inode: 2001385 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ gaurav) Gid: ( 1000/ gaurav)
Access: 2020-09-14 10:59:24.972855176 +0530
Modify: 2020-09-13 23:52:00.215090207 +0530
Change: 2020-09-14 10:59:24.972855176 +0530
Birth: -
gaurav@ubuntu:~/workspace$ stat iift.txt
File: iift.txt
Size: 66 Blocks: 8 IO Block: 4096 regular file
Device: 808h/2056d Inode: 1990385 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ gaurav) Gid: ( 1000/ gaurav)
Access: 2025-10-19 18:20:00.000000000 +0530
Modify: 2025-10-19 18:20:00.000000000 +0530
Change: 2020-09-14 20:39:55.641781140 +0530
Using the command touch -r
now.
gaurav@ubuntu:~/workspace$ touch -r gsy.c iift.txt
Output:
gaurav@ubuntu:~/workspace$ stat iift.txt
File: iift.txt
Size: 66 Blocks: 8 IO Block: 4096 regular file
Device: 808h/2056d Inode: 1990385 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ gaurav) Gid: ( 1000/ gaurav)
Access: 2020-09-14 10:59:24.972855176 +0530
Modify: 2020-09-13 23:52:00.215090207 +0530
Change: 2020-09-14 21:04:27.640026328 +0530
Birth: -
gaurav@ubuntu:~/workspace$
From the output, we can see that the timestamps for the file iift.txt
has changed. The new timestamps are now similar to those of the reference file gsy.c
Conclusion
In this tutorial, we learned about the touch
command and various options available with it to create new empty files and amend the timestamps of existing files in various ways.
Member discussion