Linux touch命令
touch命令的作用是新建空白文件和更新文件的访问(access)和修改(modify)时间戳。
注:modify为文件内容发生改变,不论是access和modify任意发生改变,都会导致change(修改)时间发生改变,因为无论哪个操作都会导致文件的属性(changetime)发生改变。
查看touch的帮助
man touch
touch的使用方法:
touch [OPTION]... FILE...
选项
选项 |
功能 |
-a |
change only the access time 修改文件的访问时间 |
-c |
不创建文件,只修改文件的时间戳 |
-d |
将时间戳修改为指定的时间 |
-m |
修改文件的修改时间 |
-r |
以一个文件为参考修改访问(access)和修改(modify)的时间戳 |
-t |
将时间戳修改为指定的时间时间格式为:[[CC]YY]MMDDhhmm[.ss] |
示例
例1:
更新文件的访问时间戳
[root@nfs-server~]#stat test.txt #使用stat命令查看文件的时间戳
File:`test.txt'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device:803h/2051d Inode: 1399408 Links: 1
Access:(0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access:2017-03-31 16:57:32.000000000 +0800
Modify:2017-03-31 16:57:32.000000000 +0800
Change:2017-03-31 16:57:32.000000000 +0800
[root@nfs-server~]# touch -a test.txt
[root@nfs-server~]# stat test.txt
File:`test.txt'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device:803h/2051d Inode: 1399408 Links: 1
Access:(0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access:2017-03-31 17:00:14.000000000 +0800
Modify:2017-03-31 16:57:32.000000000 +0800
Change:2017-03-31 17:00:14.000000000 +0800
touch 无选项,会同时更新文件的修改时间和访问时间
例2:
同时更新文件的访问时间戳和修改时间戳
[root@nfs-server~]# touch -c test.txt
[root@nfs-server~]# stat test.txt
File: `test.txt'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device:803h/2051d Inode: 1399408 Links: 1
Access:(0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access:2017-03-31 17:06:50.000000000 +0800
Modify:2017-03-31 17:06:50.000000000 +0800
Change:2017-03-31 17:06:50.000000000 +0800
例3:
同时修改文件的访问时间戳和修改时间戳为指定的时间
[root@nfs-server~]# touch -d 20170210 test.txt
[root@nfs-server~]# stat test.txt
File: `test.txt'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 803h/2051d Inode: 1399408 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2017-02-1000:00:00.000000000 +0800
Modify: 2017-02-1000:00:00.000000000 +0800
Change: 2017-03-3117:11:35.000000000 +0800
[root@nfs-server~]# touch -d "10:20pm 02/12/17" test.txt
[root@nfs-server~]# stat test.txt
File: `test.txt'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 803h/2051d Inode: 1399408 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2017-02-1222:20:00.000000000 +0800
Modify: 2017-02-1222:20:00.000000000 +0800
Change: 2017-03-3117:21:03.000000000 +0800
例4:
修改文件的修改时间
[root@nfs-server oldboy]# touch -m test.txt
[root@nfs-server oldboy]# stat test.txt
File: `test.txt'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 803h/2051d Inode: 1399408 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2017-02-1000:00:00.000000000 +0800
Modify: 2017-03-3117:14:06.000000000 +0800
Change: 2017-03-3117:14:06.000000000 +0800
例5:
以一个文件为参考修改访问(access)和修改(modify)的时间戳
[root@nfs-server~]# stat /root/us.sh
File: `/root/us.sh'
Size: 312 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051d Inode: 1399427 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2017-03-3100:05:59.000000000 +0800
Modify: 2017-03-1323:08:56.000000000 +0800
Change: 2017-03-1923:29:21.000000000 +0800
[root@nfs-server~]# touch-r /root/us.sh test.txt
[root@nfs-server~]# stat test.txt
File: `test.txt'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 803h/2051d Inode: 1399408 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2017-03-3100:05:59.000000000 +0800
Modify: 2017-03-1323:08:56.000000000 +0800
Change: 2017-03-3117:15:37.000000000 +0800
例6:
将时间戳修改为指定的时间
[root@nfs-server~]# touch-t 201703101525 test.txt
[root@nfs-server~]# stat test.txt
File: `test.txt'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 803h/2051d Inode: 1399408 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2017-03-1015:25:00.000000000 +0800
Modify: 2017-03-1015:25:00.000000000 +0800
Change: 2017-03-3117:17:12.000000000 +0800
例7:
系统维护期间禁止用户登录,仅限于ssh
touch /etc/nologin