关于linux:教你在-Linux-下时光穿梭

1次阅读

共计 3647 个字符,预计需要花费 10 分钟才能阅读完成。

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

非也非也,良许在这里要给大家介绍 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 学习路线图 ,于是我依据本人的教训,利用业余时间熬夜肝了一个月,整顿了一份电子书。无论你是面试还是自我晋升,置信都会对你有帮忙!

收费送给大家,只求大家金指给我点个赞!

电子书 | Linux 开发学习路线图

也心愿有小伙伴能退出我,把这份电子书做得更完满!

有播种?心愿老铁们来个三连击,给更多的人看到这篇文章

举荐浏览:

  • 干货 | 程序员进阶架构师必备资源免费送
  • 神器 | 反对搜寻的资源网站
正文完
 0