How to manipulate timestamps in Linux / Unix

The Unix / Linux touch command modifies the files and directories timestamps. If the file does not exist, touch creates it with the current timestamp.

How to display file/folder timestamps:

I will use stat to display the access, modify, and inode change timestamps and date to display the time:

$ date && touch a
Fri Jun 15 04:19:33 EEST 2012
$ stat a | tail -3
Access: 2012-06-15 04:19:33.394439182 +0300
Modify: 2012-06-15 04:19:33.394439182 +0300
Change: 2012-06-15 04:19:33.394439182 +0300

How to change the access and modification timestamps:

Touch -a manipulates the access timestamp and touch -m changes the modification time, both to the current time:

$ touch -a file
$ touch -m file
$ touch -am file

How to change the timestamp refering to a file’s timestamps:

You can set a file’s timestamp identically to another file’s. touch -r file1 file2 will set to file2 the file1’s timestamps.

If you want to make file2 50 seconds older than file1, use touch -r file1 -B 50 file2.

To set file2 50 seconds newer to file1, use touch -r file1 -F 50 file2.

$ touch -r a b
$ touch -r a -B 50 b
$ touch -r a -F 50 b

How to set specific timestamps:

Syntax: touch -t YYYYMMDDhhmm file

touch -t 200001011200 file will set the timestamps the 01st of January 2000, at 12:00.

$ touch -t 200001011200 file

Tagged with: , , , , ,
Posted in The Linux and Unix Articles!

Leave a Reply

Your email address will not be published. Required fields are marked *




Subscribe to get the latest Linux news and how to guides directly on your e-mail!