Linux touch命令怎么用(linux,touch命令,开发技术)

时间:2024-04-29 20:13:06 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

Linux touch命令怎么用

语法

touch[-acfm][-d][-r][-t][--help][--version][文件或目录…]

参数说明:

  • a 改变档案的读取时间记录。

  • m 改变档案的修改时间记录。

  • c 假如目的档案不存在,不会建立新的档案。与 –no-create 的效果一样。

  • f 不使用,是为了与其他 unix 系统的相容性而保留。

  • r 使用参考档的时间记录,与 –file 的效果一样。

  • d 设定时间与日期,可以使用各种不同的格式。

  • t 设定档案的时间记录,格式与 date 指令相同。

  • –no-create 不会建立新档案。

  • –help 列出指令格式。

  • –version 列出版本讯息。

touch命令应用实例

示例:1 使用 touch 创建一个空文件

要在 Linux 系统上使用 touch 命令创建空文件,键入 touch,然后输入文件名。如下所示:

[root@linuxtechi~]#touchdevops.txt[root@linuxtechi~]#ls-ldevops.txt-rw-r--r--.1rootroot0Mar2922:39devops.txt

示例:2 使用 touch 创建批量空文件

可能会出现一些情况,我们必须为某些测试创建大量空文件,这可以使用 touch 命令轻松实现:

[root@linuxtechi~]#touchsysadm-{1..20}.txt

在上面的例子中,我们创建了 20 个名为 sysadm-1.txt 到 sysadm-20.txt 的空文件,你可以根据需要更改名称和数字。

示例:3 改变/更新文件和目录的访问时间

假设我们想要改变名为 devops.txt 文件的访问时间,在 touch 命令中使用 -a 选项,然后输入文件名。如下所示:

[root@linuxtechi~]#touch-adevops.txt

现在使用 stat 命令验证文件的访问时间是否已更新:

[root@linuxtechi~]#statdevops.txtFile:'devops.txt'Size:0Blocks:0IOBlock:4096regularemptyfileDevice:fd00h/64768dInode:67324178Links:1Access:(0644/-rw-r--r--)Uid:(0/root)Gid:(0/root)Context:unconfined_u:object_r:admin_home_t:s0Access:2018-03-2923:03:10.902000000-0400Modify:2018-03-2922:39:29.365000000-0400Change:2018-03-2923:03:10.902000000-0400Birth:-

改变目录的访问时间:

假设我们在 /mnt 目录下有一个 nfsshare 文件夹,让我们用下面的命令改变这个文件夹的访问时间:

[root@linuxtechi~]#touch-m/mnt/nfsshare/[root@linuxtechi~]#stat/mnt/nfsshare/File:'/mnt/nfsshare/'Size:6Blocks:0IOBlock:4096directoryDevice:fd00h/64768dInode:2258Links:2Access:(0755/drwxr-xr-x)Uid:(0/root)Gid:(0/root)Context:unconfined_u:object_r:mnt_t:s0Access:2018-03-2923:34:38.095000000-0400Modify:2018-03-0310:42:45.194000000-0500Change:2018-03-2923:34:38.095000000-0400Birth:-

示例:4 更改访问时间而不用创建新文件

在某些情况下,如果文件存在,我们希望更改文件的访问时间,并避免创建文件。在 touch 命令中使用 -c 选项即可,如果文件存在,那么我们可以改变文件的访问时间,如果不存在,我们也可不会创建它。

[root@linuxtechi~]#touch-csysadm-20.txt[root@linuxtechi~]#touch-cwinadm-20.txt[root@linuxtechi~]#ls-lwinadm-20.txtls:cannotaccesswinadm-20.txt:Nosuchfileordirectory

示例:5 更改文件和目录的修改时间

在 touch 命令中使用 -m 选项,我们可以更改文件和目录的修改时间。

让我们更改名为 devops.txt 文件的更改时间:

[root@linuxtechi~]#touch-mdevops.txt

现在使用 stat 命令来验证修改时间是否改变:

[root@linuxtechi~]#statdevops.txtFile:'devops.txt'Size:0Blocks:0IOBlock:4096regularemptyfileDevice:fd00h/64768dInode:67324178Links:1Access:(0644/-rw-r--r--)Uid:(0/root)Gid:(0/root)Context:unconfined_u:object_r:admin_home_t:s0Access:2018-03-2923:03:10.902000000-0400Modify:2018-03-2923:59:49.106000000-0400Change:2018-03-2923:59:49.106000000-0400Birth:-

同样的,我们可以改变一个目录的修改时间:

[root@linuxtechi~]#touch-m/mnt/nfsshare/

使用 stat 交叉验证访问和修改时间:

[root@linuxtechi~]#statdevops.txtFile:'devops.txt'Size:0Blocks:0IOBlock:4096regularemptyfileDevice:fd00h/64768dInode:67324178Links:1Access:(0644/-rw-r--r--)Uid:(0/root)Gid:(0/root)Context:unconfined_u:object_r:admin_home_t:s0Access:2018-03-3000:06:20.145000000-0400Modify:2018-03-3000:06:20.145000000-0400Change:2018-03-3000:06:20.145000000-0400Birth:-

示例:7 将访问和修改时间设置为特定的日期和时间

每当我们使用 touch 命令更改文件和目录的访问和修改时间时,它将当前时间设置为该文件或目录的访问和修改时间。

假设我们想要将特定的日期和时间设置为文件的访问和修改时间,这可以使用 touch 命令中的 -c 和 -t 选项来实现。

日期和时间可以使用以下格式指定:

{CCYY}MMDDhhmm.ss

其中:

  • CC – 年份的前两位数字

  • YY – 年份的后两位数字

  • MM – 月份 (01-12)

  • DD – 天 (01-31)

  • hh – 小时 (00-23)

  • mm – 分钟 (00-59)

让我们将 devops.txt 文件的访问和修改时间设置为未来的一个时间(2025 年 10 月 19 日 18 时 20 分)。

[root@linuxtechi~]#touch-c-t202510191820devops.txt

使用 stat 命令查看更新访问和修改时间:

Linux touch命令怎么用

stat-command-output-linux

根据日期字符串设置访问和修改时间,在 touch 命令中使用 -d 选项,然后指定日期字符串,后面跟文件名。如下所示:

[root@linuxtechi~]#touch-c-d"2010-02-0720:15:12.000000000+0530"sysadm-29.txt

使用 stat 命令验证文件的状态:

[root@linuxtechi~]#statsysadm-20.txtFile:‘sysadm-20.txt’Size:0Blocks:0IOBlock:4096regularemptyfileDevice:fd00h/64768dInode:67324189Links:1Access:(0644/-rw-r--r--)Uid:(0/root)Gid:(0/root)Context:unconfined_u:object_r:admin_home_t:s0Access:2010-02-0720:15:12.000000000+0530Modify:2010-02-0720:15:12.000000000+0530Change:2018-03-3010:23:31.584000000+0530Birth:-

注意:在上述命令中,如果我们不指定 -c,如果系统中不存在该文件那么 touch 命令将创建一个新文件,并将时间戳设置为命令中给出的。

示例:8 使用参考文件设置时间戳(-r)

在 touch 命令中,我们可以使用参考文件来设置文件或目录的时间戳。假设我想在 devops.txt 文件上设置与文件 sysadm-20.txt 文件相同的时间戳,touch 命令中使用 -r 选项可以轻松实现。

语法:

#touch-r{参考文件}真正文件[root@linuxtechi~]#touch-rsysadm-20.txtdevops.txt

示例:9 在符号链接文件上更改访问和修改时间

默认情况下,每当我们尝试使用 touch 命令更改符号链接文件的时间戳时,它只会更改原始文件的时间戳。如果你想更改符号链接文件的时间戳,则可以使用 touch 命令中的 -h 选项来实现。

语法:

[root@linuxtechiopt]#ls-l/root/linuxgeeks.txtlrwxrwxrwx.1rootroot15Mar3010:56/root/linuxgeeks.txt->linuxadmins.txt[root@linuxtechi~]#touch-t203010191820-hlinuxgeeks.txt[root@linuxtechi~]#ls-llinuxgeeks.txtlrwxrwxrwx.1rootroot15Oct192030linuxgeeks.txt->linuxadmins.txt
 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:Linux touch命令怎么用的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:Linux安装Conda具体步骤是什么下一篇:

4 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18