1.功能:改变文件的时间戳
2.用法:touch [option] 文件
3.参数:
-a 改变访问时间
-c, --no-create 不建立任何文档
-d, --date=STRING 使用指定的日期时间
-m 修改变动时间
-r, --reference=FILE 用其他文件的时间代替指定文档的时间
-t 使用指定的时间戳代替当前的时间,时间戳的格式[[CC]YY]MMDDhhmm[.ss]
--time=WORD WORD是access, atime, or use时,等于 -a
WORD 是modify or mtime时,等于-m
4.例子:
例1:修改文件的访问时间
第一步,首先查看test_1.txt 的访问时间,该文件的访问时间是5月12日。
[root@mycentos test]# stat test_1.txt
File: "test_1.txt"
Size: 16 Blocks: 8 IO Block: 4096 普通文件
Device: fd00h/64768d Inode: 155375 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-05-12 11:01:35.487021175 +0800
Modify: 2016-05-15 16:00:38.202022985 +0800
Change: 2016-05-15 16:00:38.202022985 +0800
第二步,使用cat命令访问文件,并查看文件的访问时间,已经变成了 2016-05-15 16:01:14.687024326 +0800
[root@mycentos test]# cat test_1.txt
today is sunday
[root@mycentos test]# stat test_1.txt
File: "test_1.txt"
Size: 16 Blocks: 8 IO Block: 4096 普通文件
Device: fd00h/64768d Inode: 155375 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-05-15 16:01:14.687024326 +0800
Modify: 2016-05-15 16:00:38.202022985 +0800
Change: 2016-05-15 16:00:38.202022985 +0800
第三步,使用-a命令修改文件的访问时间,文件的修改时间变成了2016-05-15 16:01:35.520022752 +0800
[root@mycentos test]# touch -a test_1.txt
[root@mycentos test]# stat test_1.txt
File: "test_1.txt"
Size: 16 Blocks: 8 IO Block: 4096 普通文件
Device: fd00h/64768d Inode: 155375 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-05-15 16:01:35.520022752 +0800
Modify: 2016-05-15 16:00:38.202022985 +0800
Change: 2016-05-15 16:01:35.520022752 +0800
使用 touch --time中的相关参数修改文件的访问时间
[root@mycentos test]# touch --time=access test_1.txt
[root@mycentos test]# stat test_1.txt
File: "test_1.txt"
Size: 16 Blocks: 8 IO Block: 4096 普通文件
Device: fd00h/64768d Inode: 155375 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-05-15 16:06:47.056021432 +0800
Modify: 2016-05-15 16:00:38.202022985 +0800
Change: 2016-05-15 16:06:47.056021432 +0800
[root@mycentos test]# touch --time=atime test_1.txt
[root@mycentos test]# stat test_1.txt
File: "test_1.txt"
Size: 16 Blocks: 8 IO Block: 4096 普通文件
Device: fd00h/64768d Inode: 155375 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-05-15 16:07:14.829022788 +0800
Modify: 2016-05-15 16:00:38.202022985 +0800
Change: 2016-05-15 16:07:14.829022788 +0800
[root@mycentos test]# touch --time=use test_1.txt
[root@mycentos test]# stat test_1.txt
File: "test_1.txt"
Size: 16 Blocks: 8 IO Block: 4096 普通文件
Device: fd00h/64768d Inode: 155375 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-05-15 16:07:26.770022042 +0800
Modify: 2016-05-15 16:00:38.202022985 +0800
Change: 2016-05-15 16:07:26.770022042 +0800
例2.创建文件
[root@mycentos test]# touch test_sunday.txt
[root@mycentos test]# ls
cd.txt test_1 test_1.txt test_sunday.txt
例3:修改访问时间
[root@mycentos test]# touch -m test_sunday.txt
[root@mycentos test]# stat test_sunday.txt
File: "test_sunday.txt"
Size: 0 Blocks: 0 IO Block: 4096 普通空文件
Device: fd00h/64768d Inode: 155373 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-05-15 16:09:37.651021374 +0800
Modify: 2016-05-15 16:11:25.371022581 +0800
Change: 2016-05-15 16:11:25.371022581 +0800
[root@mycentos test]# touch --time=modify test_sunday.txt
[root@mycentos test]# stat test_sunday.txt
File: "test_sunday.txt"
Size: 0 Blocks: 0 IO Block: 4096 普通空文件
Device: fd00h/64768d Inode: 155373 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-05-15 16:09:37.651021374 +0800
Modify: 2016-05-15 16:11:59.636022320 +0800
Change: 2016-05-15 16:11:59.636022320 +0800
[root@mycentos test]# touch --time=mtime test_sunday.txt
[root@mycentos test]# stat test_sunday.txt
File: "test_sunday.txt"
Size: 0 Blocks: 0 IO Block: 4096 普通空文件
Device: fd00h/64768d Inode: 155373 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-05-15 16:09:37.651021374 +0800
Modify: 2016-05-15 16:12:15.237022718 +0800
Change: 2016-05-15 16:12:15.237022718 +0800
例4 使用指定的日期时间
[root@mycentos test]# touch -d "2016-05-14 15:00:00" test_sunday.txt
[root@mycentos test]# stat test_sunday.txt
File: "test_sunday.txt"
Size: 0 Blocks: 0 IO Block: 4096 普通空文件
Device: fd00h/64768d Inode: 155373 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-05-14 15:00:00.000000000 +0800
Modify: 2016-05-14 15:00:00.000000000 +0800
Change: 2016-05-15 16:16:08.613022886 +0800
[root@mycentos test]# touch --date="2016-05-15 10:00:00" test_sunday.txt
[root@mycentos test]# stat test_sunday.txt
File: "test_sunday.txt"
Size: 0 Blocks: 0 IO Block: 4096 普通空文件
Device: fd00h/64768d Inode: 155373 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-05-15 10:00:00.000000000 +0800
Modify: 2016-05-15 10:00:00.000000000 +0800
Change: 2016-05-15 16:19:00.505027521 +0800
[root@mycentos test]# touch -t "201605151600" test_sunday.txt
[root@mycentos test]# stat test_sunday.txt
File: "test_sunday.txt"
Size: 0 Blocks: 0 IO Block: 4096 普通空文件
Device: fd00h/64768d Inode: 155373 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-05-15 16:00:00.000000000 +0800
Modify: 2016-05-15 16:00:00.000000000 +0800
Change: 2016-05-15 16:22:03.475022936 +0800
例5:使用指定的文件的时间戳
[root@mycentos test]# touch -r test_1.txt test_sunday.txt
[root@mycentos test]# stat test_1.txt
File: "test_1.txt"
Size: 16 Blocks: 8 IO Block: 4096 普通文件
Device: fd00h/64768d Inode: 155375 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-05-15 16:07:26.770022042 +0800
Modify: 2016-05-15 16:00:38.202022985 +0800
Change: 2016-05-15 16:07:26.770022042 +0800
[root@mycentos test]# stat test_sunday.txt
File: "test_sunday.txt"
Size: 0 Blocks: 0 IO Block: 4096 普通空文件
Device: fd00h/64768d Inode: 155373 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-05-15 16:07:26.770022042 +0800
Modify: 2016-05-15 16:00:38.202022985 +0800
Change: 2016-05-15 16:23:56.011022061 +0800
原文地址:http://myhwj.blog.51cto.com/9763975/1773627