touch命令用于创建空文件,也可以修改Unix和Linux系统上现有文件时间戳。这儿所说的修改时间戳意味着更新文件和目录的访问以及更改时间。
让我们来瞧瞧touch命令的句型和选项:
句型:
#touch{选项}{文件}
touch命令中使用的选项:
在这篇文章中,我们将介绍Linux中9个有用的touch命令示例。
示例:1使用touch创建一个空文件
要在Linux系统上使用touch命令创建空文件,键入touchubuntu linux,之后输入文件名。如下所示:
[root@linuxtechi ~]# touch
[root@linuxtechi ~]# ls -l
-rw-r--r--. 1 root root 0 Mar 29 22:39
示例:2使用touch创建批量空文件
可能会出现一些情况linux安装,我们必须为个别测试创建大量空文件,这可以使用touch命令轻松实现:
[root@linuxtechi ~]# touch sysadm-{1..20}.txt
在前面的事例中,我们创建了20个名为到的空文件,你可以按照须要更更名称和数字。
示例:3改变/更新文件和目录的访问时间
假定我们想要改变名为文件的访问时间,在touch命令中使用-a选项,之后输入文件名。如下所示:
[root@linuxtechi ~]# touch -a
现今使用stat命令验证文件的访问时间是否已更新:
[root@linuxtechi ~]# stat
File: ''
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd00h/64768d Inode: 67324178 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2018-03-29 23:03:10.902000000 -0400
Modify: 2018-03-29 22:39:29.365000000 -0400
Change: 2018-03-29 23:03:10.902000000 -0400
Birth: -
改变目录的访问时间:
假定我们在/mnt目录下有一个nfsshare文件夹,让我们用下边的命令改变这个文件夹的访问时间:
[root@linuxtechi ~]# touch -m /mnt/nfsshare/
[root@linuxtechi ~]# stat /mnt/nfsshare/
File: '/mnt/nfsshare/'
Size: 6 Blocks: 0 IO Block: 4096 directory
Device: fd00h/64768d Inode: 2258 Links: 2
Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:mnt_t:s0
Access: 2018-03-29 23:34:38.095000000 -0400
Modify: 2018-03-03 10:42:45.194000000 -0500
Change: 2018-03-29 23:34:38.095000000 -0400
Birth: -
示例:4修改访问时间而不用创建新文件
在个别情况下,假如文件存在,我们希望修改文件的访问时间,并防止创建文件。在touch命令中使用-c选项即可,假如文件存在,这么我们可以改变文件的访问时间,倘若不存在linux修改文件时间戳,我们也可不会创建它。
[root@linuxtechi ~]# touch -c
[root@linuxtechi ~]# touch -c winadm-20.txt
[root@linuxtechi ~]# ls -l winadm-20.txt
ls: cannot access winadm-20.txt: No such file or directory
示例:5修改文件和目录的更改时间
在touch命令中使用-m选项,我们可以修改文件和目录的更改时间。
让我们更更名为文件的修改时间:
[root@linuxtechi ~]# touch -m
现今使用stat命令来验证更改时间是否改变:
[root@linuxtechi ~]# stat
File: ''
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd00h/64768d Inode: 67324178 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2018-03-29 23:03:10.902000000 -0400
Modify: 2018-03-29 23:59:49.106000000 -0400
Change: 2018-03-29 23:59:49.106000000 -0400
Birth: -
同样的,我们可以改变一个目录的更改时间:
[root@linuxtechi ~]# touch -m /mnt/nfsshare/
使用stat交叉验证访问和更改时间:
[root@linuxtechi ~]# stat
File: ''
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd00h/64768d Inode: 67324178 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2018-03-30 00:06:20.145000000 -0400
Modify: 2018-03-30 00:06:20.145000000 -0400
Change: 2018-03-30 00:06:20.145000000 -0400
Birth: -
示例:7将访问和更改时间设置为特定的日期和时间
每每我们使用touch命令修改文件和目录的访问和更改时间时linux修改文件时间戳,它将当前时间设置为该文件或目录的访问和更改时间。
假定我们想要将特定的日期和时间设置为文件的访问和更改时间,这可以使用touch命令中的-c和-t选项来实现。
日期和时间可以使用以下格式指定:
{CCYY}
其中:
让我们将文件的访问和更改时间设置为未来的一个时间(2025年10月19日18时20分)。
[root@linuxtechi ~]# touch -c -t 202510191820
使用stat命令查看更新访问和更改时间:
依照日期字符串设置访问和更改时间,在touch命令中使用-d选项,之后指定日期字符串,前面跟文件名。如下所示:
代码如下:
文章评论