7 Tricks of touch Command on Linux

Linux touch Command

“touch”  command is a standard Unix command-line interface program which is used to create new, empty files and update the access date and /or modification date of a file.

touch’s syntax is:

touch [option] file_name

Touch Command Options:
“-a”         Change only the access time .
“-c”         Do not create any files,if file does not exist.
“-d ”        Parse STRING and use it instead of current time that means it used to update the access and modification times.
“-m”        Change only the modification time.
“-r ”         Use the access and modification times of file
“-t”           Create new file by in-steading of current time.
“–help”   Display this help and exit.

In this article we will cover some useful tricks examples using touch command.

1) Create an Empty File using touch command.

The following command used to create an empty file.

#touch <filename>

[root@server1 gaurav]# touch fedora
[root@server1 gaurav]# ls -l
total 0
-rw-r--r--. 1 root root 0 Oct 14 10:34 fedora
[root@server1 gaurav]#

Note: “fedora” is called filename that will be created using touch command at 10:34.

2) Create multiple files.

The following command will create the multiple empty files.
#touch <filename1> <filename2> <filename3> <filename4>

[root@server1 gaurav]# date
Fri Oct 14 10:37:22 EDT 2016
[root@server1 gaurav]# touch ubuntu centos redhat debian
[root@server1 gaurav]# ls -l
total 0
-rw-r--r--. 1 root root 0 Oct 14 10:37 centos
-rw-r--r--. 1 root root 0 Oct 14 10:37 debian
-rw-r--r--. 1 root root 0 Oct 14 10:34 fedora
-rw-r--r--. 1 root root 0 Oct 14 10:37 redhat
-rw-r--r--. 1 root root 0 Oct 14 10:37 ubuntu
[root@server1 gaurav]#

To avoid Creating a New File you can use “-c” option, If you using “-c” option, and if a file doesn’t exists, touch will not create the file.

[root@server1 gaurav]# touch -c fedora
[root@server1 gaurav]# ls -l fedora
-rw-r--r--. 1 root root 0 Oct 14 10:38 fedora
[root@server1 gaurav]#

Note: As above, the following command will change the time stamp of the file called fedora (Previously the timestamps was 10:34 and now 10:38) and it will not create a file if it does not exists.

You can also create multiple file using below command.

[root@server1 gaurav]# touch file{1..10}.txt
[root@server1 gaurav]# ls
file10.txt  file1.txt  file2.txt  file3.txt  file4.txt  file5.txt  file6.txt  file7.txt  file8.txt  file9.txt  ubuntu centos redhat debian
[root@server1 gaurav]#

3) Change the Access time of file.

You can use “-a” option with touch command to change the timestamps of a file.You can use the “stat” command, to check out the last access and modification time of file.

Before touch command is executed use following command.

#stat <filename>

[root@server1 gaurav]# stat  ubuntu
  File: ‘ubuntu’
  Size: 0             Blocks: 0          IO Block: 4096   regular empty file
Device: fd03h/64771d    Inode: 137         Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2016-10-14 10:37:31.181000000 -0400
Modify: 2016-10-14 10:37:31.181000000 -0400
Change: 2016-10-14 10:37:31.181000000 -0400
 Birth: -
[root@server1 gaurav]#

Note that the access time of “ubuntu” file.

[root@server1 gaurav]# touch -a ubuntu 
[root@server1 gaurav]# stat ubuntu
  File: ‘ubuntu’
  Size: 0             Blocks: 0          IO Block: 4096   regular empty file
Device: fd03h/64771d    Inode: 137         Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2016-10-14 10:40:45.391000000 -0400
Modify: 2016-10-14 10:37:31.181000000 -0400
Change: 2016-10-14 10:40:45.391000000 -0400
 Birth: -
[root@server1 gaurav]#

After executing the above command the access time of “ubuntu” file has been changed.

4) Change Modification Time of file.

You can also use “-m” option to change the access and modification time of file.

[root@server1 gaurav]# stat ubuntu
  File: ‘ubuntu’
  Size: 0             Blocks: 0          IO Block: 4096   regular empty file
Device: fd03h/64771d    Inode: 137         Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2016-10-14 10:40:45.391000000 -0400
Modify: 2016-10-14 10:37:31.181000000 -0400
Change: 2016-10-14 10:40:45.391000000 -0400
 Birth: -
[root@server1 gaurav]# touch -m ubuntu
[root@server1 gaurav]# stat ubuntu
  File: ‘ubuntu’
  Size: 0             Blocks: 0          IO Block: 4096   regular empty file
Device: fd03h/64771d    Inode: 137         Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2016-10-14 10:40:45.391000000 -0400
Modify: 2016-10-14 10:42:41.732000000 -0400
Change: 2016-10-14 10:42:41.732000000 -0400
 Birth: -
[root@server1 gaurav]#

5) Copy the Time-stamp from Another File.

You can use “-r” option to update the timestamps of another file with touch command. Using following command, the time-stamp of file fedora will update with the timestamps of ubuntu file.

[root@server1 gaurav]# ls -l ubuntu redhat 
-rw-r--r--. 1 root root 0 Oct 14 10:45 redhat
-rw-r--r--. 1 root root 0 Oct 14 10:37 ubuntu
[root@server1 gaurav]# touch -r redhat ubuntu 
[root@server1 gaurav]# ls -l ubuntu redhat 
-rw-r--r--. 1 root root 0 Oct 14 10:45 redhat
-rw-r--r--. 1 root root 0 Oct 14 10:45 ubuntu
[root@server1 gaurav]#

Note: As shown above, Both the files hold the same time-stamp.

6)  Make a File using a specified time.

You can use “-t” option to create a file with specified time other than the current time.

#touch -t YYMMDDHHMM.SS <filename>

[root@server1 gaurav]# date
Fri Oct 14 11:47:32 IST 2016
[root@server1 gaurav]# touch -t 201609150930.45 Linuxmint
[root@server1 gaurav]# ls -l Linuxmint
-rw-rw-r-- 1 nagios nagios 0 Sep 15 09:30 Linuxmint
[root@server1 gaurav]#

In above example,Using “-t” option the file “Linuxmint” create with the timestamp of 09:30:45 p.m. on September 15, 2016.

7) Update the access and modification times.

You can use “-d” option to change or modification of file.

[root@server1 gaurav]# ls -l Linuxmint
total 0
-rw-rw-r-- 1 nagios nagios 0 Sep 15 09:30 Linuxmint
[root@server1 gaurav]# touch -d '-4 day' Linuxmint
[root@server1 gaurav]# ll
total 0
-rw-rw-r-- 1 nagios nagios 0 Oct 10 14:15 Linuxmint
[root@server1 gaurav]#

In above example,Using “-d” option the file “Linuxmint” create with the timestamp of  Oct 10 14:15.

You can also use option for setting in hours;

[root@server1 gaurav]# ll Linuxmint
-rw-rw-r-- 1 nagios nagios 0 Oct 10 14:15 Linuxmint
[root@server1 gaurav]# touch -d '-4 hour' Linuxmint
[root@server1 gaurav]# ll Linuxmint
-rw-rw-r-- 1 nagios nagios 0 Oct 14 10:50 Linuxmint
[root@server1 gaurav]#

The file “Linuxmint” create with the timestamp of  Oct 14 10:50  from Oct 10 14:15.!!!

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz