自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

教你在 Linux 下時光穿梭

系統(tǒng) Linux
良許在這里要給大家介紹 touch 命令,有了它你就可以改變時間戳,達到時光穿梭的目的。touch 命令在我們的工作中使用也相當頻繁,我們就由淺到深來詳細講解。

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

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

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

[[335790]]

touch 命令基本用法

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

  • 改變時間戳
  • 創(chuàng)建新文件

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

防止創(chuàng)建文件

如果在 touch 后面直接跟上一個文件名,該文件如果不存在的話,將創(chuàng)建一個相應名字的文件。那么如果我們只想改變文件的時間戳,如果文件不存在時不進行文件創(chuàng)建該怎么做?這里需要加上 -c 選項。

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

僅改變文件訪問時間

我們知道,如果不帶任何選項執(zhí)行 touch 命令時,文件的訪問時間及修改時間都是同時被改變成當前系統(tǒng)時間。如下所示:

  1. [alvin@VM_0_16_centos test]$ stat file 
  2.   File: ‘file’ 
  3.   Size: 10              Blocks: 8          IO Block: 4096   regular file 
  4. Device: fd01h/64769d    Inode: 371115      Links: 1 
  5. Access: (0664/-rw-rw-r--)  Uid: ( 1000/   alvin)   Gid: ( 1000/   alvin) 
  6. Access: 2019-02-20 14:20:21.154819675 +0800 
  7. Modify: 2019-02-20 14:20:21.154819675 +0800 
  8. Change: 2019-02-20 14:20:21.191819649 +0800 
  9.  Birth: - 
  10. [alvin@VM_0_16_centos test]$ touch file        # 在這里使用 touch 命令 
  11. [alvin@VM_0_16_centos test]$ stat file 
  12.   File: ‘file’ 
  13.   Size: 10              Blocks: 8          IO Block: 4096   regular file 
  14. Device: fd01h/64769d    Inode: 371115      Links: 1 
  15. Access: (0664/-rw-rw-r--)  Uid: ( 1000/   alvin)   Gid: ( 1000/   alvin) 
  16. Access: 2019-02-20 21:51:24.848774158 +0800        # 文件的訪問時間/修改時間均已改成當前系統(tǒng)時間 
  17. Modify: 2019-02-20 21:51:24.848774158 +0800 
  18. Change: 2019-02-20 21:51:24.848774158 +0800 
  19.  Birth: - 

這里使用到 stat 命令,可以查看文件更詳細的信息。

如果我們只想改變文件的訪問時間,只需加上 -a 選項即可, a 即是單詞 access 的縮寫。

  1. [alvin@VM_0_16_centos test]$ touch -a file 
  2. [alvin@VM_0_16_centos test]$ stat file 
  3.   File: ‘file’ 
  4.   Size: 10              Blocks: 8          IO Block: 4096   regular file 
  5. Device: fd01h/64769d    Inode: 371115      Links: 1 
  6. Access: (0664/-rw-rw-r--)  Uid: ( 1000/   alvin)   Gid: ( 1000/   alvin) 
  7. Access: 2019-02-20 21:56:40.858021859 +0800        # 只有訪問時間的時間戳被改變了 
  8. Modify: 2019-02-20 21:51:24.848774158 +0800        # 修改時間保持不變 
  9. Change: 2019-02-20 21:56:40.858021859 +0800 
  10.  Birth: - 

僅改變修改時間

如果我們只想改變文件的修改時間,只需加上 -m 選項即可, m 即是單詞 modify 的縮寫。

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

更改為自定義時間戳

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

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

1. 加上 -t 選項

比如我們將文件的時間戳改為一個將來時間:

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

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

  1. [[CC]YY]MMDDhhmm [.SS] 

具體來講,是這樣的:

  1. CC - 年份的前兩位  
  2. YY - 年份的后兩位  
  3. MM - 月份 [01-12] 
  4. DD - 日期 [01-31] 
  5. hh - 時 [00-23] 
  6. mm - 分 [00-59] 
  7. SS - 秒 [00-61] 

2. 加上 -d 選項

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

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

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

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

除了更改時間,它還可以改時區(qū)。

更改時區(qū),只需在 -d 后面跟上對應的時區(qū)就可以。

本文授權轉載自公眾號「良許Linux」。良許,世界500強外企Linux開發(fā)工程師,公眾號里分享大量Linux干貨,歡迎關注!

責任編輯:趙寧寧 來源: 今日頭條
相關推薦

2011-09-09 18:16:18

WindowsLinux

2011-10-21 07:55:28

2020-07-27 10:15:51

LinuxJenkins命令

2009-11-26 09:24:48

Suse Linux

2020-05-22 08:52:08

LinuxPython工具

2011-09-08 13:15:00

UbuntuFoxit Reade

2022-01-17 07:50:37

Linux Patch項目

2013-10-23 13:01:29

Ubuntu技巧時光機

2011-01-05 09:40:33

windowslinux

2011-03-03 10:49:37

Linux配置Proftpd

2009-06-15 13:56:02

linux下安裝JBo

2011-03-04 12:33:15

linuxFileZilla

2011-09-01 09:14:49

UbuntuLaunchpad

2009-06-15 16:58:57

Java安裝Linux

2011-09-05 13:19:42

UbuntuWebmin

2011-03-22 10:22:18

Windows環(huán)境Oracle

2020-06-01 16:25:43

WindowsLinux命令

2009-11-16 13:17:20

SRP服務LinuxFTP服務器

2010-10-25 09:58:23

Chrominum 9

2010-03-15 16:03:57

Ubuntu Linu
點贊
收藏

51CTO技術棧公眾號