Change file timestamp

Change file timestamp

There has been instances where I had to find the ability to change file timestamp without actually having to edit the file.

This can be achieved using the command touch.

Traditionally, touch has been more known for creating a blank empty file in Linux. There are however, a lot of things which you are do with the help of touch. One of them being the ability to change file timestamp.

Howto Change file timestamp

In order to Change file timestamp and set it to the current time, you simple need to use the -m flag for the touch command.

Change file timestamp – Set the current timestamp

The following command will change file timestamp and set it to the current time stamp.

[email protected]:/tmp/temp$ touch -m FILE_NAME

Example:

Let’s create an empty file (gagan-test.txt) to test this thing out.

[email protected]:/tmp/temp$ touch gagan-test.txt
[email protected]:/tmp/temp$ stat gagan-test.txt

Output:

  File: `gagan-test.txt'
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: 806h/2054d	Inode: 2957857     Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/   gagan)   Gid: ( 1000/   gagan)
Access: 2012-11-19 12:08:22.000000000 -0700
Modify: 2012-11-19 12:08:22.000000000 -0700
Change: 2012-11-19 12:08:22.000000000 -0700

[email protected]:/tmp/temp$ ll gagan-test.txt

Output:

-rw-r--r-- 1 gagan gagan 0 2012-11-19 12:08 gagan-test.txt

Now, let’s change file timestamp for this file.

[email protected]:/tmp/temp$ touch -m gagan-test.txt
[email protected]:/tmp/temp$ stat gagan-test.txt

Output:

  File: `gagan-test.txt'
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: 806h/2054d	Inode: 2957857     Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/   gagan)   Gid: ( 1000/   gagan)
Access: 2012-11-19 12:08:22.000000000 -0700
Modify: 2012-11-19 12:09:34.000000000 -0700
Change: 2012-11-19 12:09:34.000000000 -0700

[email protected]:/tmp/temp$ ll gagan-test.txt

Output:

-rw-r--r-- 1 gagan gagan 0 2012-11-19 12:09 gagan-test.txt

Change file timestamp – to an explicit timestamp

In order to set/change file timestamp by setting it to a specific date/time, we can use any of the two flags for touch command. These two flags are -t and -d or –date.

From the man page for touch, the syntax for setting the timestamp using the -t flag is given below:

# touch -t [[CC]YY]MMDDhhmm[.ss]

Where,

  • CC – is the first two digits of the year.
  • YY – is the last two digits of the year.
  • MM – is the month (digits).
  • DD – is the date.
  • hh – being the hour.
  • mm – is the minutes.
  • ss – specifies the seconds.

Continuing the example of the same file above, let’s set the timestamp for the file gagan-test.txt to May 14th 2013 at 19:30:05

[email protected]:/tmp/temp$ touch -t 201305141930.05 gagan-test.txt

Or

[email protected]:/tmp/temp$ touch -t 1305141930.05 gagan-test.txt

Now let’s check the results:

[email protected]:/tmp/temp$ stat gagan-test.txt

Output:

  File: `gagan-test.txt'
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: 806h/2054d	Inode: 2957857     Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/   gagan)   Gid: ( 1000/   gagan)
Access: 2013-05-14 19:30:05.000000000 -0700
Modify: 2013-05-14 19:30:05.000000000 -0700
Change: 2012-11-19 12:28:53.000000000 -0700

[email protected]:/tmp/temp$ ll gagan-test.txt

Output:

-rw-r--r-- 1 gagan gagan 0 2013-05-14 19:30 gagan-test.txt

Now we will change file timestamp for the same file using the -d or –date flag.

The flag -d or –date allows us to set the timestamp for the file as seen in the stat output:

Here is the example:

[email protected]:/tmp/temp$ touch -d “2014-05-14 19:30:05.000000000 -0700” gagan-test.txt

Verify the results.

[email protected]:/tmp/temp$ stat gagan-test.txt

Output:

  File: `gagan-test.txt'
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: 806h/2054d	Inode: 2957857     Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/   gagan)   Gid: ( 1000/   gagan)
Access: 2014-05-14 19:30:05.000000000 -0700
Modify: 2014-05-14 19:30:05.000000000 -0700
Change: 2012-11-19 12:35:00.000000000 -0700

No Comments

Post a Comment

Time limit is exhausted. Please reload CAPTCHA.