欢迎光临
我们一直在努力

linux文件管理命令与命令行展开

 

touch: 创建文件或者修改文件的时间属性
atime和mtime都可以更改,但ctime不能使用命令更改,通过更改atime和mtime,ctime会自动修改。

格式: touch [-acm][-r ref_file(参照文件)|-t time(时间值)] file(文件名)…

 

用法描述: touch 修改每个指定文件 file 的存取(access)和/或修改(modifica‐ tion)时间戳记. 除非使用-r或-t选项,这些时间戳记都将修改为当前的时间. 使用-r选项时,这些戳记将按照文件–ref_file的时间戳记来修改(即变得和ref_file时间戳记值相同).使用-t选项,则这些戳记将按照给定的时间值 time 进行修改.同时使用或同时不使用选项-a和-m,存取和修改两个戳记都将被更改.若只使用选项-a,则只修改存取戳记.同样,只使用选项-m,则只修改修改戳记.若要修改戳记的文件尚不存在,除非使用-c选项,touch将创建它(作为空文件,并赋予0666的模式且受umask值的限制)

 

选项:

-a, –time=atime, –time=access, –time=use

只修改存取时间.

-c, –no-create

如果要修改的文件不存在,此选项使touch不去创建它.

 

-m, –time=mtime, –time=modify

只修改修改时间.

-r file, –reference=file

使用参照文件 file 的时间戳记值修改指定文件的时间戳记.

-t decimtime

这里时间值 decimtime 的格式为MMDDhhmm[[CC]YY][.ss]

从左至右分别是月份,日期,小时,分钟,可选的世纪和

年,以及可选的秒. touch 将用这组数值修改指定文件的

时间戳记.请注意,这个格式与POSIX标准所规定的格式并

不一样.

实际的时间格式为年年年年月月日日时时分分[秒秒]

 

示例:

1) 使用touch创建文件file1

[root@localhost tmp]# touch file1

[root@localhost tmp]# ll file1

-rw-r–r–. 1 root root 0 12月 10 20:16 file1

[root@localhost tmp]#

 

[root@localhost tmp]# stat file1

文件:”file1″

大小:0 块:0 IO 块:4096 普通空文件

设备:fd00h/64768d Inode:67167337 硬链接:1

权限:(0644/-rw-r–r–) Uid:( 0/ root) Gid:( 0/ root)

环境:unconfined_u:object_r:user_tmp_t:s0

最近访问:2017-12-10 20:16:52.075000000 +0800

最近更改:2017-12-10 20:16:52.075000000 +0800

最近改动:2017-12-10 20:16:52.075000000 +0800

创建时间:-

 

 

 

2) 修改file1的atime为2012年10月20日19点16分

[root@localhost tmp]# touch -a -t 201210201916 file1

 

[root@localhost tmp]# stat file1

文件:”file1″

大小:0 块:0 IO 块:4096 普通空文件

设备:fd00h/64768d Inode:67167337 硬链接:1

权限:(0644/-rw-r–r–) Uid:( 0/ root) Gid:( 0/ root)

环境:unconfined_u:object_r:user_tmp_t:s0

最近访问:2012-10-20 19:16:00.000000000 +0800

最近更改:2017-12-10 20:16:52.075000000 +0800

最近改动:2017-12-10 20:33:04.285000000 +0800

创建时间:-

 

 

3) 使用touch -c,并不真正创建文件,但如果文件存在,会更新文件的三个时间戳

[root@localhost tmp]# touch -c file1

[root@localhost tmp]# stat file1

文件:”file1″

大小:0 块:0 IO 块:4096 普通空文件

设备:fd00h/64768d Inode:67167337 硬链接:1

权限:(0644/-rw-r–r–) Uid:( 0/ root) Gid:( 0/ root)

环境:unconfined_u:object_r:user_tmp_t:s0

最近访问:2017-12-10 20:34:47.505000000 +0800

最近更改:2017-12-10 20:34:47.505000000 +0800

最近改动:2017-12-10 20:34:47.505000000 +0800

创建时间:-

[root@localhost tmp]# touch -c file2

 

 

4) 修改文件file1的mtime时间为2012年10月20日19点16分

[root@localhost tmp]# touch -m -t 201210201916 file1

[root@localhost tmp]# stat file1

文件:”file1″

大小:0 块:0 IO 块:4096 普通空文件

设备:fd00h/64768d Inode:67167337 硬链接:1

权限:(0644/-rw-r–r–) Uid:( 0/ root) Gid:( 0/ root)

环境:unconfined_u:object_r:user_tmp_t:s0

最近访问:2017-12-10 20:34:47.505000000 +0800

最近更改:2012-10-20 19:16:00.000000000 +0800

最近改动:2017-12-10 20:42:13.486000000 +0800

创建时间:-

 

 

5) 使用file1的时间戳来创建file3,注意ctime没有同步

[root@localhost tmp]# stat file1

文件:”file1″

大小:0 块:0 IO 块:4096 普通空文件

设备:fd00h/64768d Inode:67167337 硬链接:1

权限:(0644/-rw-r–r–) Uid:( 0/ root) Gid:( 0/ root)

环境:unconfined_u:object_r:user_tmp_t:s0

最近访问:2017-12-10 20:34:47.505000000 +0800

最近更改:2012-10-20 19:16:00.000000000 +0800

最近改动:2017-12-10 20:42:13.486000000 +0800

创建时间:-

 

[root@localhost tmp]# touch -r file1 file3

 

[root@localhost tmp]# stat file3

文件:”file3″

大小:0 块:0 IO 块:4096 普通空文件

设备:fd00h/64768d Inode:67167358 硬链接:1

权限:(0644/-rw-r–r–) Uid:( 0/ root) Gid:( 0/ root)

环境:unconfined_u:object_r:user_tmp_t:s0

最近访问:2017-12-10 20:34:47.505000000 +0800

最近更改:2012-10-20 19:16:00.000000000 +0800

最近改动:2017-12-10 21:03:39.538000000 +0800

创建时间:-

1 2 3 4 5
赞(0) 打赏
未经允许不得转载:运维那些事 » linux文件管理命令与命令行展开

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续给力更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏