touch 命令详解

touch命令两个功能:

  1. 用于把已存在文件的时间标签更新为系统当前的时间(默认方式),它们的数据将原封不动地保留下来;

  2. 用来创建新的空文件。

语法

touch (选项) (参数)
最常用用法:touch fileA

  • 如果fileA存在,使用touch指令可更改这个文件或目录的日期时间,包括存取时间和更改时间;
  • 如果fileA不存在,touch指令会在当前目录下新建一个空白文件fileA。

参数

在这里插入图片描述

使用范例:

  1. 创建不存在的文件

touch test1.txt test2.txt

[root@MLibra tmp]# touch test1.txt test2.txt
[root@MLibra 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

  1. 如果test3不存在,则不创建文件

touch -c test3.txt

[root@MLibra tmp]# touch -c test3.txt
[root@MLibra 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


  1. 更新test1.txt的时间和test2.txt时间戳相同
    touch touch test1.txt test3.txt
[root@MLibra 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@MLibra tmp]# touch test1.txt test3.txt
[root@MLibra 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@MLibra tmp]#

  1. 设定文件的时间戳
    touch -t 201808080808 test1.txt
[root@MLibra tmp]# ll test1.txt
-rw-r--r-- 1 root root 0 Nov 30 14:58 test1.txt
[root@MLibra tmp]# touch -t 201808080808 test1.txt

[root@MLibra 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: 2016-11-30 15:01:03.959402702 +0800
 Birth: -

  1. 将后面的时间改成前面的时间,将test2.txt的时间改为test1.txt的时间。
    touch -r test1.txt test2.txt
[root@MLibra tmp]# ll
总用量 0
-rw-r--r-- 1 root root 0 88 2018 test1.txt
-rw-r--r-- 1 root root 0 1130 14:54 test2.txt
-rw-r--r-- 1 root root 0 1130 14:58 test3.txt
[root@MLibra tmp]# touch -r test1.txt test2.txt
[root@MLibra tmp]# ll
总用量 0
-rw-r--r-- 1 root root 0 88 2018 test1.txt
-rw-r--r-- 1 root root 0 88 2018 test2.txt
-rw-r--r-- 1 root root 0 1130 14:58 test3.txt

  1. 把所以的.txt文件修改到2013年10月13日的时间。操作命令:
    touch -d "10/13/2013" *.txt
[root@MLibra tmp]# touch -d 20161013 *.txt
[root@MLibra tmp]# ll
总用量 0
-rw-r--r-- 1 root root 0 1013 00:00 test1.txt
-rw-r--r-- 1 root root 0 1013 00:00 test2.txt
-rw-r--r-- 1 root root 0 1013 00:00 test3.txt

更多用法
7. 不带任何选项下运行 touch
8. 只改变访问时间
9. 只改变修改时间
10. 更改为自定义时间戳
11. 改变日期和时间的另一种方式
12. 改变时区
13. 结合参数下使用-d选项
14. 创建一个空文件
15. 同时创建多个文件

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值