touch –Command Explained

touch –Command Explained

by -
0 552

touch command is very useful, does  two things..

1. Create new file if file is not exist.
2. Change Time Stamp (modification time ) if file already exist.

Create a zero byte file (empty file)

[root@localhost ~]# touch file1

file is created now
[root@localhost ~]# stat file1                  <————-to check the status of file
File: `file1′
Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fd00h/64768d    Inode: 260632      Links: 1                 <—————-inode of the file
Access: (0644/-rw-r–r–)  Uid: (    0/    root)   Gid: (    0/    root)      <—-showing permission
Access: 2016-07-30 17:53:52.192385971 +0530
Modify: 2016-07-30 17:53:52.192385971 +0530           <—–modification time
Change: 2016-07-30 17:53:52.192385971 +0530
[root@localhost ~]#

or

[root@localhost ~]# ls -l
total 72
-rw——-. 1 root root  1411 Jul 20 20:27 anaconda-ks.cfg
drwxr-xr-x. 2 root root  4096 Jul 23 18:49 backup
-rw-r–r–. 1 root root     0 Jul 30 17:53 file1      <——showing size and modification time
-rw-r–r–. 1 root root  8664 Jul 30 18:00 hello
-rw-r–r–. 1 root root 27312 Jul 20 20:27 install.log

Change Access Time Only

We see that Access time and Modify time has the same value which is 17:53:52. and the timezone is GMT +0530 .

Now if we want to change only the Access time, we need to use -a option.

[root@localhost ~]# touch -a file1
[root@localhost ~]# stat file1
File: `file1′
Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fd00h/64768d    Inode: 260632      Links: 1
Access: (0644/-rw-r–r–)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2016-07-30 18:04:35.524382876 +0530           <——-Access Time Changed
Modify: 2016-07-30 17:53:52.192385971 +0530            <—–Modify Time remain same
Change: 2016-07-30 18:04:35.524382876 +0530
[root@localhost ~]#

Change Modification time only

[root@localhost ~]# touch -m file1
[root@localhost ~]# stat file1
File: `file1′
Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fd00h/64768d    Inode: 260632      Links: 1
Access: (0644/-rw-r–r–)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2016-07-30 18:04:35.524382876 +0530        <————–Access time remain same
Modify: 2016-07-30 18:23:15.668384445 +0530        <————- Changed modification time
Change: 2016-07-30 18:23:15.668384445 +0530
[root@localhost ~]#

Set custom time stamp

Both -a and -m option will change the file timestamp into current time. We can also possible to change it into custom timestamp. To do this, use -t option.

From file1 above example, we knew that its time stamp are :

  • 18:04:35 for Access time
  • 18:23:15 for Modify time
  • 2016-07-30 for the date

Let say we want to change it into 11:00:23 for both Access time and Modify time and 15 august 1947. To do this, we can use this command :

[root@localhost ~]# touch -t 194708151100.23 file1
[root@localhost ~]# stat file1
File: `file1′
Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fd00h/64768d    Inode: 260632      Links: 1
Access: (0644/-rw-r–r–)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 1947-08-15 11:00:23.000000000 +0530
Modify: 1947-08-15 11:00:23.000000000 +0530
Change: 2016-07-30 18:34:43.310389358 +0530
[root@localhost ~]#

 194708151100.23   is a time syntax   <—denotes  date 15 august 1947 and time 11:00:23

-t option is consist of :

[[CC]YY]MMDDhhmm [.SS]

  • CC – The first two digits of the year
  • YY – The second two digits of the year
  • MM – The month of the year [01-12]
  • DD – The day of the month [01-31]
  • hh – The hour of the day [00-23]
  • mm – The minute of the hour [00-59]
  • SS – The second of the minute [00-61]

Another way to change custom timestamp

we have two files here Install.log and  file1, need to copy timestamp of file Install.log to file1

[root@localhost ~]# stat install.log
File: `install.log’
Size: 27312           Blocks: 64         IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 260612      Links: 1
Access: (0644/-rw-r–r–)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2016-07-20 20:24:15.672000007 +0530
Modify: 2016-07-20 20:27:10.045999991 +0530
Change: 2016-07-20 20:27:14.281999990 +0530
[root@localhost ~]#
[root@localhost ~]# stat file1
File: `file1′
Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fd00h/64768d    Inode: 260632      Links: 1
Access: (0644/-rw-r–r–)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2016-07-30 08:30:00.000000000 +0530
Modify: 2016-07-30 08:30:00.000000000 +0530
Change: 2016-07-30 18:57:04.814382966 +0530
[root@localhost ~]#
[root@localhost ~]# touch file1 -r install.log
[root@localhost ~]# stat file1
File: `file1′
Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fd00h/64768d    Inode: 260632      Links: 1
Access: (0644/-rw-r–r–)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2016-07-20 20:24:15.672000007 +0530
Modify: 2016-07-20 20:27:10.045999991 +0530  <————-Time Stamp copied here from install.log
Change: 2016-07-30 18:59:08.099381653 +0530
[root@localhost ~]#

Enjoy Linux !!!

 

CEO, KV IT-Solutions Pvt. Ltd. | vikas@kvit.in | 9810028374|
Linux Professional and an Industrial Trainer | 20 + years Experience in IT Industry

” We are born free, No Gate and Windows can snatch our freedom “

Download PDF

SIMILAR ARTICLES

0 890

0 572

1 534

NO COMMENTS

Leave a Reply

Required Captcha *