手机版
你好,游客 登录 注册
背景:
阅读新闻

Linux文件时间的查看和修改touch

[日期:2014-06-17] 来源:Linux社区  作者:yonggang [字体: ]

1. 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.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

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变成当前时间

本文永久更新链接地址http://www.linuxidc.com/Linux/2014-06/103332.htm

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款