Linux命令之touch

touch [选项] … 文件 …

说明:将每个文件的访问时间和修改时间改为当前时间。如果文件不存在则会创建空文件,除非使用-c或-h选项。如果文件名为”-”则特殊处理,更改与标准输出相关的文件的访问时间。

(1).选项

-a 只更改访问时间

-c,--no-create 不创建任何文件

-d,--date=字符串 使用指定字符串表示时间而非当前时间

-f 忽略

-h,--no-dereference 只影响符号链接本身,而非符号链接所指示的目的地(当系统支持更改符号链接的所有者时,此选项才有用)

-m 只更改修改时间

-r,--reference=文件 使用指定文件的时间属性而非当前时间

-t STAMP 使用[[CC(世纪)]YY]MMDDhhmm[.ss]格式的时间而非当前时间

--time=WORD使用WORD指定的时间:access/atime/use都等于-a选项的效果,而modify/mtime等于-m选项的效果

(2).实例

用touch创建文件

[root@CentOS6 桌面]# ll
总用量 0
[root@CentOS6 桌面]# touch touch_test_file    //在当前目录下创建文件
[root@CentOS6 桌面]# ll
总用量 0
-rw-r--r--. 1 root root 0 6月  17 13:28 touch_test_file
[root@CentOS6 桌面]# mkdir mydir
[root@CentOS6 桌面]# touch mydir/touch_test_file    //在指定目录下创建文件
[root@CentOS6 桌面]# ls -l mydir/
总用量 0
-rw-r--r--. 1 root root 0 6月  17 13:28 touch_test_file

 使用-t选项更改文档时间

[root@CentOS6 桌面]# touch -t 06061806 touch_test_file 
[root@CentOS6 桌面]# ll
总用量 4
drwxr-xr-x. 2 root root 4096 6月  17 13:28 mydir
-rw-r--r--. 1 root root    0 6月   6 18:06 touch_test_file

 使用将一个文档时间同步到另一个文档

[root@CentOS6 桌面]# touch touch_test_file2
[root@CentOS6 桌面]# ll
总用量 4
drwxr-xr-x. 2 root root 4096 6月  17 13:28 mydir
-rw-r--r--. 1 root root    0 6月   6 18:06 touch_test_file
-rw-r--r--. 1 root root    0 6月  17 13:34 touch_test_file2
[root@CentOS6 桌面]# touch -r touch_test_file touch_test_file2
[root@CentOS6 桌面]# ll
总用量 4
drwxr-xr-x. 2 root root 4096 6月  17 13:28 mydir
-rw-r--r--. 1 root root    0 6月   6 18:06 touch_test_file
-rw-r--r--. 1 root root    0 6月   6 18:06 touch_test_file2

以当前时间为参照,将将对过去的时间更改为文档时间

[root@CentOS6 桌面]# ll
总用量 4
drwxr-xr-x. 2 root root 4096 6月  17 13:28 mydir
-rw-r--r--. 1 root root    0 6月   6 18:06 touch_test_file
-rw-r--r--. 1 root root    0 6月   6 18:06 touch_test_file2
[root@CentOS6 桌面]# touch -d "10 days ago" touch_test_file
[root@CentOS6 桌面]# ll
总用量 4
drwxr-xr-x. 2 root root 4096 6月  17 13:28 mydir
-rw-r--r--. 1 root root    0 6月   7 13:38 touch_test_file
-rw-r--r--. 1 root root    0 6月   6 18:06 touch_test_file2

猜你喜欢

转载自www.cnblogs.com/diantong/p/9212425.html
今日推荐