Wednesday 11 September 2013

Time Stamp of a file in Linux


Every Linux file is associated with timestamps, which specifies the last access time, last modification time and last change time.

To get the timestamp of a file "ls -l" and "stat".

1. Use -c option to avoid creating new files, but will change the timestamp by present time.

stat tgs.txt

  File: `tgs.txt'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 802h/2050d      Inode: 525402      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2012-10-18 23:42:12.000000000 -0700
Modify: 2012-10-18 23:42:12.000000000 -0700
Change: 2013-09-11 08:49:16.509859887 -0700

touch -c tgs.txt

stat tgs.txt

  File: `tgs.txt'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 802h/2050d      Inode: 525402      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2013-09-11 08:49:25.685239864 -0700
Modify: 2013-09-11 08:49:25.685239864 -0700
Change: 2013-09-11 08:49:25.685239864 -0700

2. Change File’s Access Time using -a.

stat tgs.txt

  File: `tgs.txt'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 802h/2050d      Inode: 525402      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2013-09-11 08:10:44.783796541 -0700
Modify: 2013-09-11 08:10:44.783796541 -0700
Change: 2013-09-11 08:10:44.783796541 -0700

touch -a tgs.txt

stat tgs.txt

  File: `tgs.txt'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 802h/2050d      Inode: 525402      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2013-09-11 08:13:14.824798981 -0700
Modify: 2013-09-11 08:10:44.783796541 -0700
Change: 2013-09-11 08:13:14.824798981 -0700

3. Change File’s Modification Time using -m.

touch -m *.o

stat tgs.txt

  File: `tgs.txt'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 802h/2050d      Inode: 525402      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2013-09-11 08:13:14.824798981 -0700
Modify: 2013-09-11 08:10:44.783796541 -0700
Change: 2013-09-11 08:13:14.824798981 -0700

touch -m tgs.txt

stat tgs.txt

  File: `tgs.txt'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 802h/2050d      Inode: 525402      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2013-09-11 08:13:14.824798981 -0700
Modify: 2013-09-11 08:16:36.600236392 -0700
Change: 2013-09-11 08:16:36.600236392 -0700

4. Modification time using -t and -d.

touch -t [CCYYMMDDHHMM.SS] filename

touch -t 201212312359.59 tgs.txt 
touch -a -m -t 201212312359.59 tgs.txt 

stat tgs.txt

  File: `tgs.txt'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 802h/2050d      Inode: 525402      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2012-12-31 23:59:59.000000000 -0800
Modify: 2012-12-31 23:59:59.000000000 -0800
Change: 2013-09-11 08:30:24.706252619 -0700

touch -d "2012-10-19 12:12:12.000000000 +0530" tgs.txt

stat tgs.txt

  File: `tgs.txt'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 802h/2050d      Inode: 525402      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2012-10-18 23:42:12.000000000 -0700
Modify: 2012-10-18 23:42:12.000000000 -0700
Change: 2013-09-11 08:37:51.182799786 -0700

5. Copy the Time-stamp from Another File using -r.

ls -l
-rw-r--r--. 1 root root 0 Sep 11 08:43 test.txt
-rw-r--r--. 1 root root 0 Oct 19  2012 tgs.txt

touch test.txt -r tgs.txt

ls -l

-rw-r--r--. 1 root root 0 Oct 19  2012 test.txt
-rw-r--r--. 1 root root 0 Oct 19  2012 tgs.txt

ref: http://www.thegeekstuff.com/2012/11/linux-touch-command/

No comments:

Post a Comment