温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Linux下touch命令怎么用

发布时间:2021-10-27 11:50:11 来源:亿速云 阅读:185 作者:小新 栏目:系统运维

这篇文章给大家分享的是有关Linux下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下touch命令怎么用”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI