时光穿梭? 电影里的桥段吧? 良许你又在唬人?
非也非也, 良许在这里要给大家介绍 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.cnblogs.com/yychuyu/p/13406066.html