Search This Blog

Friday, December 13, 2013

touch: Changing the Time Stamps

The touch command changes these times and is used in the following manner:

# touch options expressions filename(s)

When touch is used without options or an expression, both times are set to the current time. The file is created if it doesn't exist.., but not overwritten if it does:

#touch note #Creates file if it doesn't exist

Various Options of Touch Command:
-a (touch -a) Update only the access time to current time
-c (touch -c) Don't create if the file doesn't exist
-d (touch -d) Set an arbitrary time for both times using free format human readable data string
-m (touch -m) Update only the modification time to current time
-r (touch -r) Update both file's times from a reference file
-t (touch -t) Set an arbitrary time for both time using stamp


If you want to check the file timestamp, you can do so with this command:

#stat file

[root@localhost indrajit]# stat abcd
  File: `abcd'
  Size: 0         Blocks: 0          IO Block: 4096   regular empty file
Device: 802h/2050d Inode: 281784      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2013-04-15 22:40:00.000000000 -0700
Modify: 2013-04-15 22:40:00.000000000 -0700
Change: 2013-12-12 20:08:43.506978374 -0800


Now let's use the command on file abcd, but only after seen its initial time stamps:


[root@localhost indrajit]# ls -l abcd Modification Time
-rw-r--r--. 1 root root 0 Apr 15  2013 abcd
[root@localhost indrajit]# ls -lu abcd Access Time
-rw-r--r--. 1 root root 0 Apr 15  2013 abcd


With the -m (modification) option, you can alter the modificaiton time alone:

#touch abcd
#ls -l abcd
-rw-r--r--   1 root       sys              0 Dec 13 05:33 abcd
#touch -m 02281030 abcd;ls -l abcd
-rw-r--r--   1 root       sys              0 Feb 28  2013 abcd

The -a (access) option changes the access time:

#ls -lu abcd
-rw-r--r--   1 root       sys              0 Dec 13 05:33 abcd
#touch -a 01261650 abcd;ls -lu abcd
-rw-r--r--   1 root       sys              0 Jan 26  2013 abcd


-c option
If you use this option, touch won’t do anything at all if the file specified doesn't exist. Look:

#ls -l xyz
xyz not found
#touch -c xyz;ls -l xyz
xyz not found


-r option
This option might come in handy if you want to copy a timestamp from a file to another file. Like so:


#ls -l abcd abcd1
-rw-r--r--   1 root       sys              0 Feb 28  2013 abcd
-rw-r--r--   1 root       sys              0 Dec 13 05:41 abcd1
#touch -r abcd1 abcd;ls -l abcd
-rw-r--r--   1 root       sys              0 Dec 13 05:41 abcd


-d and –t options
Both (-d) and (-t) options do the same thing, which is setting the same arbitrary timestamps for access and modification times. The difference is that (-d) uses free format human readable date, this means that you can use “Sun, 29 Feb 2004 16:21:42? or “2004-02-29 16:21:42” or even “next Thursday”. This option is complex to fully describe it here. On the other hand (-t) uses a simple stamp that you are confined to use. The stamp is [[CC]YY]MMDDhhmm[.ss]. [CC] is for century and you may ignore it and ignore the seconds as well. If you ignore [CC] the command will substitute it depending on what you enter as year. If you specify the year with only two digits, then CC is 20 for years in the range (0~68) and 19 for years in (69~99).


#ls -l abcd
-rw-r--r--   1 root       sys              0 Dec 13 05:41 abcd
#touch -t 3404152240 abcd;ls -l abcd
-rw-r--r--   1 root       sys              0 Apr 15  2034 abcd
#touch -t 8804152240 abcd;ls -l abcd
-rw-r--r--   1 root       sys              0 Apr 15  1988 abcd

In the first command the file timestamps will be set to: 15th April 2034 10:40 PM. While the second command will set it to: 15th April 1988 which is in a different century. If no year is specified it will be set to the current year. Example:


#touch -t 04152240 abcd;ls -l abcd
-rw-r--r--   1 root       sys              0 Apr 15  2013 abcd