Shell命令(每天学一个shell命令)第十一天 touch命令实例:创建文件

touch 命令实例:创建文件

在Linux系统中,每个文件都关联一个时间戳,并且每个文件都会存储最近一次访问的时间、最近一次修改的时间和最近一次变更的时间等信息。所以,无论何时我们创建一个新文件,访问或修改一个已存在的文件,文件的时间戳都会自动更新。
touch命令就可用于创建、变更和修改文件的时间戳。它是Linux操作系统的标准程序,touch命令有如下选项:
-a:只改变访问时间。猜测单词:access

-c创建任何文件。下面这个单词就不是猜测单词了,叫帮助记忆单词:create

-m:只改变修改时间。猜测单词:modify

-r:用指定文件的时间代替当前时间,猜测单词:replace

-t:使用[[CC]YY]MMDDhhmm[.ss]替代当前时间,这个猜测就是time喽;

  • 使用touch命令创建一个名称是 ncflm(你吃饭了吗)的新的空文件:

[root@vagrant-centos65 linux_shell_example]# clear
[root@vagrant-centos65 linux_shell_example]# touch ncflm
[root@vagrant-centos65 linux_shell_example]# ls
example Example hello.php more.php ncflm tmp touch.php

  • 使用touch命令,你同样可以创建多个文件:

[root@vagrant-centos65 linux_shell_example]# touch ncflm1 ncflm2
[root@vagrant-centos65 linux_shell_example]# ls
example Example hello.php more.php ncflm ncflm1 ncflm2 tmp touch.php

  • 使用-a选项,可以改变或更新文件的最新访问时间。如下命令更新文件的访问时间,如果文件名不存在,则创建一个以相同名字命名的新的空文件:
    在这里插入图片描述

[root@vagrant-centos65 linux_shell_example]# touch -a ncflm
[root@vagrant-centos65 linux_shell_example]# ls -ll
total 12
-rw-r–r-- 1 root root 0 Jul 5 03:32 example
-rw-r–r-- 1 root root 0 Jul 5 03:32 Example
-r–r--r-- 1 root root 0 Jul 5 03:48 hello.php
-rwxrwxrwx. 1 root root 759 Jun 19 07:10 more.php
-rw-r–r-- 1 root root 7 Jul 10 10:12 ncflm
-rw-r–r-- 1 root root 0 Jul 10 10:09 ncflm1
-rw-r–r-- 1 root root 0 Jul 10 10:09 ncflm2
drwxr-xr-x 2 root root 4096 Jul 5 03:43 tmp
-rw-r–r-- 1 root root 0 Jul 10 09:08 touch.php

  • 使用-c选项,可以避免创建一个新文件,并用当前时间更新文件的时间戳:

[root@vagrant-centos65 linux_shell_example]# touch -c ncflm1
[root@vagrant-centos65 linux_shell_example]# ls -ll
total 12
-rw-r–r-- 1 root root 0 Jul 5 03:32 example
-rw-r–r-- 1 root root 0 Jul 5 03:32 Example
-r–r--r-- 1 root root 0 Jul 5 03:48 hello.php
-rwxrwxrwx. 1 root root 759 Jun 19 07:10 more.php
-rw-r–r-- 1 root root 7 Jul 10 10:12 ncflm
-rw-r–r-- 1 root root 0 Jul 10 10:16 ncflm1
-rw-r–r-- 1 root root 0 Jul 10 10:09 ncflm2
drwxr-xr-x 2 root root 4096 Jul 5 03:43 tmp
-rw-r–r-- 1 root root 0 Jul 10 09:08 touch.php

  • 使用-m选项,可以只改变文件的修改时间,而访问时间不变:

[root@vagrant-centos65 linux_shell_example]# touch -m ncflm
[root@vagrant-centos65 linux_shell_example]# ls -lh
total 12K
-rw-r–r-- 1 root root 0 Jul 5 03:32 example
-rw-r–r-- 1 root root 0 Jul 5 03:32 Example
-r–r--r-- 1 root root 0 Jul 5 03:48 hello.php
-rwxrwxrwx. 1 root root 759 Jun 19 07:10 more.php
-rw-r–r-- 1 root root 7 Jul 10 10:23 ncflm
-rw-r–r-- 1 root root 0 Jul 10 10:16 ncflm1
-rw-r–r-- 1 root root 0 Jul 10 10:09 ncflm2
drwxr-xr-x 2 root root 4.0K Jul 5 03:43 tmp
-rw-r–r-- 1 root root 0 Jul 10 09:08 touch.php

  • 你可以同时使用-c-t选项来明确设置文件的时间,举个例子,设置为2019年1月31日23点59分修改了文件:

[root@vagrant-centos65 linux_shell_example]# touch -c -t 1901312359 ncflm
[root@vagrant-centos65 linux_shell_example]# ls -lh
total 12K
-rw-r–r-- 1 root root 0 Jul 5 03:32 example
-rw-r–r-- 1 root root 0 Jul 5 03:32 Example
-r–r--r-- 1 root root 0 Jul 5 03:48 hello.php
-rwxrwxrwx. 1 root root 759 Jun 19 07:10 more.php
-rw-r–r-- 1 root root 7 Jan 31 23:59 ncflm
-rw-r–r-- 1 root root 0 Jul 10 10:16 ncflm1
-rw-r–r-- 1 root root 0 Jul 10 10:09 ncflm2
drwxr-xr-x 2 root root 4.0K Jul 5 03:43 tmp
-rw-r–r-- 1 root root 0 Jul 10 09:08 touch.php

  • 如果想使用文件 a的时间戳更新文件b的时间戳,使用-r选项可以实现:

[root@vagrant-centos65 linux_shell_example]# touch -r ncflm ncflm1
[root@vagrant-centos65 linux_shell_example]# ls -lh
total 12K
-rw-r–r-- 1 root root 0 Jul 5 03:32 example
-rw-r–r-- 1 root root 0 Jul 5 03:32 Example
-r–r--r-- 1 root root 0 Jul 5 03:48 hello.php
-rwxrwxrwx. 1 root root 759 Jun 19 07:10 more.php
-rw-r–r-- 1 root root 7 Jan 31 23:59 ncflm
-rw-r–r-- 1 root root 0 Jan 31 23:59 ncflm1
-rw-r–r-- 1 root root 0 Jul 10 10:09 ncflm2
drwxr-xr-x 2 root root 4.0K Jul 5 03:43 tmp
-rw-r–r-- 1 root root 0 Jul 10 09:08 touch.php

以上就是今天的全部内容,如果喜欢的话,可以打赏哦,谢谢
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值