linux 命令:touch 详解

用法:touch [OPTION]... FILE...

更新一个文件的访问时间(atime)和修改时间(mtime)为当前时间。
如果不使用 -c 或 -h ,创建一个空文件。

FILE参数字符串 - 是经过特殊处理的,并使touch更改与标准输出关联的文件的时间。

  -a			            只更改访问时间(atime)
  -c, --no-create	        不创建任何文件
  -d, --date=字符串	         使用指定字符串表示时间而非当前时间
  -f			            (忽略)
  -h, --no-dereference		会影响符号链接本身,而非符号链接所指示的目的地
				            (当系统支持更改符号链接的所有者时,此选项才有用)
  -m			            只更改修改时间(mtime)
  -r, --reference=FILE      使用FILE的时间参数,而不是当前时间
  -t STAMP                  使用 [[CC]YY]MMDDhhmm[.ss] 而不是当前时间
      --time=WORD           改变具体时间:
                            WORD 是 access, atime, 相当于使用 -a
                            WORD 是 modify, mtime, 相当于使用 -m
      --help		        显示此帮助信息并退出
      --version		        显示版本信息并退出

请注意,-d 和 -t 选项可接受不同的时间/日期格式。

使用示例:

1. 修改文件的atime, mtime:

[root@server dir]# ll file1
-rw-r--r-- 1 root root 0 11月 28 15:12 file1
[root@server dir]# touch file1
[root@server dir]# ll file1
-rw-r--r-- 1 root root 0 11月 29 14:19 file1
[root@server dir]# stat file1
  文件:"file1"
  大小:0         	块:0          IO 块:4096   普通空文件
设备:fd01h/64769d	Inode:1050106     硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
最近访问:2021-11-29 14:19:22.248921719 +0800
最近更改:2021-11-29 14:19:22.248921719 +0800
最近改动:2021-11-29 14:19:22.248921719 +0800
创建时间:-

可以看到,file1在执行touch之后,atime和mtime都发生了变化。

2. 创建新的空文件:

[root@server dir]# ll
总用量 12
drwxr-xr-x 2 root root 4096 11月 28 15:12 dir1
lrwxrwxrwx 1 root root    4 11月 28 15:13 dir2 -> dir1
-rw-r--r-- 1 root root    0 11月 29 14:19 file1
-rw-r--r-- 1 root root    4 11月 28 16:41 file2
-rw-r--r-- 1 root root    0 11月 28 15:12 file3
-rw-r--r-- 1 root root  164 11月 28 16:41 file.zip
[root@server dir]# touch file4
[root@server dir]# ll
总用量 12
drwxr-xr-x 2 root root 4096 11月 28 15:12 dir1
lrwxrwxrwx 1 root root    4 11月 28 15:13 dir2 -> dir1
-rw-r--r-- 1 root root    0 11月 29 14:19 file1
-rw-r--r-- 1 root root    4 11月 28 16:41 file2
-rw-r--r-- 1 root root    0 11月 28 15:12 file3
-rw-r--r-- 1 root root    0 11月 29 14:41 file4
-rw-r--r-- 1 root root  164 11月 28 16:41 file.zip
[root@server dir]# file file4
file4: empty

可以看到,file4被成功创建,并且是个空文件。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值