linux中的touch命令一般用来修改文件时间戳,或者新建一个不存在的文件。相关linux 命令的文章中所用到的测试文件都是用 touch 创建出来的,创建目录则是mkdir。

1.命令格式


 touch [选项]… 文件…

2.命令参数


-a,–time=atime,–time=access或–time=use 只更改存取时间。
-c 或–no-create不建立任何文档。
-d 使用指定的日期时间,而非现在的时间。
-f 此参数将忽略不予处理,仅负责解决BSD版本touch指令的兼容性问题。
-m 或–time=mtime或–time=modify  只更改变动时间。
-r 把指定文档或目录的日期时间,统统设成和参考文档或目录的日期时间相同。
-t 使用指定的日期时间,不是当前时间。

3.命令功能


touch命令参数可更改文档或目录的日期时间,包括存取时间和更改时间;创建新的文件 。

4.使用范例


1、创建不存在的文件,用法如下

命令:

touch text.txt text1.txt text2.txt

输出:

lighthouse@VM-4-14-ubuntu:~/touch$ touch text.txt text1.txt text2.txt
lighthouse@VM-4-14-ubuntu:~/touch$ tree
.
├── text1.txt
├── text2.txt
└── text.txt

0 directories, 3 files

不创建text3.txt,参数使用 -c

lighthouse@VM-4-14-ubuntu:~/touch$ touch -c text3.txt
lighthouse@VM-4-14-ubuntu:~/touch$ tree
.
├── text1.txt
├── text2.txt
└── text.txt

0 directories, 3 files

2、更新text.txt 的时间和text3.txt时间戳相同

命令:

touch -r text3.txt text.txt 

先新建text3.txt,再修改时间(使得和之前的文件时间不同)
输出:

lighthouse@VM-4-14-ubuntu:~/touch$ touch text3.txt
lighthouse@VM-4-14-ubuntu:~/touch$ ll
total 8
drwxrwxr-x 2 lighthouse lighthouse 4096 Jun 29 14:29 ./
drwxr-xr-x 9 lighthouse lighthouse 4096 Jun 29 14:22 ../
-rw-rw-r-- 1 lighthouse lighthouse    0 Jun 29 14:23 text1.txt
-rw-rw-r-- 1 lighthouse lighthouse    0 Jun 29 14:23 text2.txt
-rw-rw-r-- 1 lighthouse lighthouse    0 Jun 29 14:29 text3.txt
-rw-rw-r-- 1 lighthouse lighthouse    0 Jun 29 14:23 text.txt
lighthouse@VM-4-14-ubuntu:~/touch$ touch -r text3.txt text.txt 
lighthouse@VM-4-14-ubuntu:~/touch$ ll
total 8
drwxrwxr-x 2 lighthouse lighthouse 4096 Jun 29 14:29 ./
drwxr-xr-x 9 lighthouse lighthouse 4096 Jun 29 14:22 ../
-rw-rw-r-- 1 lighthouse lighthouse    0 Jun 29 14:23 text1.txt
-rw-rw-r-- 1 lighthouse lighthouse    0 Jun 29 14:23 text2.txt
-rw-rw-r-- 1 lighthouse lighthouse    0 Jun 29 14:29 text3.txt
-rw-rw-r-- 1 lighthouse lighthouse    0 Jun 29 14:29 text.txt

原来 Jun 29 14:23 text.txt,修改后Jun 29 14:29 text.txt,变成了 text3.txt的时间,注意,被修改的文件是后面的参数

3、设定文件的时间戳

命令:

touch -t 202202291437.50 text.txt

输出:

lighthouse@VM-4-14-ubuntu:~/touch$ ll
total 8
drwxrwxr-x 2 lighthouse lighthouse 4096 Jun 29 14:29 ./
drwxr-xr-x 9 lighthouse lighthouse 4096 Jun 29 14:22 ../
-rw-rw-r-- 1 lighthouse lighthouse    0 Jun 29 14:23 text1.txt
-rw-rw-r-- 1 lighthouse lighthouse    0 Jun 29 14:23 text2.txt
-rw-rw-r-- 1 lighthouse lighthouse    0 Jun 29 14:29 text3.txt
-rw-rw-r-- 1 lighthouse lighthouse    0 Jun 29 14:29 text.txt
lighthouse@VM-4-14-ubuntu:~/touch$ touch -t 202206291437.50 text.txt
lighthouse@VM-4-14-ubuntu:~/touch$ ll
total 8
drwxrwxr-x 2 lighthouse lighthouse 4096 Jun 29 14:29 ./
drwxr-xr-x 9 lighthouse lighthouse 4096 Jun 29 14:22 ../
-rw-rw-r-- 1 lighthouse lighthouse    0 Jun 29 14:23 text1.txt
-rw-rw-r-- 1 lighthouse lighthouse    0 Jun 29 14:23 text2.txt
-rw-rw-r-- 1 lighthouse lighthouse    0 Jun 29 14:29 text3.txt
-rw-rw-r-- 1 lighthouse lighthouse    0 Jun 29 14:37 text.txt
lighthouse@VM-4-14-ubuntu:~/touch$ 

修改前:29 14:29 text.txt
修改后:29 14:37 text.txt

-t time 格式详细说明


使用指定的时间值 time 作为指定文件相应时间戳记的新值。此处的 time 规定为如下形式的十进制数: [[CC]YY]MMDDhhmm[.SS] 这里,CC为年数中的前两位,即”世纪数”;YY为年数的后两位,即某世纪中的年数.如果不给出CC的值,则 touch 将把年数CCYY限定在1969–2068之内.MM为月数,DD为天将把年数CCYY限定在1969–2068之内.MM为月数,DD为天数,hh 为小时数(几点),mm为分钟数,SS为秒数.此处秒的设定范围是0–61,这样可以处理闰秒.这些数字组成的时间是环境变量TZ指定的时区中的一个时间。由于系统的限制,早于1970年1月1日的时间是错误的。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐