zoukankan      html  css  js  c++  java
  • Linux下查看和修改文件时间

    一 、查看文件的时间及相关命令

    1、stat 查看文件时间

    [root@linux-node1 ~]# stat  lnmp-install.log 
      File: `lnmp-install.log'
      Size: 2228418   	Blocks: 4368       IO Block: 4096   regular file
    Device: fd00h/64768d	Inode: 1970147     Links: 1
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2019-04-18 16:03:58.903725603 +0800
    Modify: 2016-05-15 15:02:15.836183263 +0800
    Change: 2016-05-15 15:02:15.836183263 +0800

    说明:Access:访问时间   Modify:修改时间  Change:改变时间 ,可以使用stat * 查看这个目录所有文件的状态。

    而我们想要查看某文件的三个时间中的具体某个时间,并以年月日时分秒的格式保存。我们可以使用下面的命令:

    [root@linux-node1 ~]# stat lnmp-install.log  |grep  -i Access | awk -F. '{print $1}' | awk '{print $2$3}'| awk -F- '{print $1$2$3}' | awk -F: '{print $1$2$3}' |grep -v 'rwr'
    20190418160358
    [root@linux-node1 ~]# stat lnmp-install.log  |grep  -i Modify | awk -F. '{print $1}' | awk '{print $2$3}'| awk -F- '{print $1$2$3}' | awk -F: '{print $1$2$3}'
    20160515150215
    [root@linux-node1 ~]# stat lnmp-install.log  |grep  -i Change | awk -F. '{print $1}' | awk '{print $2$3}'| awk -F- '{print $1$2$3}' | awk -F: '{print $1$2$3}'
    20160515150215  

    2、ls查看文件时间

    相应的通过ls 查看时也有三个时间:

    • modification time(mtime,修改时间):当该文件的“内容数据”更改时,就会更新这个时间。内容数据指的是文件的内容,而不是文件的属性。 
    • status time(ctime,状态时间):当该文件的”状态(status)”改变时,就会更新这个时间,举例来说,更改了权限与属性,就会更新这个时间。 
    • access time(atime,存取时间):当“取用文件内容”时,就会更新这个读取时间。举例来说,使用cat去读取 ~/.bashrc,就会更新atime了。
    [root@linux-node1 ~]# ls -l --time=ctime lnmp-install.log 
    -rw-r--r--. 1 root root 2228418 May 15  2016 lnmp-install.log
    [root@linux-node1 ~]# ls -l --time=atime lnmp-install.log 
    -rw-r--r--. 1 root root 2228418 Apr 18 16:03 lnmp-install.log
    

    注意:ls 参数里没有--time=mtime 这个参数,因为我们默认通过ls -l 查看到的时间就是mtime .

    二、修改文件时间

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

    -a : 仅修改access time

    -c  :仅修改时间,而不建立文件

    -d  :后面可以接日期,也可以使用 --date="日期或时间" 

    -m :仅修改mtime

    -t  :后面可以接时间,格式为 [YYMMDDhhmm]

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

    [root@linux-node1 ~]# ll 
    -rw-r--r--. 1 root root     9214 Apr 24  2016 install.log
    -rw-r--r--. 1 root root     3091 Apr 24  2016 install.log.syslog
    -rw-r--r--. 1 root root  2228418 May 15  2016 lnmp-install.log
    [root@linux-node1 ~]# touch install.log
    [root@linux-node1 ~]# stat install.log
      File: `install.log'
      Size: 9214      	Blocks: 24         IO Block: 4096   regular file
    Device: fd00h/64768d	Inode: 1962242     Links: 1
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2019-04-18 16:20:22.729724898 +0800
    Modify: 2019-04-18 16:20:22.729724898 +0800
    Change: 2019-04-18 16:20:22.729724898 +0800
    

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

    [root@linux-node1 ~]# ls -l --time=ctime install.log
    -rw-r--r--. 1 root root 9214 Apr 18 16:20 install.log
    [root@linux-node1 ~]# ls -l --time=atime install.log
    -rw-r--r--. 1 root root 9214 Apr 18 16:20 install.log
    [root@linux-node1 ~]# ls -l  install.log
    -rw-r--r--. 1 root root 9214 Apr 18 16:20 install.log
    

    利用touch修改文件时间:

    1. 同时变更文件的修改时间和访问时间
    [root@linux-node1 ~]# touch -d "2018-04-18 08:00:00" install.log
    [root@linux-node1 ~]# stat install.log
      File: `install.log'
      Size: 9214      	Blocks: 24         IO Block: 4096   regular file
    Device: fd00h/64768d	Inode: 1962242     Links: 1
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2018-04-18 08:00:00.000000000 +0800
    Modify: 2018-04-18 08:00:00.000000000 +0800
    Change: 2019-04-18 16:32:35.947725358 +0800
    2. 只变更文件的修改时间
    [root@linux-node1 ~]# touch -m -d "2018-05-20 08:00:00" install.log
    [root@linux-node1 ~]# stat install.log
      File: `install.log'
      Size: 9214      	Blocks: 24         IO Block: 4096   regular file
    Device: fd00h/64768d	Inode: 1962242     Links: 1
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2018-04-18 08:00:00.000000000 +0800
    Modify: 2018-05-20 08:00:00.000000000 +0800
    Change: 2019-04-18 16:36:06.617725402 +0800
    
    3. 只变更文件的访问时间
    [root@linux-node1 ~]# touch -a -d "2017-05-10 09:00:00" install.log
    [root@linux-node1 ~]# stat install.log
      File: `install.log'
      Size: 9214      	Blocks: 24         IO Block: 4096   regular file
    Device: fd00h/64768d	Inode: 1962242     Links: 1
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2017-05-10 09:00:00.000000000 +0800
    Modify: 2018-05-20 08:00:00.000000000 +0800
    Change: 2019-04-18 16:38:09.851725390 +0800
    

    将一个文件的时间修改为同另一个文件的时间相同

    [root@linux-node1 ~]# ls -l anaconda-ks.cfg 
    -rw-------. 1 root root 1097 Apr 24 2016 anaconda-ks.cfg
    
    [root@linux-node1 ~]# touch -acmr anaconda-ks.cfg  install.log
    [root@linux-node1 ~]# stat install.log
      File: `install.log'
      Size: 9214      	Blocks: 24         IO Block: 4096   regular file
    Device: fd00h/64768d	Inode: 1962242     Links: 1
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2016-04-24 22:21:38.363999825 +0800
    Modify: 2016-04-24 22:21:39.114999827 +0800
    Change: 2019-04-18 16:40:09.464725644 +0800
    

    ## 将install.log文件的时间修改成和anaconda-ks.cfg ##

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

    [root@linux-node1 ~]# stat install.log
      File: `install.log'
      Size: 9214      	Blocks: 24         IO Block: 4096   regular file
    Device: fd00h/64768d	Inode: 1962242     Links: 1
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2016-04-24 22:21:38.363999825 +0800
    Modify: 2016-04-24 22:21:39.114999827 +0800
    Change: 2019-04-18 16:40:09.464725644 +0800
    [root@linux-node1 ~]# touch -d "3 days ago" install.log;ls -l install.log
    -rw-r--r--. 1 root root 9214 Apr 15 16:47 install.log
    

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

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

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

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

  • 相关阅读:
    机器学习sklearn(66):算法实例(二十三)SVM(一)概述
    机器学习sklearn(65):算法实例(二十二)聚类(五)KMeans (四) 总结
    机器学习sklearn(64):算法实例(二十一)聚类(四)KMeans (三) 案例:聚类算法用于降维,KMeans的矢量量化应用
    机器学习sklearn(63):算法实例(二十)聚类(三)KMeans (二) sklearn.cluster.KMeans
    机器学习sklearn(62):算法实例(十九)聚类(二)KMeans
    机器学习sklearn(61):算法实例(十八)聚类(一)概述
    机器学习sklearn(60):算法实例(十七)分类(十)逻辑回归(五)附录
    机器学习sklearn(59):算法实例(十六)分类(九)逻辑回归(四)实例 用逻辑回归制作评分卡
    机器学习sklearn(58):算法实例(十五)分类(八)逻辑回归(三)linear_model.LogisticRegression(二) 重要参数
    机器学习sklearn(57):算法实例(十四)分类(七)逻辑回归(二)linear_model.LogisticRegression(一) 重要参数
  • 原文地址:https://www.cnblogs.com/caoshousong/p/10730178.html
Copyright © 2011-2022 走看看