linux touch命令: 修改文件时间属性
介绍:touch命令用来修改文件的访问时间、修改时间。如果没有指定时间,则将文件时间属性改为当前时间。当指定文件不存在,则touch命令变为创建该文件。
语法:touch [-acm] [-dSTRING-time] [-r reference-file] [-t [[CC]YY]MMDDhhmm[.ss]]
选项介绍:-a: 只修改访问时间;
-c: 如果指定文件不存在,则不创建文件;
-dSTRING-time: 用字符串格式的时间来指定时间属性的修改值;
-m: 只修改指定文件的修改时间;
-r refernce-file: 将指定文件的时间属性改为与reference-file时间属性相同的值;
-t [[CC]YY]MMDDhhmm[.ss]: 用[[CC]YY]MMDDhhmm[.ss]这种时间格式来指定时间属性的修改值;
执行范例:03 | $ touch text #指定文件不存在,创建文件text |
04 | $stat text # stat命令可能查看文件的时间属性。 |
06 | Size: 0 Blocks: 8IO Block: 4096 regular empty file |
07 | Device: 831h/2097dInode: 211271681 Links: 1 |
08 | Access: (0644/-rw-r--r--)Uid: (514/xxxx)Gid: (100/ users ) |
09 | Access: 2010-03-01 22:03:30.000000000 +0800 |
10 | Modify: 2010-03-01 22:03:30.000000000 +0800 |
11 | Change: 2010-03-01 22:03:30.000000000 +0800 |
13 | $stat text # 发现文件的访问时间(Access)改变 |
15 | Size: 0 Blocks: 8IO Block: 4096 regular empty file |
16 | Device: 831h/2097dInode: 211271681 Links: 1 |
17 | Access: (0644/-rw-r--r--)Uid: (514/xxxx)Gid: (100/ users ) |
18 | Access: 2010-03-01 22:04:08.000000000 +0800 |
19 | Modify: 2010-03-01 22:03:30.000000000 +0800 |
20 | Change: 2010-03-01 22:03:30.000000000 +0800 |
21 | $ echo "hello world" > text # 相当于写文件 |
22 | $stat text # 发现文件的修改时间(Modify)改变 |
24 | Size: 12Blocks: 16 IO Block: 4096 regular file |
25 | Device: 831h/2097dInode: 211271681 Links: 1 |
26 | Access: (0644/-rw-r--r--)Uid: (514/xxxx)Gid: (100/ users ) |
27 | Access: 2010-03-01 22:04:08.000000000 +0800 |
28 | Modify: 2010-03-01 22:04:58.000000000 +0800 |
29 | Change: 2010-03-01 22:04:58.000000000 +0800 |
30 | $ touch text #将文件的访问时间、修改时间改为当前时间 |
33 | Size: 12Blocks: 16 IO Block: 4096 regular file |
34 | Device: 831h/2097dInode: 211271681 Links: 1 |
35 | Access: (0644/-rw-r--r--)Uid: (514/xxxx)Gid: (100/ users ) |
36 | Access: 2010-03-01 22:05:41.000000000 +0800 |
37 | Modify: 2010-03-01 22:05:41.000000000 +0800 |
38 | Change: 2010-03-01 22:05:41.000000000 +0800 |
39 | $ touch -a text # 只修改文件的访问时间 |
42 | Size: 12Blocks: 16 IO Block: 4096 regular file |
43 | Device: 831h/2097dInode: 211271681 Links: 1 |
44 | Access: (0644/-rw-r--r--)Uid: (514/xxxx)Gid: (100/ users ) |
45 | Access: 2010-03-01 22:05:51.000000000 +0800 |
46 | Modify: 2010-03-01 22:05:41.000000000 +0800 |
47 | Change: 2010-03-01 22:05:51.000000000 +0800 |
48 | $ touch -d "01/23/2000" text #将文件时间属性改为2000年1月23号 |
51 | Size: 12Blocks: 16 IO Block: 4096 regular file |
52 | Device: 831h/2097dInode: 211271681 Links: 1 |
53 | Access: (0644/-rw-r--r--)Uid: (514/xxxx)Gid: (100/ users ) |
54 | Access: 2000-01-23 00:00:00.000000000 +0800 |
55 | Modify: 2000-01-23 00:00:00.000000000 +0800 |
56 | Change: 2010-03-01 22:07:05.000000000 +0800 |
57 | $ touch -d "1:04am 01/24/2000" text #将文件时间属性改为2000年1月23号1:04am |
60 | Size: 12Blocks: 16 IO Block: 4096 regular file |
61 | Device: 831h/2097dInode: 211271681 Links: 1 |
62 | Access: (0644/-rw-r--r--)Uid: (514/xxxx)Gid: (100/ users ) |
63 | Access: 2000-01-24 01:04:00.000000000 +0800 |
64 | Modify: 2000-01-24 01:04:00.000000000 +0800 |
65 | Change: 2010-03-01 22:07:48.000000000 +0800 |
66 | $ touch -t 09010059.30 text #将文件时间属性改为9月1号0点59分30秒 |
69 | Size: 12Blocks: 16 IO Block: 4096 regular file |
70 | Device: 831h/2097dInode: 211271681 Links: 1 |
71 | Access: (0644/-rw-r--r--)Uid: (514/xxxx)Gid: (100/ users ) |
72 | Access: 2010-09-01 00:59:30.000000000 +0800 |
73 | Modify: 2010-09-01 00:59:30.000000000 +0800 |
74 | Change: 2010-03-01 22:09:27.000000000 +0800 |
75 | $ touch -r /etc/ passwd text #将文件的时间属性改为与/etc/passwd文件的时间属性相同的值 |
78 | Size: 12Blocks: 16 IO Block: 4096 regular file |
79 | Device: 831h/2097dInode: 211271681 Links: 1 |
80 | Access: (0644/-rw-r--r--)Uid: (514/xxxx)Gid: (100/ users ) |
81 | Access: 2010-03-01 22:22:20.000000000 +0800 |
82 | Modify: 2010-01-22 21:02:50.000000000 +0800 |
83 | Change: 2010-03-01 22:22:35.000000000 +0800 |
扩展阅读:什么是文件的时间属性?
在linux下,一个文件有三种时间属性: 访问时间、修改时间、状态改动时间。
访问时间: 记录文件最近被阅读的时间(vim,cat,more等命令都能修改该值)。
修改时间: 记录文件最近被修改的时间(ls -l命令显示的时间即为该时间)。
状态改动时间: 记录该文件的inode节点被修改的时间。touch命令除了-d和-t选项外都会改变该时间。而且chmod,chown等命令也能改变该值。
如何查看文件的时间属性?
用stat命令可以查看文件的时间属性,见上例。
另外ls命令也可以查看文件的时间属性:
ls -l file : 查看文件修改时间
ls -lc file: 查看文件状态改动时间
ls -lu file: 查看文件访问时间
touch命令-t选项的时间格式[[CC]YY]MMDDhhmm[.ss]说明:
CC表示世纪
YY表示年
MM表示月
DD表示日
hh表示小时
mm表示分钟
ss表示秒
201001311200.34
CCYYMMDDhhmm ss
表示时间为2010年1月31日12点0分34秒