我不是网管 - 轻松掌握 Linux Touch 命令

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

[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 ~]#
展开阅读全文

页面更新:2024-02-07

标签:命令   示例   网管   语法   符号   选项   日期   轻松   链接   文件   时间   目录

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top