linux中查看和修改文件时间的命令

  linux下文件时间主要有下面三种:
 
  1.1 modification time(mtime)
 
  文件修改时间,即文件内容的修改时,更新这个时间,不包括文件权限和属性的修改,使用ls -l查看,默认显示时间为mtime.
 
  $ ls -l uconv.h
  -rw-rw-r--  1 work work 1808 Jul 23  2013 uconv.h
  1.2 status time(ctime)
  文件状态status的修改时间,如文件的权限和属性修改时更新这个时间,使用 ls --time=ctime 查看.
 
  $ ls -l --time=ctime uconv.h  
  -rw-rw-r--  1 work work 1808 Jul 23  2013 uconv.h
  1、stat查看文件时间
 
  [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:02:34.000000000 +0800
  Modify: 2011-11-29 16:03:06.000000000 +0800
  Change: 2011-11-29 16:03:08.000000000 +0800
  说明:Access访问时间,Modify修改时间,Change状态改变时间,可以stat *查看这个目录所有文件的状态,而我们想要查看某文件的三个时间中的具体某个时间,并以年月日时分秒的格式保存,我们可以使用下面的命令:
 
  [root@web10 ~]# stat 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}'
  20111129160306 --phpfensi.com
  1.3 access time(atime)
  文件访问时间,当文件内容被获取时,更新这个时间,使用 ls --time=actime 查看.
 
  $ ls -l --time=atime uconv.h
 
  -rw-rw-r--  1 work work 1808 Dec 12  2013 uconv.h
 
  相应的通过ls 查看时也有三个时间:
 
  • modification time(mtime,修改时间):当该文件的“内容数据”更改时,就会更新这个时间。内容数据指的是文件的内容,而不是文件的属性。
 
  • status time(ctime,状态时间):当该文件的”状态(status)”改变时,就会更新这个时间,举例来说,更改了权限与属性,就会更新这个时间。
 
  • access time(atime,存取时间):当“取用文件内容”时,就会更新这个读取时间。举例来说,使用cat去读取 ~/.bashrc,就会更新atime了。
 
  [root@web10 ~]# ls -l --time=ctime install.log
  -rw-r--r-- 1 root root 33386 2011-11-29 install.log
  [root@web10 ~]# ls -l --time=atime install.log
  -rw-r--r-- 1 root root 33386 07-13 16:02 install.log
  注意:ls参数里没有--mtime这个参数,因为我们默认通过ls -l查看到的时间就是mtime 。
 
  2.修改文件的时间
 
  如果需要修改上述三个时间,使用touch命令来修改,touch filename,如果文件不存在,则新建一个文件.
 
  $ touch --help
  Usage: touch [OPTION]... FILE...
  Update the access and modification times of each FILE to the current time.
    -a                     change only the access time
                           修改访问时间
    -c, --no-create        do not create any files
                           修改文件三个时间,不存在则不创建
    -d, --date=STRING      parse STRING and use it instead of current time
                           指定时间代替当前时间
    -f                     (ignored)
    -m                     change only the modification time
                           修改mtime
    -r, --reference=FILE   use this file's times instead of current time
    -t STAMP               use [[CC]YY]MMDDhhmm[.ss] instead of current time
                           指定修改时间
  例如:
 
  $ touch -d "2 days ago" uconv.h
  $ ll uconv.h ; ll --time=atime uconv.h ; ll --time=ctime uconv.h ;
  -rw-rw-r--  1 work work 1808 Jun 13 18:17 uconv.h
  -rw-rw-r--  1 work work 1808 Jun 13 18:17 uconv.h
  -rw-rw-r--  1 work work 1808 Jun 15 18:17 uconv.h
  将mtime和atime修改为两天前,ctime没变.
 
  $ touch -t 201406142020 uconv.h   
  $ ll uconv.h ; ll --time=atime uconv.h ; ll --time=ctime uconv.h ;
  -rw-rw-r--  1 work work 1808 Jun 14 20:20 uconv.h
  -rw-rw-r--  1 work work 1808 Jun 14 20:20 uconv.h
  -rw-rw-r--  1 work work 1808 Jun 15 18:23 uconv.h
  atime和mtime都变了,但是ctime变成了当前时间,使用cp命令,-a保持原属性.
 
  $ cp -a uconv.h uconv.h1
  $ ll uconv.h1 ; ll --time=atime uconv.h1 ; ll --time=ctime uconv.h1 ;
  -rw-rw-r--  1 work work 1808 Jun 14 20:20 uconv.h1
  -rw-rw-r--  1 work work 1808 Jun 15 18:25 uconv.h1
  -rw-rw-r--  1 work work 1808 Jun 15 18:27 uconv.h1
  mtime和atime都保持原文件不变,但是ctime变成当前时间.
 
  注:如果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命令 文件名.
 
 

dawei

【声明】:站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。