linux命令之touch

本文详细介绍了Linux中touch命令的使用方法及其多种选项的功能,包括创建空文件、更新文件时间戳等,并通过具体示例展示了如何操作。

linux命令

linux命令之touch

touch

格式:touch (选项) (参数)

作用:

一、是用来创建新的空文件

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

选项:

r -a:    只更改存取时间

r -c:    不建立任何文件

r -d:   <时间日期> 使用指定的日期时间,而非现在的时间

r -f:    此参数将忽略不予处理,仅负责解决BSD版本touch指令的兼容性问题

r -m: 只更该变动时间

r -r:   <参考文件或目录> 把指定文件或目录的日期时间,统统设成和参考文件或目录的日期时间相同

r -t: <日期时间> 使用指定的日期时间,而非现在的时间;

参数:

一、指定要创建的空文件名称

二、指定要设置时间属性的文件名称

示例:

1

在目录/mnt下创建空文件 a.txt b.txt

[root@localhost mnt]# touch a.txt b.txt

[root@localhost mnt]# ll

total 0

-rw-r--r-- 1 root root 0 Nov  7 16:59 a.txt

-rw-r--r-- 1 root root 0 Nov  7 16:59 b.txt

2

复制/etc/passwd文件到/mnt目录下,使用 touch –a 修改文件passwd的存取时间

[root@localhost mnt]# cp -a /etc/passwd .

[root@localhost mnt]# stat passwd

File: ‘passwd’

Size: 2264         Blocks:
8          IO Block: 4096   regular file

Device: fd00h/64768d    Inode: 4558657     Links: 1

Access: (0644/-rw-r--r--)  Uid: (   
0/    root)   Gid: (   
0/    root)

Access: 2016-11-07 18:36:43.441999782 +0800

Modify: 2016-10-21 18:15:07.082976185 +0800

Change: 2016-11-07 17:03:09.395040011 +0800

Birth: -

[root@localhost mnt]# touch -a passwd

[root@localhost mnt]# stat passwd

File: ‘passwd’

Size: 2264         Blocks:
8          IO Block: 4096   regular file

Device: fd00h/64768d    Inode: 4558657     Links: 1

Access: (0644/-rw-r--r--)  Uid: (   
0/    root)   Gid: (   
0/    root)

Access: 2016-11-07 17:04:09.175035587 +0800

Modify: 2016-10-21 18:15:07.082976185 +0800

Change: 2016-11-07 17:04:09.175035587 +0800

Birth: -

3

使用touch –d 修改文件passwd


[root@localhost mnt]# stat passwd

File: ‘passwd’

Size: 2264         Blocks:
8          IO Block: 4096   regular file

Device: fd00h/64768d    Inode: 4558657     Links: 1

Access: (0644/-rw-r--r--)  Uid: (   
0/    root)   Gid: (   
0/    root)

Access: 2016-11-07 17:04:09.175035587 +0800

Modify: 2016-10-21 18:15:07.082976185 +0800

Change: 2016-11-07 17:04:09.175035587 +0800

Birth: -

[root@localhost mnt]# touch -d
2018-10-12  passwd

[root@localhost mnt]# stat passwd

File: ‘passwd’

Size: 2264         Blocks:
8          IO Block: 4096   regular file

Device: fd00h/64768d    Inode: 4558657     Links: 1

Access: (0644/-rw-r--r--)  Uid: (   
0/    root)   Gid: (   
0/    root)

Access: 2018-10-12 00:00:00.000000000 +0800

Modify: 2018-10-12 00:00:00.000000000 +0800

Change: 2016-11-07 17:04:35.739033621 +0800

Birth: -

4

使用touch –m 修改文件passwd

[root@localhost mnt]# stat passwd

File: ‘passwd’

Size: 2264         Blocks:
8          IO Block: 4096   regular file

Device: fd00h/64768d    Inode: 4558657     Links: 1

Access: (0644/-rw-r--r--)  Uid: (   
0/    root)   Gid: (   
0/    root)

Access: 2018-10-12 00:00:00.000000000 +0800

Modify: 2018-10-12 00:00:00.000000000 +0800

Change: 2016-11-07 17:04:35.739033621 +0800

Birth: -

[root@localhost mnt]# touch  -m passwd

[root@localhost mnt]# stat passwd

File: ‘passwd’

Size: 2264         Blocks:
8          IO Block: 4096   regular file

Device: fd00h/64768d    Inode: 4558657     Links: 1

Access: (0644/-rw-r--r--)  Uid: (   
0/    root)   Gid: (   
0/    root)

Access: 2018-10-12 00:00:00.000000000 +0800

Modify: 2016-11-07 17:05:07.385031279 +0800

Change: 2016-11-07 17:05:07.385031279 +0800

Birth: -

5

使用touch –r修改文件passwd

[root@localhost mnt]# stat passwd

File: ‘passwd’

Size: 2264         Blocks:
8          IO Block: 4096   regular file

Device: fd00h/64768d    Inode: 4558657     Links: 1

Access: (0644/-rw-r--r--)  Uid: (   
0/    root)   Gid: (   
0/    root)

Access: 2018-10-12 00:00:00.000000000 +0800

Modify: 2016-11-07 17:05:07.385031279 +0800

Change: 2016-11-07 17:05:07.385031279 +0800

Birth: -

[root@localhost mnt]# touch -r /etc/passwd passwd

[root@localhost mnt]# stat passwd

File: ‘passwd’

Size: 2264         Blocks:
8          IO Block: 4096   regular file

Device: fd00h/64768d    Inode: 4558657     Links: 1

Access: (0644/-rw-r--r--)  Uid: (   
0/    root)   Gid: (   
0/    root)

Access: 2016-11-07 18:36:43.441999782 +0800

Modify: 2016-10-21 18:15:07.082976185 +0800

Change: 2016-11-07 17:06:45.478024019 +0800

Birth: -

◆Time 使用指定时间而不是当前时间。Time 变量以十进制形[[CC]YY]MMDDhhmm[.SS] 指定

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

互联网老辛

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值