每日一个linux命令08-touch

1. 命令解析

命令用途

更新文件时间戳(访问时间aTime,修改时间mTime、状态时间)或创建不存在的文件

命令格式

touch [OPTION]... FILE...

命令参数

-a change only the access time
-c, --no-create do not create any files
-d, --date=STRING parse STRING and use it instead of current time
-f (ignored)
-h, --no-dereference affect each symbolic link instead of any referenced
file (useful only on systems that can change the
timestamps of a symlink)
-m change only the modification time
-r, --reference=FILE use this file's times instead of current time
-t STAMP use [[CC]YY]MMDDhhmm[.ss] instead of current time
--time=WORD change the specified time:
WORD is access, atime, or use: equivalent to -a
WORD is modify or mtime: equivalent to -m

2. 示例

2.1 更新文件时间戳(三个都改)

[root@fanshifeng touchTest]# ll
total 0
-rw-r--r-- 1 root root 0 Jan  1 12:12 f1
[root@fanshifeng touchTest]# touch f1
[root@fanshifeng touchTest]# ll
total 0
-rw-r--r-- 1 root root 0 Apr 12 19:47 f1

2.2 创建不存在的文件

root@fanshifeng touchTest]# ll
total 0
-rw-r--r-- 1 root root 0 Apr 12 19:47 f1
[root@fanshifeng touchTest]# touch f2
[root@fanshifeng touchTest]# ll
total 0
-rw-r--r-- 1 root root 0 Apr 12 19:47 f1
-rw-r--r-- 1 root root 0 Apr 12 19:48 f2

2.3 批量操作

root@fanshifeng touchTest]# ll
total 0
-rw-r--r-- 1 root root 0 Apr 12 19:47 f1
-rw-r--r-- 1 root root 0 Apr 12 19:48 f2
[root@fanshifeng touchTest]# touch f1 f2
[root@fanshifeng touchTest]# ll
total 0
-rw-r--r-- 1 root root 0 Apr 12 19:51 f1
-rw-r--r-- 1 root root 0 Apr 12 19:51 f2

2.4 只更新文件的访问时间 -a

[root@fanshifeng touchTest]# stat f1
  File: `f1'
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: fd01h/64769d    Inode: 1321195     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2017-04-12 19:51:22.794019631 +0800
Modify: 2017-04-12 19:51:22.794019631 +0800
Change: 2017-04-12 19:51:22.794019631 +0800
[root@fanshifeng touchTest]# touch -a f1
[root@fanshifeng touchTest]# stat f1
  File: `f1'
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: fd01h/64769d    Inode: 1321195     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2017-04-12 20:02:07.821548363 +0800
Modify: 2017-04-12 19:51:22.794019631 +0800
Change: 2017-04-12 20:02:07.821548363 +0800

2.5 只更新文件的修改时间 -m

[root@fanshifeng touchTest]# stat f1
  File: `f1'
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: fd01h/64769d    Inode: 1321195     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2017-04-12 20:02:47.304009142 +0800
Modify: 2017-04-12 20:02:47.304009142 +0800
Change: 2017-04-12 20:02:47.304009142 +0800
[root@fanshifeng touchTest]# touch -m f1
[root@fanshifeng touchTest]# stat f1
  File: `f1'
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: fd01h/64769d    Inode: 1321195     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2017-04-12 20:02:47.304009142 +0800
Modify: 2017-04-12 20:07:23.419261753 +0800
Change: 2017-04-12 20:07:23.419261753 +0800

2.6 不创建文件 -c

[root@fanshifeng touchTest]# ls
f1  f2
[root@fanshifeng touchTest]# touch -c f3
[root@fanshifeng touchTest]# ls
f1  f2

2.7 使用其他文件的时间戳作为当前时间戳 -r

[root@fanshifeng touchTest]# stat f2
  File: `f2'
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: fd01h/64769d    Inode: 1321198     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2017-04-12 19:59:57.643029077 +0800
Modify: 2017-04-12 19:51:22.794019631 +0800
Change: 2017-04-12 19:59:57.643029077 +0800
[root@fanshifeng touchTest]# touch -r f1 f2;
[root@fanshifeng touchTest]# stat f2
  File: `f2'
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: fd01h/64769d    Inode: 1321198     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2017-04-12 20:02:47.304009142 +0800
Modify: 2017-04-12 20:07:23.419261753 +0800
Change: 2017-04-12 20:08:58.573382677 +0800
[root@fanshifeng touchTest]# stat f1
  File: `f1'
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: fd01h/64769d    Inode: 1321195     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2017-04-12 20:02:47.304009142 +0800
Modify: 2017-04-12 20:07:23.419261753 +0800
Change: 2017-04-12 20:07:23.419261753 +0800

2.8 使用指定时间戳(只改变aTime和mTime)

[root@fanshifeng touchTest]# stat f1
  File: `f1'
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: fd01h/64769d    Inode: 1321195     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2017-04-12 20:02:47.304009142 +0800
Modify: 2017-04-12 20:07:23.419261753 +0800
Change: 2017-04-12 20:07:23.419261753 +0800
[root@fanshifeng touchTest]# touch -t201702031112.13 f1
[root@fanshifeng touchTest]# stat f1
  File: `f1'
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: fd01h/64769d    Inode: 1321195     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2017-02-03 11:12:13.000000000 +0800
Modify: 2017-02-03 11:12:13.000000000 +0800
Change: 2017-04-12 20:11:01.583831352 +0800
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容