A Guide to Time Travel in Linux

Click the “blue text” above to add it to your “starred” or “pinned” list.

Essential content delivered to you first.A Guide to Time Travel in Linux

From: Liangxu Linux (WeChat ID: liangxuxiansheng)

Time travel? Is that just a scene from a movie? Liangxu, are you trying to fool us again?

No, not at all. Here, Liangxu will introduce the touch command, which allows you to change timestamps and achieve the goal of time travel.

The touch command is frequently used in our work, so we will explain it in detail from basic to advanced.

Basic Usage of the touch Command

When mentioning the touch command, everyone likely thinks of its two main uses:

  • Changing timestamps

  • Creating new files

These two uses are already familiar to everyone, so Liangxu will not elaborate further.

Prevent File Creation

If you directly follow touch with a filename, it will create a file with that name if it does not exist. But what if we only want to change the file’s timestamp without creating a file if it doesn’t exist? In this case, you need to add the <span>-c</span> option.

[alvin@VM_0_16_centos test]$ touch -c alvin
[alvin@VM_0_16_centos test]$ ll alvin
ls: cannot access alvin: No such file or directory

Change Only the File Access Time

We know that if the touch command is executed without any options, both the access time and modification time of the file are changed to the current system time. As shown below:

[alvin@VM_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: -
[alvin@VM_0_16_centos test]$ touch file        # Using the touch command here
[alvin@VM_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        # Both access and modification times have been changed to the current system time
Modify: 2019-02-20 21:51:24.848774158 +0800
Change: 2019-02-20 21:51:24.848774158 +0800
 Birth: -

Here, the stat command is used to view more detailed information about the file.

If we only want to change the file’s access time, we can simply add the <span>-a</span> option, where a is an abbreviation for the word access.

[alvin@VM_0_16_centos test]$ touch -a file
[alvin@VM_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        # Only the access time's timestamp has been changed
Modify: 2019-02-20 21:51:24.848774158 +0800        # Modification time remains unchanged
Change: 2019-02-20 21:56:40.858021859 +0800
 Birth: -

Change Only the Modification Time

If we only want to change the file’s modification time, we can simply add the <span>-m</span> option, where m is an abbreviation for the word modify.

[alvin@VM_0_16_centos test]$ touch -m file
[alvin@VM_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 a Custom Timestamp

Whether using no options or using the <span>-a</span> or <span>-m</span> options, the corresponding file’s time will be changed to the current system timestamp. But what if we want to change it to a custom timestamp? How can we achieve time travel then?

We have two methods to change to a custom timestamp.

1. Add the -t option

For example, we can change the file’s timestamp to a future time:

[alvin@VM_0_16_centos test]$ touch -t 202001012020.20 file
[alvin@VM_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 timestamp following -t is:

[[CC]YY]MMDDhhmm [.SS]

Specifically, it is as follows:

CC - The first two digits of the year 
YY - The last two digits of the year 
MM - Month [01-12]
DD - Day [01-31]
hh - Hour [00-23]
mm - Minute [00-59]
SS - Second [00-61]

2. Add the -d option

We can also use a new method to change the file’s timestamp to a past time (the opening ceremony of the 2008 Olympics):

[alvin@VM_0_16_centos test]$ touch -d '08-August-2008' file
[alvin@VM_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 the time is:<span>day-month-year</span>. However, the time can be quite flexible, as it also supports <span>yesterday</span>, <span>1 year ago</span>, and other relative times:

[alvin@VM_0_16_centos test]$ touch -d 'yesterday 08-August-2008' file
[alvin@VM_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 timezone.

To change the timezone, simply follow -d with the corresponding timezone:

A Guide to Time Travel in Linux

Long press the QR code to follow for more exciting content

Leave a Comment