教你在 Linux 下时光穿梭

系统 Linux
良许在这里要给大家介绍 touch 命令,有了它你就可以改变时间戳,达到时光穿梭的目的。touch 命令在我们的工作中使用也相当频繁,我们就由浅到深来详细讲解。

时光穿梭?电影里的桥段吧?良许你又在唬人?

非也非也,良许在这里要给大家介绍 touch 命令,有了它你就可以改变时间戳,达到时光穿梭的目的。

touch 命令在我们的工作中使用也相当频繁,我们就由浅到深来详细讲解。

[[335790]]

touch 命令基本用法

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

  • 改变时间戳
  • 创建新文件

这两种用法大家在工作中早已用腻了,良许就不再赘述了。

防止创建文件

如果在 touch 后面直接跟上一个文件名,该文件如果不存在的话,将创建一个相应名字的文件。那么如果我们只想改变文件的时间戳,如果文件不存在时不进行文件创建该怎么做?这里需要加上 -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 

仅改变文件访问时间

我们知道,如果不带任何选项执行 touch 命令时,文件的访问时间及修改时间都是同时被改变成当前系统时间。如下所示:

  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        # 文件的访问时间/修改时间均已改成当前系统时间 
  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 选项,都会将文件相应的时间改为当前系统时间戳。那如果我们想改为自定义的时间戳呢?要怎么处理?否则怎么算得上时光穿梭?

我们有两种方法来更改为自定义时间戳。

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

除了更改时间,它还可以改时区。

更改时区,只需在 -d 后面跟上对应的时区就可以。

本文授权转载自公众号「良许Linux」。良许,世界500强外企Linux开发工程师,公众号里分享大量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

2013-10-23 13:01:29

Ubuntu技巧时光机

2011-01-05 09:40:33

windowslinux

2009-06-15 13:56:02

linux下安装JBo

2011-03-03 10:49:37

Linux配置Proftpd

2011-03-04 12:33:15

linuxFileZilla

2022-01-17 07:50:37

Linux Patch项目

2011-09-01 09:14:49

UbuntuLaunchpad

2011-03-22 10:22:18

Windows环境Oracle

2011-09-05 13:19:42

UbuntuWebmin

2009-06-15 16:58:57

Java安装Linux

2020-06-01 16:25:43

WindowsLinux命令

2021-08-05 07:00:04

框性能优化element

2009-08-04 10:06:37

如何进入linux命令linux系统linux

2017-01-16 10:40:33

点赞
收藏

51CTO技术栈公众号