時光穿梭?電影里的橋段吧?良許你又在唬人?
非也非也,良許在這里要給大家介紹 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: -
在這里,時間的格式為:日-月-年 ,但是,這里的時間可以相當靈活,比如也支持 yesterday 、 1 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
有識訓?希望老鐵們來個三連擊,給更多的人看到這篇文章
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/17210.html
標籤:Linux
上一篇:常用Linux命令
下一篇:Linux常用基礎命令
