Time travel under Linux

Time:2021-4-17

Time travel? The bridge in the movie, right? Liangxu, are you bluffing again?

No, it’s not. Maybe I’ll introduce the touch command here. With it, you can change the timestamp to achieve the purpose of time travel.

Touch command is also frequently used in our work. Let’s explain it in detail from shallow to deep.

Basic usage of touch command

When we talk about the touch command, we must think of its two usages:

  • Change timestamp
  • create a new file

We are tired of using these two usages in our work. Liang Xu will not repeat them.

Prevent file creation

If a file name is directly followed by the touch, if the file does not exist, a file with the corresponding name will be created. So if we just want to change the time stamp of the file, what should we do if we don’t create the file when it doesn’t exist? It needs to be added here-cOptions.

[[email protected]_0_16_centos test]$ touch -c alvin
[[email protected]_0_16_centos test]$ ll alvin
ls: cannot access alvin: No such file or directory

Change file access time only

We know that if you execute the touch command without any options, the file access time and modification time will be changed to the current system time at the same time. As follows:

[[email protected]_0_16_centos test]$ stat file
  File: ‘file’
  Size: 10              Blocks: 8          IO Block: 4096   regular file
Device: fd01h/64769d    Inode: 371115      Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/   alvin)   Gid: ( 1000/   alvin)
Access: 2019-02-20 14:20:21.154819675 +0800
Modify: 2019-02-20 14:20:21.154819675 +0800
Change: 2019-02-20 14:20:21.191819649 +0800
 Birth: -
[ [email protected]_ 0_ 16_ CentOS test] $touch file # use the touch command here
[[email protected]_0_16_centos test]$ stat file
  File: ‘file’
  Size: 10              Blocks: 8          IO Block: 4096   regular file
Device: fd01h/64769d    Inode: 371115      Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/   alvin)   Gid: ( 1000/   alvin)
Access: 2019-02-20 21:51:24.848774158 + 0800
Modify: 2019-02-20 21:51:24.848774158 +0800
Change: 2019-02-20 21:51:24.848774158 +0800
 Birth: -

Here, you can use the stat command to view more detailed information of the file.

If we just want to change the access time of the file, just add-aA is the abbreviation of the word access.

[[email protected]_0_16_centos test]$ touch -a file
[[email protected]_0_16_centos test]$ stat file
  File: ‘file’
  Size: 10              Blocks: 8          IO Block: 4096   regular file
Device: fd01h/64769d    Inode: 371115      Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/   alvin)   Gid: ( 1000/   alvin)
Access: February 20, 2019 21:56:40.858021859 + 0800
Modify: February 20, 2019 21:51:24.848774158 + 0800
Change: 2019-02-20 21:56:40.858021859 +0800
 Birth: -

Change modification time only

If we just want to change the modification time of the file, just add-mM is the abbreviation of the word modify.

[[email protected]_0_16_centos test]$ touch -m file
[[email protected]_0_16_centos test]$ stat file
  File: ‘file’
  Size: 10              Blocks: 8          IO Block: 4096   regular file
Device: fd01h/64769d    Inode: 371115      Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/   alvin)   Gid: ( 1000/   alvin)
Access: 2019-02-20 21:56:40.858021859 +0800
Modify: 2019-02-20 22:07:39.138701655 +0800
Change: 2019-02-20 22:07:39.138701655 +0800
 Birth: -

Change to custom timestamp

Whether it’s with or without options-aor-mOption, the corresponding time of the file will be changed to the current system timestamp. What if we want to change it to a custom timestamp? How to deal with it? Otherwise, how can it be called time travel?

We have two ways to change to a custom timestamp.

1. Add-toption

For example, we change the time stamp of the file to a future time:

[[email protected]_0_16_centos test]$ touch -t 202001012020.20 file
[[email protected]_0_16_centos test]$ stat file
  File: ‘file’
  Size: 10              Blocks: 8          IO Block: 4096   regular file
Device: fd01h/64769d    Inode: 371115      Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/   alvin)   Gid: ( 1000/   alvin)
Access: 2020-01-01 20:20:20.000000000 +0800
Modify: 2020-01-01 20:20:20.000000000 +0800
Change: 2019-02-20 22:13:01.526965566 +0800
 Birth: -

Here, the format of the time stamp after – t is:

[[CC]YY]MMDDhhmm [.SS]

Specifically, it is as follows:

CC - top two of year 
YY - the last two digits of the year 
Mm - month [01-12]
DD - date [01-31]
HH - hour [00-23]
Mm - min [00-59]
SS - second [00-61]

2. Add-doption

We use a new method to change the time stamp of the document to a past time (2008 Olympic opening ceremony)

[[email protected]_0_16_centos test]$ touch -d '08-August-2008' file
[[email protected]_0_16_centos test]$ stat file
  File: ‘file’
  Size: 10              Blocks: 8          IO Block: 4096   regular file
Device: fd01h/64769d    Inode: 371115      Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/   alvin)   Gid: ( 1000/   alvin)
Access: 2008-08-08 00:00:00.000000000 +0800
Modify: 2008-08-08 00:00:00.000000000 +0800
Change: 2019-02-20 22:25:47.808490725 +0800
 Birth: -

Here, the format of time is:Day month year. However, the time here can be quite flexible, such as supportyesterday1 year agoAnd so on

[[email protected]_0_16_centos test]$ touch -d 'yesterday 08-August-2008' file
[[email protected]_0_16_centos test]$ stat file
  File: ‘file’
  Size: 10              Blocks: 8          IO Block: 4096   regular file
Device: fd01h/64769d    Inode: 371115      Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/   alvin)   Gid: ( 1000/   alvin)
Access: 2008-08-07 00:00:00.000000000 +0800
Modify: 2008-08-07 00:00:00.000000000 +0800
Change: 2019-02-20 22:31:57.564725604 +0800
 Birth: -

In addition to changing the time, it can also change the time zone.

To change the time zone, just follow the corresponding time zone after – D.


Official account: good Linux

Time travel under Linux

What’s the gain? I hope the old fellow will take three strikes to show this article to more people.