创建文件 touch

一、命令详解


1.命令说明

touch 命令用于修改已存在的文件或者目录的时间属性,包括访问时间(atime)和修改时间(mtime),
若文件不存在,系统会创建新文件

用来创建新的空文件,这个是最常用的一种方法。

注意:
所以文件的真实访问时间和修改时间是可以被修改,在排查系统异常的时候还需要结合日志、历史命令等等因素综合决策。


2.语法格式

touch      [option]      [file]
touch      [选项]         [参数]

3.选项描述

DESCRIPTION
       -a     change only the access time
              #改变文件或文件夹访问时间(access time),默认改为当前时间
       -c, --no-create
              do not create any files
              #如果文件不存在,不会建立新的文件。与 --no-create 的效果一样。
              #已经存在的文件会改变修改时间和访问时间。
       -d, --date=STRING
              parse STRING and use it instead of current time
              #设定时间与日期,可以使用各种不同的格式。
       -f     (ignored)
              #不使用,是为了与其他 unix 系统的相容性而保留
       -h, --no-dereference
              affect  each  symbolic  link  instead of any referenced file (useful only on systems that can change the timestamps of a symlink)
              #影响每个符号链接,而不是任何引用的文件(仅在可以更改符号链接时间戳的系统上有用)
       -m     change only the modification time
              #改变文件或文件夹修改时间(modification time),默认为当前时间
       -r, --reference=FILE
              use this file's times instead of current time
              #更新当前文件夹下的文件及其子目录的时间戳,
              #使用参考文件的时间记录作为指定文件或目录的时间,与 --file 的效果一样。
       -t STAMP
              use [[CC]YY]MMDDhhmm[.ss] instead of current time
              #设置文件或目录的时间记录,格式与 date 指令相同。格式为[YYYYMMDDhhmm]
       --help display this help and exit
              #列出指令帮助
       --version
              output version information and exit
             # 列出版本信息                        

二、命令示例


案例1:创建空文件 touch

touch最常用的方式是创建空文件

[root@centos7 app]#touch f5
[root@centos7 app]#ll f5
-rw-r--r--. 1 root root 0 Dec 13 13:46 f5
[root@centos7 app]#cat f5

案例2:更新文件时间 touch

如果文件已经存在,更新文件的时间atime,mtime,ctime为当前时间。

stat 查看文件属性

[root@centos7 app]#stat ls.log
  File: ‘ls.log’
  Size: 329       	Blocks: 8          IO Block: 4096   regular file
Device: 805h/2053d	Inode: 68          Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2022-11-17 21:57:01.389572254 +0800
Modify: 2022-11-17 22:34:34.969679713 +0800
Change: 2022-11-17 22:34:34.969679713 +0800
 Birth: -
[root@centos7 app]#touch ls.log
[root@centos7 app]#stat ls.log
  File: ‘ls.log’
  Size: 329       	Blocks: 8          IO Block: 4096   regular file
Device: 805h/2053d	Inode: 68          Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2022-12-13 13:57:03.231425702 +0800
Modify: 2022-12-13 13:57:03.231425702 +0800
Change: 2022-12-13 13:57:03.231425702 +0800
 Birth: -

案例3:修改atime和ctime #touch -a

如果改变名为文件的访问时间,在 touch 命令中使用 -a 选项,然后输入文件名。

[root@centos7 app]#touch -a fi
[root@centos7 app]#ls -lu fi
-rw-r--r--. 1 root root 88 Dec 13 13:50 fi
[root@centos7 app]#stat fi
  File: ‘fi’
  Size: 88        	Blocks: 8          IO Block: 4096   regular file
Device: 805h/2053d	Inode: 73          Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2022-12-13 13:50:38.187407341 +0800
Modify: 2022-12-08 22:25:59.874854707 +0800
Change: 2022-12-13 13:50:38.187407341 +0800
 Birth: -

案例4:不创建文件,修改时间#touch -c

可以看到如果文件不存在,就不会建立新的文件;
如果已经存在的文件会改变全部的时间戳。

[root@centos7 app]#stat fo
  File: ‘fo’
  Size: 88        	Blocks: 8          IO Block: 4096   regular file
Device: 805h/2053d	Inode: 72          Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2022-12-08 22:39:38.024893720 +0800
Modify: 2022-12-08 22:39:38.024893720 +0800
Change: 2022-12-08 22:39:38.024893720 +0800
 Birth: -
[root@centos7 app]#touch -c fo
[root@centos7 app]#stat fo
  File: ‘fo’
  Size: 88        	Blocks: 8          IO Block: 4096   regular file
Device: 805h/2053d	Inode: 72          Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2022-12-13 14:08:32.500458569 +0800
Modify: 2022-12-13 14:08:32.500458569 +0800
Change: 2022-12-13 14:08:32.500458569 +0800
 Birth: -

案例5:设定时间#touch -d

文件会将atime,mtime按照设置修改,同时更新ctime时间。

[root@centos7 app]#touch -d "2022-12-12 08:08:08" fo
[root@centos7 app]#stat fo
  File: ‘fo’
  Size: 88        	Blocks: 8          IO Block: 4096   regular file
Device: 805h/2053d	Inode: 72          Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2022-12-12 08:08:08.000000000 +0800
Modify: 2022-12-12 08:08:08.000000000 +0800
Change: 2022-12-13 14:35:32.181535801 +0800
 Birth: -

案例6:修改时间#touch -m

-m参数是改变文件的mtime和ctime修改时间,不会影响访问时间。

[root@centos7 ~]#stat f333
  File: ‘f333’
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: 802h/2050d	Inode: 1798140     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2022-11-30 21:11:07.558882878 +0800
Modify: 2022-11-30 21:11:07.558882878 +0800
Change: 2022-11-30 21:11:07.558882878 +0800
 Birth: -
[root@centos7 ~]#touch -m f333
[root@centos7 ~]#stat f333
  File: ‘f333’
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: 802h/2050d	Inode: 1798140     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2022-11-30 21:11:07.558882878 +0800
Modify: 2022-12-13 14:46:35.811567445 +0800
Change: 2022-12-13 14:46:35.811567445 +0800
 Birth: -

案例7:参考文件设置时间戳#touch -r

格式
touch   -r   {参考文件}    真正文件

替换文件的修改时间(mtime)和访问时间(atime)

[root@centos7 app]#stat f1
  File: ‘f1’
  Size: 19        	Blocks: 8          IO Block: 4096   regular file
Device: 805h/2053d	Inode: 76          Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2022-12-12 17:31:30.997629012 +0800
Modify: 2022-12-12 17:32:57.777633150 +0800
Change: 2022-12-12 17:32:57.777633150 +0800
 Birth: -
[root@centos7 app]#stat f2
  File: ‘f2’
  Size: 4         	Blocks: 8          IO Block: 4096   regular file
Device: 805h/2053d	Inode: 69          Links: 2
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2022-12-08 13:53:11.075844593 +0800
Modify: 2022-12-13 10:47:08.168882343 +0800
Change: 2022-12-13 10:47:08.168882343 +0800
 Birth: -
[root@centos7 app]#touch -r f1 f2
[root@centos7 app]#stat f2
  File: ‘f2’
  Size: 4         	Blocks: 8          IO Block: 4096   regular file
Device: 805h/2053d	Inode: 69          Links: 2
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2022-12-12 17:31:30.997629012 +0800
Modify: 2022-12-12 17:32:57.777633150 +0800
Change: 2022-12-13 15:00:04.372606001 +0800
 Birth: -

案例8:更改文件的访问时间和修改时间#touch -t

注意
CC是年的前两位:如2022年的 20
YY是年的后两位:如2022年的 22
MM代表月份:如 12
DD代表日期:如 10
hh代表小时:如 15
mm代表分钟:如 15
ss代表秒:如 00

[root@centos7 app]#touch -t  202212101516.00 fo
[root@centos7 app]#stat fo
  File: ‘fo’
  Size: 88        	Blocks: 8          IO Block: 4096   regular file
Device: 805h/2053d	Inode: 72          Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2022-12-10 15:16:00.000000000 +0800
Modify: 2022-12-10 15:16:00.000000000 +0800
Change: 2022-12-13 15:07:01.484625890 +0800
 Birth: -

案例9:只修改atime#touch -at

[root@centos7 app]#touch -at "200012121212" fo
[root@centos7 app]#stat fo
  File: ‘fo’
  Size: 88        	Blocks: 8          IO Block: 4096   regular file
Device: 805h/2053d	Inode: 72          Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2000-12-12 12:12:00.000000000 +0800
Modify: 2022-12-10 15:16:00.000000000 +0800
Change: 2022-12-13 15:12:20.661641110 +0800
 Birth: -

案例10:只修改mtime #touch -mt

root@centos7 app]#touch -mt "222212121212" fo
[root@centos7 app]#stat fo
  File: ‘fo’
  Size: 88        	Blocks: 8          IO Block: 4096   regular file
Device: 805h/2053d	Inode: 72          Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2000-12-12 12:12:00.000000000 +0800
Modify: 2222-12-12 12:12:00.000000000 +0800
Change: 2022-12-13 15:13:12.677643590 +0800
 Birth: -

补充说明:

stat 查看这个目录所有文件的状态。


Access访问时间。
Modify修改时间。
Change状态改变时间。


Access time (atime): 当“该文件的内容被取用”时,就会更新这个读取时间 (access)。
举例:我们使用 cat 去读取 /etc/f , 就会更新该文件的 atime 。


Modifiy time (mtime): 当该文件的“内容数据”变更时,就会更新这个时间。
内容数据指的是文件的内容,而不是文件的属性或权限。


Change time (ctime): 当该文件的属性状态改变时,就会更新这个时间。
举例来说,像是权限与属性被更改了,都会更新这个时间。

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值