1. 程式人生 > 實用技巧 >教你在 Linux 下時光穿梭

教你在 Linux 下時光穿梭

時光穿梭?電影裡的橋段吧?良許你又在唬人?

非也非也,良許在這裡要給大家介紹 touch 命令,有了它你就可以改變時間戳,達到時光穿梭的目的。

touch 命令在我們的工作中使用也相當頻繁,我們就由淺到深來詳細講解。

touch 命令基本用法

提起 touch 命令,大家想到的肯定是它的兩個用法:

  • 改變時間戳
  • 建立新檔案

這兩種用法大家在工作中早已用膩了,良許就不再贅述了。

防止建立檔案

如果在 touch 後面直接跟上一個檔名,該檔案如果不存在的話,將建立一個相應名字的檔案。那麼如果我們只想改變檔案的時間戳,如果檔案不存在時不進行檔案建立該怎麼做?這裡需要加上 -c 選項。

[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

僅改變檔案訪問時間

我們知道,如果不帶任何選項執行 touch 命令時,檔案的訪問時間及修改時間都是同時被改變成當前系統時間。如下所示:

[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		# 在這裡使用 touch 命令
[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		# 檔案的訪問時間/修改時間均已改成當前系統時間
Modify: 2019-02-20 21:51:24.848774158 +0800
Change: 2019-02-20 21:51:24.848774158 +0800
 Birth: -

這裡使用到 stat 命令,可以檢視檔案更詳細的資訊。

如果我們只想改變檔案的訪問時間,只需加上 -a 選項即可, a 即是單詞 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		# 只有訪問時間的時間戳被改變了
Modify: 2019-02-20 21:51:24.848774158 +0800		# 修改時間保持不變
Change: 2019-02-20 21:56:40.858021859 +0800
 Birth: -

僅改變修改時間

如果我們只想改變檔案的修改時間,只需加上 -m 選項即可, m 即是單詞 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: -

更改為自定義時間戳

不管是不帶選項,還是帶上 -a-m 選項,都會將檔案相應的時間改為當前系統時間戳。那如果我們想改為自定義的時間戳呢?要怎麼處理?否則怎麼算得上時光穿梭?

我們有兩種方法來更改為自定義時間戳。

1. 加上 -t 選項

比如我們將檔案的時間戳改為一個將來時間:

[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: -

在這裡, -t 後面所帶的時間戳的格式為:

[[CC]YY]MMDDhhmm [.SS]

具體來講,是這樣的:

CC - 年份的前兩位 
YY - 年份的後兩位 
MM - 月份 [01-12]
DD - 日期 [01-31]
hh - 時 [00-23]
mm - 分 [00-59]
SS - 秒 [00-61]

2. 加上 -d 選項

我們再用新方法將檔案的時間戳改成一個過去的時間(2008年奧運會開幕式):

[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: -

在這裡,時間的格式為:日-月-年 。但是,這裡的時間可以相當靈活,比如也支援 yesterday1 year ago 等等模糊時間:

[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: -

除了更改時間,它還可以改時區。

更改時區,只需在 -d 後面跟上對應的時區就可以。


公眾號:良許Linux

有收穫?希望老鐵們來個三連擊,給更多的人看到這篇文章