zoukankan      html  css  js  c++  java
  • linux 修改文件的时间属性

    二、修改文件时间

    创建文件我们可以通过touch来创建。同样,我们也可以使用touch来修改文件时间。touch的相关参数如下:

    -a : 仅修改access time。
    -c : 仅修改时间,而不建立文件。
    -d : 后面可以接日期,也可以使用 --date="日期或时间"
    -m : 仅修改mtime。
    -t : 后面可以接时间,格式为 [YYMMDDhhmm]

    注:如果touch后面接一个已经存在的文件,则该文件的3个时间(atime/ctime/mtime)都会更新为当前时间。若该文件不存在,则会主动建立一个新的空文件。

    [root@web10 ~]# touch install.log
    [root@web10 ~]# stat install.log
      File: “install.log”
      Size: 33386           Blocks: 80         IO Block: 4096   一般文件
    Device: fd00h/64768d    Inode: 7692962     Links: 1
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2012-07-13 16:21:50.000000000 +0800
    Modify: 2012-07-13 16:21:50.000000000 +0800
    Change: 2012-07-13 16:21:50.000000000 +0800

    同样,使用ls ,查看到的结果也一样。

    [root@web10 ~]# ls -l --time=ctime install.log
    -rw-r--r-- 1 root root 33386 07-13 16:21 install.log
    [root@web10 ~]# ls -l --time=atime install.log
    -rw-r--r-- 1 root root 33386 07-13 16:21 install.log
    [root@web10 ~]# ls -l install.log
    -rw-r--r-- 1 root root 33386 07-13 16:21 install.log

    下面再看一个和touch不相关的例子:

    [root@web10 ~]# cp /etc/profile .;ll --time=atime profile ;ll --time=ctime profile
    cp:是否覆盖“./profile”? y
    -rw-r--r-- 1 root root 1344 07-13 16:24 profile
    -rw-r--r-- 1 root root 1344 07-13 16:25 profile

    因为我之前运行过这个命令一次,所以会出现覆盖,不过这个覆盖出的好,刚才让我们看到了atime和ctime的时间的差别。

    我们再回到touch利用touch修改文件时间:

    1. 同时修改文件的修改时间和访问时间
    touch -d "2010-05-31 08:10:30" install.log
    2. 只修改文件的修改时间
    touch -m -d "2010-05-31 08:10:30" install.log
    3. 只修改文件的访问时间
    touch -a -d "2010-05-31 08:10:30" install.log

    下面再给一个rootkit木马常用的伎俩。就是把后一个文件的时间修改成和前一个相同。

    touch -acmr /bin/ls /etc/sh.conf

    另外touch还支持像date命令一样参数修改文件时间:

    [root@web10 ~]# touch -d "2 days ago" install.log ; ll install.log
    -rw-r--r-- 1 root root 33386 07-11 16:35 install.log

    最后总结下常用的文件操作与时间的关系:

    1、访问时间,读一次这个文件的内容,这个时间就会更新。比如对这个文件使用more命令。ls、stat命令都不会修改文件的访问时间。

    2、修改时间,对文件内容修改一次,这个时间就会更新。比如:vim后保存文件。ls -l列出的时间就是这个时间。

    3、状态改变时间。通过chmod命令更改一次文件属性,这个时间就会更新。查看文件的详细的状态、准确的修改时间等,可以通过stat命令 文件名

  • 相关阅读:
    [译]在Python中如何使用额enumerate 和 zip 来迭代两个列表和它们的index?
    [译]如何去除Git的unstaged的文件提示“old mode 100755 new mode 100644”?
    [译]在SQL查询中如何映射(替换)查询的结果?
    [总结]《敏捷软件开发: 原则、模式与实践》一次编程实践
    [书摘]《敏捷软件开发: 原则、模式与实践》第一部分:敏捷开发
    [译]Python
    [问题解决]Python locale error: unsupported locale setting
    [持续补充]开发过程中常见bug查找思路
    [译]如何比较同一分支上的不同commit的代码区别?
    [整理]如何切换到远程分支
  • 原文地址:https://www.cnblogs.com/sunshine-long/p/10812994.html
Copyright © 2011-2022 走看看