Linux命令touch——如何修改Linux文件的时间

今天因为要写一个下的定时任务,就是删除三个月前的文件,写好了,需要测试下。但是苦于当前的的系统找不到三个月前的文件。那就想到两种:1.修改os的时间,把系统时间调到三个月以后。这是一个办法。2.修改文件本身的建立时间。前面一个办法似乎见过的,后面的,只是此时想了,但是还不知道怎么办。那就尝试第二种办法。去网上搜了下,知道touch命令可以搞定(最早知道touch,是因为需要它来帮忙建立一个新的file,O(∩_∩)O~没想到它的用处还蛮多的嘛)。记录了下来。

我特意查看了下touch的下的帮助,确实可以使用参数d来实现,帮助里是这样描述的:

-d, –date=STRING

parse STRING and use it instead of current time

udast

这完全符合我的要求。
先把touch的参数说明贴在这里:
命令参数:
-a  或–time=atime或–time=access或–time=use  只更改存取时间。

-c  或–no-create  不建立任何文档。

-d  使用指定的日期时间,而非现在的时间。

-f  此参数将忽略不予处理,仅负责解决BSD版本touch指令的兼容性

-m  或–time=mtime或–time=modify  只更改变动时间。

-r  把指定文档或目录的日期时间,统统设成和参考文档或目录的日期时间相同。

-t  使用指定的日期时间,而非现在的时间。

–no-create 不会建立新档案。 

–help 列出指令格式。 

–version 列出版本讯息。
此处就使用简单的修改文件时间——————
实例:将我系统里某些文件修改为三个月前的时间:
 当前系统文件信息如下:

[root@linuxidc test]# ls -l
 total 28
 drwxr-xr-x 2 root  root    4096 Jan  9 15:47 1333
 -rwxrwxr-x 1 oinstall 8143 Jan  9 14:41 3.sh
 drwxr-xr-x 2 root  root    4096 Jan  9 15:49 444
 -rw-r–r– 1 root  root        0 Jan  9 14:41 4.sh
 -rwxr-xr-x 1 root  root    8143 Jan  9 14:44 77.sh
 -rwxrwxr-x 1 root  root      54 Jan  9 16:03 find.sh
我想把所以的.sh文件修改到三个月前(2013年10月13)的时间。操作命令:
[root@linuxidc test]# touch -d “10/13/2013” *.sh
结果是:
[root@linuxidc test]# ls -l
 total 28
 drwxr-xr-x 2 root  root    4096 Jan  9 15:47 1333
 -rwxrwxr-x 1 oracle oinstall 8143 Oct 13 00:00 3.sh
 drwxr-xr-x 2 root  root    4096 Jan  9 15:49 444
 -rw-r–r– 1 root  root        0 Oct 13 00:00 4.sh
 -rwxr-xr-x 1 root  root    8143 Oct 13 00:00 77.sh
 -rwxrwxr-x 1 root  root      54 Oct 13 00:00 find.sh

另外也可以单独修改时间或者月份,如下
以使用 am, pm 或是 24 小时的格式,日期可以使用其他格式如 6 May 2000 。
touch -d “6:03pm” file 

touch -d “05/06/2000” file 

touch -d “6:03pm 05/06/2000” file

转载自:https://www.linuxidc.com/Linux/2015-11/124813.htm

声明: 除非转自他站(如有侵权,请联系处理)外,本文采用 BY-NC-SA 协议进行授权 | 智乐兔
转载请注明:转自《Linux命令touch——如何修改Linux文件的时间
本文地址:https://www.zhiletu.com/archives-5139.html
关注公众号:智乐兔

赞赏

wechat pay微信赞赏alipay pay支付宝赞赏

上一篇
下一篇

相关文章

在线留言

你必须 登录后 才能留言!

在线客服
在线客服 X

售前: 点击这里给我发消息
售后: 点击这里给我发消息

智乐兔官微