linux中的文件都有三个时间,分别是atime/mtime/ctime,如果不知道的话,请先看看《Linux中的atime/mtime/ctime详解》补下课。touch指令的作用就是用于改变文件的时间戳,touch命令的语法格式如下:
touch [选项]... filename... 选项与参数: -a : 仅修订 atime; -c : 仅修改档案的时间,若该档案不存在则不建立新档案; -d : 后面可以接欲修订的日期而不用目前的日期,也可以使用 --date="日期或时间" -m : 仅修改 mtime ; -t : 后面可以接欲修订的时间而不用目前的时间,格式为[YYMMDDhhmm] -r : 把指定文档或目录的日期时间,统统设成和参考文档或目录的日期时间相同 ...
示例演示
# touch a.txt # stat a.txt File: `a.txt' Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: ca01h/51713d Inode: 1050217 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2014-03-18 09:30:49.965240705 +0800 Modify: 2014-03-18 09:30:49.965240705 +0800 Change: 2014-03-18 09:30:49.965240705 +0800 Birth: -
先通过touch创建a.txt,如果已经存在则会修改文件的atime/mtime/ctime。
# touch a.txt # stat a.txt File: `a.txt' Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: ca01h/51713d Inode: 1050217 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2014-03-18 09:31:43.584234285 +0800 Modify: 2014-03-18 09:31:43.584234285 +0800 Change: 2014-03-18 09:31:43.584234285 +0800 Birth: -
再touch a.txt,发现atime/mtime/ctime都发生变化了。
# touch -a a.txt # stat a.txt File: `a.txt' Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: ca01h/51713d Inode: 1050217 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2014-03-18 09:32:01.459898755 +0800 Modify: 2014-03-18 09:31:43.584234285 +0800 Change: 2014-03-18 09:32:01.459898755 +0800 Birth: -
使用touch -a a.txt,只有atime和ctime发生变化了,mtime保存不变。
# touch -m a.txt
# stat a.txt
File: `a.txt'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: ca01h/51713d Inode: 1050217 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2014-03-18 09:32:01.459898755 +0800
Modify: 2014-03-18 09:32:22.591502109 +0800
Change: 2014-03-18 09:32:22.591502109 +0800
Birth: -
使用touch -m a.txt,只有mtime和ctime发生变化了,atime保存不变。
任何情况下,使用touch指令,文件的ctime都会发生改变。以上经过touch指令后,atime/mtime/ctime的值都是当前时间点的值,通过touch指令还可以随意指定时间戳。
# touch -d "2 days ago" b.txt && stat b.txt File: `b.txt' Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: ca01h/51713d Inode: 1050218 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2014-03-16 09:38:49.261034132 +0800 Modify: 2014-03-16 09:38:49.261034132 +0800 Change: 2014-03-18 09:38:49.256243241 +0800 Birth: -
touch -d 通过指定一个表示时间的字符串来当做当前时间,这个格式比较自由,只要是能表示时间的都可以,例如”2014-02-28 16:21:42″、”next Thursday”、”2 days ago”等。
# touch -t 201401011212.30 c.txt && stat c.txtFile: `c.txt' Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: ca01h/51713d Inode: 1050219 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2014-01-01 12:12:30.000000000 +0800 Modify: 2014-01-01 12:12:30.000000000 +0800 Change: 2014-03-18 09:39:45.983178168 +0800 Birth: - # touch -t 01011212 d.txt && stat d.txt File: `d.txt' Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: ca01h/51713d Inode: 1050220 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2014-01-01 12:12:00.000000000 +0800 Modify: 2014-01-01 12:12:00.000000000 +0800 Change: 2014-03-18 09:41:59.092678768 +0800 Birth: -
touch -t 指令后面的时间戳格式为:[[CC]YY]MMDDhhmm[.ss]
同样的,ctime始终是当前时间。