touch命令
touch命令有两个功能:一是用于把已存在文件的时间标签更新为系统当前的时间(默认方式),它们的数据将原封不动地保留下来;二是用来创建新的空文件。
语法
touch(选项)(参数)
ASP站长网最常用用法:touch fileA
- 如果fileA存在,使用touch指令可更改这个文件或目录的日期时间,包括存取时间和更改时间;
- 如果fileA不存在,touch指令会在当前目录下新建一个空白文件fileA。
参数
参数 解释
-a 只更改存取时间。
-c 不建立任何文档。
-d 使用指定的日期时间,而非现在的时间。 [[CC]YY]MMDD text
-f 此参数将忽略不予处理,仅负责解决BSD版本touch指令的兼容性问题。
-m 只更改变动时间。
-r 把指定文档或目录的日期时间,统统设成和参考文档或目录的日期时间相同。
-t 使用指定的日期时间,而非现在的时间[CC[YY]MMDDhhmm[.SS]
--help 在线帮助
--version 显示版本信息。
使用范例:
实例一:创建不存在的文件
touch test1.txt test2.txt
[root@Linuxidc tmp]# touch test1.txt test2.txt
[root@Linuxidc tmp]# ll
total 0
-rw-r--r-- 1 root root 0 Nov 30 14:54 test1.txt
-rw-r--r-- 1 root root 0 Nov 30 14:54 test2.txt
实例二:如果test3不存在,则不创建文件
touch -c test3.txt
[root@Linuxidc tmp]# touch -c test3.txt
[root@Linuxidc tmp]# ll
total 0
-rw-r--r-- 1 root root 0 Nov 30 14:54 test1.txt
-rw-r--r-- 1 root root 0 Nov 30 14:54 test2.txt
实例三:更新test1.txt的时间和test2.txt时间戳相同
touch touch test1.txt test3.txt
[root@Linuxidc tmp]# ll test1.txt test3.txt
-rw-r--r-- 1 root root 0 Nov 30 14:54 test1.txt
-rw-r--r-- 1 root root 0 Nov 30 14:58 test3.txt
[root@Linuxidc tmp]# touch test1.txt test3.txt
[root@Linuxidc tmp]# ll test1.txt test3.txt
-rw-r--r-- 1 root root 0 Nov 30 14:58 test1.txt
-rw-r--r-- 1 root root 0 Nov 30 14:58 test3.txt
[root@Linuxidc tmp]#
实例四:设定文件的时间戳
touch -t 201808080808 test1.txt
[root@Linuxidc tmp]# ll test1.txt
-rw-r--r-- 1 root root 0 Nov 30 14:58 test1.txt
[root@Linuxidc tmp]# touch -t 201808080808 test1.txt
[root@Linuxidc tmp]# stat test1.txt
File: 'test1.txt'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 803h/2051d Inode: 11041 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2018-08-08 08:08:00.000000000 +0800
Modify: 2018-08-08 08:08:00.000000000 +0800
Change: 2019-05-30 15:01:03.959402702 +0800
Birth: -
实例五:将后面的时间改成前面的时间,将test2.txt的时间改为test1.txt的时间。
touch -r test1.txt test2.txt
[root@Linuxidc tmp]# ll
总用量 0
-rw-r--r-- 1 root root 0 8月 8 2018 test1.txt
-rw-r--r-- 1 root root 0 11月 30 14:54 test2.txt
-rw-r--r-- 1 root root 0 11月 30 14:58 test3.txt
[root@Linuxidc tmp]# touch -r test1.txt test2.txt
[root@Linuxidc tmp]# ll
总用量 0
-rw-r--r-- 1 root root 0 8月 8 2018 test1.txt
-rw-r--r-- 1 root root 0 8月 8 2018 test2.txt
-rw-r--r-- 1 root root 0 11月 30 14:58 test3.txt
实例六:把所以的.txt文件修改到2013年10月13日的时间。操作命令:
touch -d "10/13/2013" *.txt
[root@Linuxidc tmp]# touch -d 20161013 *.txt
[root@Linuxidc tmp]# ll
总用量 0
-rw-r--r-- 1 root root 0 10月 13 00:00 test1.txt
-rw-r--r-- 1 root root 0 10月 13 00:00 test2.txt
-rw-r--r-- 1 root root 0 10月 13 00:00 test3.txt
另外也可以单独修改时间或者月份,如下:
以使用 am, pm 或是 24 小时的格式,日期可以使用其他格式如 6 May 2000 。
- touch -d "6:03pm" file
- touch -d "05/06/2000" file
- touch -d "6:03pm 05/06/2000" file
实例七: 不带任何选项下运行 touch
最简单的使用 touch 命令。只需键入:$ touch file_name
请观察下面的一张截图。
[root@Linuxidc tmp]# ll
总用量 0
-rw-r--r-- 1 root root 0 10月 13 00:00 test1.txt
[root@Linuxidc tmp]# touch test1.txt
[root@Linuxidc tmp]# ll
总用量 0
-rw-r--r-- 1 root root 0 11月 30 15:23 test1.txt
test1.txt原来的时间戳是 00:00在我们使用 touch 命令后,它改变为了 15:23。由此看出,默认情况下,touch 会将文件的时间戳改为当前时间。
大型站长资讯类网站! https://www.0792zz.cn