Touch命令能够用来修改文件访问或修改的时间戳。实际上,它更常仅仅用于快速的建立一个空白文件。node
这篇文章展现了一些很是简单和快速的实例,使用Touch命令来修改时间戳和建立文件。linux
1.建立空白文件ide
为了使用touch命令简单的建立一个空白文件,使用如下语法:code
# touch abc.txt
若是文件已经存在,它的访问时间将会被更新。字符串
2.用Touch建立多个文件it
为了建立多个文件,指定它们的名字并经过空格分隔。io
#touch abc.txt cde.txt xyz.txt
3 建立大量文件object
若是出于某些缘由你但愿建立大量文件,而后像这样的命令将很是有帮助。file
// Create files with names A to Z # touch {A..Z} // Create files with names 1 to 20 # touch {1..20} //Create files with extension # touch {1..1000}.txt // Create 10k files # touch {1..10}{1..1000}
而后用ls命令查看全部的文件已经被建立了语法
4.避免建立新文件
若是你只想要更新已经存在文件的访问时间,而不建立它,使用'-c'选项。若是文件存在,touch命令将更新访问时间,不然将什么也不作。
# touch -c hello.txt
5.改变文件的访问时间 -'a'选项
使用-a选项和文件名来只改变文件的访问时间
# touch -a abc.txt
使用stat命令检查访问时间
# stat a.txt File: ‘a.txt’ Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 803h/2051d Inode: 35864114 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Context: unconfined_u:object_r:admin_home_t:s0 Access: 2018-11-10 21:32:12.581976147 +0800 Modify: 2018-11-10 21:32:12.581976147 +0800 Change: 2018-11-10 21:32:12.581976147 +0800 Birth: -
6.改变修改时间 '-m'选项
使用'-m'选项改变文件的修改时间
[root@lanquark ~]# touch -m a.txt [root@lanquark ~]# stat a.txt File: ‘a.txt’ Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 803h/2051d Inode: 35864114 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Context: unconfined_u:object_r:admin_home_t:s0 Access: 2018-11-10 21:32:12.581976147 +0800 Modify: 2018-11-10 21:33:52.703978421 +0800 Change: 2018-11-10 21:33:52.703978421 +0800 Birth: -
使用通配符改变多个文件的修改时间
# touch -m *.txt
7.同时改变访问和修改时间
使用a和m选项一块儿修改访问和修改时间
# touch -am a.txt [root@lanquark ~]# touch -am a.txt [root@lanquark ~]# stat a.txt File: ‘a.txt’ Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 803h/2051d Inode: 35864114 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Context: unconfined_u:object_r:admin_home_t:s0 Access: 2018-11-10 21:37:28.352983318 +0800 Modify: 2018-11-10 21:37:28.352983318 +0800 Change: 2018-11-10 21:37:28.352983318 +0800 Birth: -
8.设定一个明确的访问或修改时间
使用t选项设定访问或修改时间为一个明确的日期时间,明确的日期时间格式为[[CC]YY]MMDDhhmm[.ss]
# touch -c -t 1811101015 a.txt or # touch -c -t 201811101015 a.txt
注意-若是忽略了c选项,若是文件不存在,一个新的指定了日期时间的文件将被建立。
9.以其余文件的时间戳做为参考
# touch -r ref.txt abc.txt
上面的命令将设定abc.txt的访问或修改时间为ref.txt的访问和修改时间
10.指定时期时间为字符串
除了t选项以外,还有另外一个选项'-d', 它接受通常人类可读格式的日期时间。
下面的示例仅仅提供了日期,时间自动的设置为00:00
# touch -c -d '14 Mar' abc.txt
或者只提供时间,将设定为当前的日期
# touch -d '14:24' abc.txt
想了解更多,经过'man touch'查看man帮助信息
原文: https://www.binarytides.com/linux-touch-command/
2018-11-10 by Kennminn