关于linux:9个超有用的-Linux-Touch-命令实例讲解

6次阅读

共计 4326 个字符,预计需要花费 11 分钟才能阅读完成。

touch 命令用于创立空文件,也用于更改 Linux 零碎中现有文件的工夫戳。这里更改工夫戳意味着更新文件和目录的拜访和批改工夫。

命令语法

touch {options} {file}

语法选项

1) 创立一个空文件

touch 命令创立一个空文件,输出 touch 后跟文件名,示例如下

[root@linuxtechi ~]# touch devops.txt
[root@linuxtechi ~]# ls -l devops.txt
-rw-r--r--. 1 root root 0 Mar 29 22:39 devops.txt
[root@linuxtechi ~]#

2) 批量创立空文件

在某些状况下,咱们必须为一些测试创立大量的空文件,示例如下

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

在下面的例子中,咱们创立了 20 个空文件,名称为 sysadm-1.txt 到 sysadm-20.txt,您能够依据须要更改名称和编号。

3) 更改 / 更新文件和目录的拜访工夫

应用 -a 选项,能够更改 / 更新文件和目录的拜访工夫

批改文件的拜访工夫

[root@linuxtechi ~]# touch -a devops.txt
[root@linuxtechi ~]#

应用 stat 命令验证文件的拜访工夫是否曾经更新

[root@linuxtechi ~]# stat devops.txt
  File:‘devops.txt’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: -
[root@linuxtechi ~]#

批改目录的拜访工夫

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

应用 stat 命令验证目录的拜访工夫是否曾经更新

[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: -
[root@linuxtechi ~]#

4) 更改拜访工夫而不创立新文件

应用 -c 选项,能够更改文件的拜访工夫 (如果它存在的话),并防止创立该文件。

[root@linuxtechi ~]# touch -c sysadm-20.txt
[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
[root@linuxtechi ~]#

留神: winadm-20.txt 是不存在的文件,这就是咱们列出文件时报错的起因

5) 更改文件和目录的批改工夫

应用 -m 选项能够更改文件和目录的批改工夫,示例如下

[root@linuxtechi ~]# touch -m devops.txt
[root@linuxtechi ~]#

当初应用 stat 命令验证批改工夫是否曾经更改

[root@linuxtechi ~]# stat devops.txt
  File:‘devops.txt’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 ~]#

同样,咱们能够扭转目录的批改工夫

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

6) 同时更改拜访和批改工夫

应用 -am 选项,能够同时更改拜访和批改,示例如下

[root@linuxtechi ~]# touch -am devops.txt
[root@linuxtechi ~]#

应用 stat 验证拜访和批改工夫

[root@linuxtechi ~]# stat devops.txt
  File:‘devops.txt’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: -
[root@linuxtechi ~]#

7) 将拜访和批改工夫设置为特定的日期和工夫

当咱们应用 touch 命令更改文件和目录的拜访和批改工夫时,它将以后工夫设置为该文件或目录的拜访和批改工夫。

假如咱们想要设置特定的日期和工夫作为文件的拜访和批改工夫,这能够应用 touch 命令中的 -c 和 -t 选项来实现。

日期和工夫的格局为:{CCYY}MMDDhhmm.ss

  • CC – First two digits of a year
  • YY – Second two digits of a year
  • MM – Month of the Year (01-12)
  • DD – Day of the Month (01-31)
  • hh – Hour of the day (00-23)
  • mm – Minutes of the hour (00-59)
[root@linuxtechi ~]# touch -c -t 202510191820 devops.txt

应用 stat 命令查看更新拜访和批改工夫

应用 -d 选项,依据日期字符串设置拜访和批改工夫,示例如下

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

应用 stat 命令查看状态

[root@linuxtechi ~]# stat sysadm-20.txt
  File:‘sysadm-20.txt’Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fd00h/64768d    Inode: 67324189    Links: 1
Access: (0644/-rw-r--r--)  Uid: (0/    root)   Gid: (0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2010-02-07 20:15:12.000000000 +0530
Modify: 2010-02-07 20:15:12.000000000 +0530
Change: 2018-03-30 10:23:31.584000000 +0530
 Birth: -
[root@linuxtechi ~]#

留神: 在上述命令中,如果咱们不指定 -c,那么 touch 命令将创立一个新文件,以防零碎中不存在该文件,并将设置命令中提到的工夫戳。

8) 应用援用文件为文件设置工夫戳

应用 -r 选项,咱们能够应用一个参考文件来设置文件或目录的工夫戳。

假如我想在 devops.txt 文件上设置与 sysadm-20.txt 文件雷同的工夫戳。

[root@linuxtechi ~]# touch -r sysadm-20.txt devops.txt
[root@linuxtechi ~]#

9) 更改符号链接文件的拜访和批改工夫

默认状况下,当咱们试图用 touch 命令扭转一个符号链接文件的工夫戳时,它只会扭转原始文件的工夫戳,如果你想扭转一个符号链接文件的工夫戳,那么能够应用 touch 命令中的 -h 选项来实现

语法如下:

touch -h {symbolic link file}

[root@linuxtechi opt]# ls -l /root/linuxgeeks.txt
lrwxrwxrwx. 1 root root 15 Mar 30 10:56 /root/linuxgeeks.txt -> linuxadmins.txt
[root@linuxtechi ~]# touch -t 203010191820 -h linuxgeeks.txt
[root@linuxtechi ~]# ls -l linuxgeeks.txt
lrwxrwxrwx. 1 root root 15 Oct 19  2030 linuxgeeks.txt -> linuxadmins.txt
[root@linuxtechi ~]#

以上就是这篇文章的全部内容,我心愿这些 touch 命令示例对你有用。请在下方评论区分享您的疑难和反馈。

我的开源我的项目

  • course-tencent-cloud(酷瓜云课堂 – gitee 仓库)
  • course-tencent-cloud(酷瓜云课堂 – github 仓库)
正文完
 0