linux文件时间属性


1.文件时间概念

回到顶部
在linux中的文件有三种时间属性,分别是:
文件内容修改时间(modification time, mtime):文件内容被修改的时间。文件内容指的是文件中的内容,而不是文件的属性或权限。 cat命令显示的即为文本文件的内容。对文件内容修改便会更新该时间。 我们用vim等文本编辑工具编辑的就是文本文件中的内容,用vim编辑并保存文件后,文件的mtime就会发生变化。 通过ls –l列出的时间便是文件的最后修改时间mtime。
文件内容访问时间(access time,atime):文件内容被读取访问的时间。一旦文件内容被读取就会更新文件访问时间atime。 比如用cat命令读取文本文件后,就会更新文件访问时间atime。可以用ls -ul命令查看文件的访问时间atime。 注意用ls或者stat命令查看文件属性的时候,也是在访问文件,但是并没有访问读取文件的内容,只是读取了文件的属性信息,所以不会更新文件访问时间atime。
文件状态属性改变时间(status change time ctime):即文件属性更改时间,修改文件的属性便会更新文件状态时间ctime。 比如使用chmod命令更改文件属性后,文件的状态时间ctime就会被更新。

1.查看文件时间

回到顶部
ls -al命令默认显示的时间是mtime,可以通过--time选项指定要显示的文件时间。 --time=atime显示atime,--time=ctime显示ctime:
我们以/etc/manpath.config文件为例,展示如何查看文件的各种时间信息。
我们先用data命令查看当前时间:
                [root@initroot ~]# date;
                Wed Jan  8 20:52:17 CST 2020
              
用ls -l命令查看/etc/manpath.config文件内容的最后修改时间mtime:
                [root@initroot ~]# ls -l /etc/manpath.config
                -rw-r--r-- 1 root root 5174 Aug  5  2018 /etc/manpath.config
              
文件内容最后一次被修改的时间mtime为2018/Aug/5;
在ls -l的基础上加上--time=atime选项查看文件最后一次被访问读取的时间atime:
                [root@initroot ~]# ls -l --time=atime /etc/manpath.config
                -rw-r--r-- 1 root root 5174 Jan  8 19:29 /etc/manpath.config
              
这里没有显示年份,说明就是今年,通过上面的date命令我们知道今年是2020年, 所以文件最后一次被访问读取的时间atime为2020/jan/8 19:29;
在ls -l的基础上加上--time=ctime选项查看文件属性信息最后一次被修改的时间ctime:
                [root@initroot ~]# ls -l --time=ctime /etc/manpath.config
                -rw-r--r-- 1 root root 5174 Jul 10 20:09 /etc/manpath.config
              
文件属性信息最后一次被修改的时间ctime为2020/jul/10 20:09.
为了便于观察,我们将上述命令放在一起执行,命令之间用;号隔开。 shell会按照顺序执行命令。如果命令超过了一行,也可以用\将命令转接到下一行:
                [root@initroot ~]# date; ls -l /etc/manpath.config; ls -l --time=atime /etc/manpath.config; ls -l --time=ctime /etc/manpath.config
                Wed Jan  8 21:49:42 CST 2020   #当前系统时间
                -rw-r--r-- 1 root root 5174 Aug  5  2018 /etc/manpath.config  #mtime
                -rw-r--r-- 1 root root 5174 Jan  8 19:29 /etc/manpath.config  #atime
                -rw-r--r-- 1 root root 5174 Jul 10 20:09 /etc/manpath.config  #ctime
              
当然我们刚刚介绍的stat命令可以更方便的查看文件时间属性:
root@initroot:~# date; stat /etc/manpath.config 
Thu Jan  9 10:37:06 CST 2020
  File: /etc/manpath.config
  Size: 5174      	Blocks: 16         IO Block: 4096   regular file
Device: 801h/2049d	Inode: 917700      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-01-08 14:33:49.736210529 +0800
Modify: 2018-08-05 03:16:12.000000000 +0800
Change: 2019-11-18 16:56:30.116717065 +0800
 Birth: -
              

3.修改文件时间

回到顶部
touch命令用来更新文件的访问时间和修改时间, 默认情况下,如果touch后面接有文件参数,则该文件的三个时间都会更新到目前的系统时间。
如果touch命令后接的文件不存在,那么就创建一个空文件,所以touch命令经常被用来创建新的空白文件
可以用-d或者-t选项将文件的时间修改到指定的时间,使用-a选项只修改atime,使用-m选项只修改mtime
touch命令常用格式:
                [root@initroot ~]# touch [-acdmt] filepath
              
选项与参数:
-a :仅修订access time(atime);
-c :仅修改文件的时间,若该文件不存在则不建立新文件;
-d :后面可以接欲更新的日期而不用目前的日期,也可以使用 --date="日期或时间"
-m :仅修改mtime ;
-t :后面可以接欲更新的时间而不用目前的时间,格式为[YYYYMMDDhhmm]
touch会将文件的三个时间(atime/ctime/mtime)都更新到当前的系统时间。 如果touch命令后接的文件参数在当前工作目录下不存在,touch就会以该文件名创建一个新的空文件。 所以我们经常用touch命令创建一个新文件。 例如我们在/tmp目录下创建一个空文件并观察文件的时间:
              [peter@initroot ~]# cd /tmp
              [peter@initroot /tmp]# touch touchtest
              [peter@initroot /tmp]# ls -l touchtest
              -rw-r--r-- 1 peter peter 0 Jan 8 21:27 touchtest
              
可以看到文件大小是0,说明这是一个空文件。
用cp命令将~/.bashrc文件复制为/tmp/bashrc文件,加上-a选项复制源文件的所有属性信息:
[peter@initroot /tmp]# type ll
ll is aliased to `ls -alF'
[peter@initroot /tmp]# cp -a ~/.bashrc bashrc
[peter@initroot /tmp]# date; ll bashrc; ll --time=atime bashrc; ll --time=ctime bashrc
Wed Jan  8 21:57:27 CST 2020  #当前系统时间
-rw-r--r-- 1 root root 3106 Apr  9  2018 bashrc  #mtime
-rw-r--r-- 1 root root 3106 Jan  7 22:06 bashrc  #atime
-rw-r--r-- 1 root root 3106 Jan  8 21:56 bashrc  #ctime
通过type ll命令我们发现ll其实是ls -alF的命令别名。
通过上面的输出结果我们可以看出文件的内容与属性是被复制过来的,因此文件内容时间(mtime) 与原文件相同。但是由于这个文件是刚刚被建立的,因此状态(ctime)就变成现在的时间了!
将上面刚刚复制过来的bashrc文件的时间调整到当前系统时间:
root@initroot:/tmp# touch bashrc 
root@initroot:/tmp# date; ll bashrc; ll --time=atime bashrc; ll --time=ctime bashrc
Wed Jan  8 22:08:34 CST 2020  #当前系统时间
-rw-r--r-- 1 root root 3106 Jan  8 22:08 bashrc #mtime
-rw-r--r-- 1 root root 3106 Jan  8 22:08 bashrc #atime
-rw-r--r-- 1 root root 3106 Jan  8 22:08 bashrc #ctime
                
可以看到文件的三个时间都被同时更改到最新的系统时间了。
只变更文件的修改时间mtime:
peter@peter-VirtualBox:~/Desktop/test$ touch -m bashrc
peter@peter-VirtualBox:~/Desktop/test$ stat bashrc
  File: adtest.png
  Size: 20752     	Blocks: 48         IO Block: 4096   regular file
Device: 801h/2049d	Inode: 4876769     Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/   peter)   Gid: ( 1000/   peter)
Access: 2019-10-10 16:22:45.690963925 +0800
Modify: 2019-10-10 16:59:48.834963925 +0800
Change: 2019-10-10 16:59:48.834963925 +0800
 Birth: -
只变更文件的访问时间atime:
peter@peter-VirtualBox:~/Desktop/test$ touch -a bashrc

peter@peter-VirtualBox:~/Desktop/test$ stat adtest.png 
  File: adtest.png
  Size: 20752     	Blocks: 48         IO Block: 4096   regular file
Device: 801h/2049d	Inode: 4876769     Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/   peter)   Gid: ( 1000/   peter)
Access: 2019-10-10 16:22:45.690963925 +0800
Modify: 2019-10-10 16:59:48.834963925 +0800
Change: 2019-10-10 16:59:48.834963925 +0800
 Birth: -
将bashrc文件的时间调整到两天前:
root@initroot:/tmp# touch -d "2 days ago" bashrc
root@introot:/tmp# date; ll bashrc; ll --time=atime bashrc; ll --time=ctime bashrc
Wed Jan  8 22:10:53 CST 2020 #当前系统时间
-rw-r--r-- 1 root root 3106 Jan  6 22:10 bashrc #mtime
-rw-r--r-- 1 root root 3106 Jan  6 22:10 bashrc #atime
-rw-r--r-- 1 root root 3106 Jan  8 22:10 bashrc #ctime
可以看到文件的mtime和atime日期由原来的8号变成了6号,但是因为刚刚文件的属性信息发生了改变,所以ctime就更新到当前系统时间了。
将bashrc文件的时间改为2016/06/15 2:02
root@initroot:/tmp# touch -t 201606150202 bashrc
root@initroot:/tmp# date; ll bashrc; ll --time=atime bashrc; ll --time=ctime bashrc
Wed Jan  8 22:14:55 CST 2020  #当前系统时间
-rw-r--r-- 1 root root 3106 Jun 15  2016 bashrc  #mtime
-rw-r--r-- 1 root root 3106 Jun 15  2016 bashrc  #atime
-rw-r--r-- 1 root root 3106 Jan  8 22:14 bashrc  #ctime
可以看到文件的mtime和atime日期都设置成了指定的日期,但是因为刚刚文件的属性信息发生了改变,所以ctime就更新到当前系统时间了。
通过上面我们发现不论如何改变文件时间,mtime和atime都会按照指定的方式改变,但是ctime则总是变更到文件状态最后一次变动的时间。
其实我们平时最经常关注的文件时间就是mtime,其他时间则真的很少关注。总之touch命令最常用的两种情况就是:
1.新建一个空文件;
2.更新文件的时间(mtime、atime和ctime)

linux下文件的创建时间、访问时间、修改时间和改变时间 2017-12-20 22:24:29 运维自动化&云计算 阅读数 28596更多 分类专栏: LINUX 确切的说不存在创建时间。若文件从创建后不曾修改过则可认为创建时间=修改时间,若文件创建后状态也不曾改变过则可认为创建时间=改变时间,若文件创建后不曾被读取过则可认为创建时间=访问时间。但是,但是,上述情况基本上是不可能的,也就是说几乎不可能获取到文件的创建时间。 可以通过stat命令查看文件的状态 可以看到该文件的访问时间、修改时间、改变时间均为13:36:55。此处这一时间也是该文件的创建时间,因为文件创建后没有发生过访问、修改和改变。 Ø 访问时间(accesstime):读取一次文件的内容,该时间便会更新。比如对这个文件使用less命令或者more命令。(ls、stat这样的命令不会修改文件访问时间) 可以看到对文件使用more\less\cat命令后文件的访问时间分别变化为最后一次访问的时间,而文件的修改时间和改变时间并没有发生变化。 可以看到,对文件使用ls\stat命令列出文件属性\显示文件状态后文件的访问时间并没有发生变化,这是因为这些过程并没有读取文件内容本身。 Ø 修改时间(modifytime):对文件内容修改一次便会更新该时间。例如使用vim等工具更改了文件内容并保存后,文件修改时间发生变化。通过ls –l列出的时间便是这个时间。要想看到文件访问时间可使用ls –ul命令。 当前的文件状态。 使用vim编辑文件添加了“test file access time,modify time,change time”后保存文件,查看文件的状态,可以看到文件的修改时间发生了变化。但值得注意的是文件的访问时间和改变时间也发生了变化。这是问什么呢?这是因为我们通过vim打开文件实际上也是访问了文件因此文件访问时间会更新,而当添加了内容并保存后文件的大小、块数、时间数据等状态也发生了变化,因此其状态改变时间也被更新。也就是说修改文件内容往往也会更新文件的状态改变时间和访问时间,但不是绝对的。后边的例子会讲解这种情况。 这一次我们只通过vim打开文件,不进行任何编辑再保存文件,查看得知文件的访问时间发生了变化而修改时间和状态改变时间并没有被更新。这印证了通过vim编辑文件实际上是先访问了文件的说法。另外也说明只要其内容没有发生变化,那么文件的修改时间也保持不变。 ls –l命令显示文件的修改时间,ls –ul命令显示文件的访问时间。 Ø 改变时间(changetime):更改文件的属性便会更新该时间,比如使用chmod命令更改文件属性,或者执行其他命令时隐式的附带更改了文件的属性若文件大小等。 当前的访问时间、修改时间、状态变化时间。 执行完chmod命令后查看文件状态发现状态变化时间被更新。访问时间和修改时间保持不变,因为这一过程并没有读取文件内容也没改变文件内容 通过重定向命令来改变文件内容会更新文件的修改时间和状态改变时间。因为这一过程修改了文件内容,改变了文件大小、修改时间等状态,而没有读取文件的内容。这也验证了“修改文件内容往往也会更新文件的状态改变时间和访问时间,但不是绝对的。”这一说法。 Linux下查看和修改文件时间 一 、查看文件的时间及相关命令 1、stat 查看文件时间 [root@linux-node1 ~]# stat lnmp-install.log File: `lnmp-install.log' Size: 2228418 Blocks: 4368 IO Block: 4096 regular file Device: fd00h/64768d Inode: 1970147 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2019-04-18 16:03:58.903725603 +0800 Modify: 2016-05-15 15:02:15.836183263 +0800 Change: 2016-05-15 15:02:15.836183263 +0800 说明:Access:访问时间 Modify:修改时间 Change:改变时间 ,可以使用stat * 查看这个目录所有文件的状态。 而我们想要查看某文件的三个时间中的具体某个时间,并以年月日时分秒的格式保存。我们可以使用下面的命令: [root@linux-node1 ~]# stat lnmp-install.log |grep -i Access | awk -F. '{print $1}' | awk '{print $2$3}'| awk -F- '{print $1$2$3}' | awk -F: '{print $1$2$3}' |grep -v 'rwr' 20190418160358 [root@linux-node1 ~]# stat lnmp-install.log |grep -i Modify | awk -F. '{print $1}' | awk '{print $2$3}'| awk -F- '{print $1$2$3}' | awk -F: '{print $1$2$3}' 20160515150215 [root@linux-node1 ~]# stat lnmp-install.log |grep -i Change | awk -F. '{print $1}' | awk '{print $2$3}'| awk -F- '{print $1$2$3}' | awk -F: '{print $1$2$3}' 20160515150215   2、ls查看文件时间 相应的通过ls 查看时也有三个时间: 1 2 3 • modification time(mtime,修改时间):当该文件的“内容数据”更改时,就会更新这个时间。内容数据指的是文件的内容,而不是文件的属性。 • status time(ctime,状态时间):当该文件的”状态(status)”改变时,就会更新这个时间,举例来说,更改了权限与属性,就会更新这个时间。 • access time(atime,存取时间):当“取用文件内容”时,就会更新这个读取时间。举例来说,使用cat去读取 ~/.bashrc,就会更新atime了。 1 2 3 4 [root@linux-node1 ~]# ls -l --time=ctime lnmp-install.log -rw-r--r--. 1 root root 2228418 May 15 2016 lnmp-install.log [root@linux-node1 ~]# ls -l --time=atime lnmp-install.log -rw-r--r--. 1 root root 2228418 Apr 18 16:03 lnmp-install.log 注意:ls 参数里没有--time=mtime 这个参数,因为我们默认通过ls -l 查看到的时间就是mtime . 二、修改文件时间 创建文件我们可以通过touch命令来创建,同样的,我们也可以使用touch命令来修改文件时间。touch 的相关参数如下: -a : 仅修改access time -c :仅修改时间,而不建立文件 -d :后面可以接日期,也可以使用 --date="日期或时间"  -m :仅修改mtime -t :后面可以接时间,格式为 [YYMMDDhhmm] 注意:如果touch后面接一个已经存在的文件,则该文件的3个时间(atime/ctime/mtime)都会更新为当前时间。若该文件不存在,则会主动建立一个新的空文件。 [root@linux-node1 ~]# ll -rw-r--r--. 1 root root 9214 Apr 24 2016 install.log -rw-r--r--. 1 root root 3091 Apr 24 2016 install.log.syslog -rw-r--r--. 1 root root 2228418 May 15 2016 lnmp-install.log [root@linux-node1 ~]# touch install.log [root@linux-node1 ~]# stat install.log File: `install.log' Size: 9214 Blocks: 24 IO Block: 4096 regular file Device: fd00h/64768d Inode: 1962242 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2019-04-18 16:20:22.729724898 +0800 Modify: 2019-04-18 16:20:22.729724898 +0800 Change: 2019-04-18 16:20:22.729724898 +0800 同样的,使用ls,查看到的结果也是一样: [root@linux-node1 ~]# ls -l --time=ctime install.log -rw-r--r--. 1 root root 9214 Apr 18 16:20 install.log [root@linux-node1 ~]# ls -l --time=atime install.log -rw-r--r--. 1 root root 9214 Apr 18 16:20 install.log [root@linux-node1 ~]# ls -l install.log -rw-r--r--. 1 root root 9214 Apr 18 16:20 install.log 利用touch修改文件时间: 1. 同时变更文件的修改时间和访问时间 [root@linux-node1 ~]# touch -d "2018-04-18 08:00:00" install.log [root@linux-node1 ~]# stat install.log File: `install.log' Size: 9214 Blocks: 24 IO Block: 4096 regular file Device: fd00h/64768d Inode: 1962242 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2018-04-18 08:00:00.000000000 +0800 Modify: 2018-04-18 08:00:00.000000000 +0800 Change: 2019-04-18 16:32:35.947725358 +0800 2. 只变更文件的修改时间 [root@linux-node1 ~]# touch -m -d "2018-05-20 08:00:00" install.log [root@linux-node1 ~]# stat install.log File: `install.log' Size: 9214 Blocks: 24 IO Block: 4096 regular file Device: fd00h/64768d Inode: 1962242 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2018-04-18 08:00:00.000000000 +0800 Modify: 2018-05-20 08:00:00.000000000 +0800 Change: 2019-04-18 16:36:06.617725402 +0800 3. 只变更文件的访问时间 [root@linux-node1 ~]# touch -a -d "2017-05-10 09:00:00" install.log [root@linux-node1 ~]# stat install.log File: `install.log' Size: 9214 Blocks: 24 IO Block: 4096 regular file Device: fd00h/64768d Inode: 1962242 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2017-05-10 09:00:00.000000000 +0800 Modify: 2018-05-20 08:00:00.000000000 +0800 Change: 2019-04-18 16:38:09.851725390 +0800 将一个文件的时间修改为同另一个文件的时间相同 [root@linux-node1 ~]# ls -l anaconda-ks.cfg -rw-------. 1 root root 1097 Apr 24 2016 anaconda-ks.cfg [root@linux-node1 ~]# touch -acmr anaconda-ks.cfg install.log [root@linux-node1 ~]# stat install.log File: `install.log' Size: 9214 Blocks: 24 IO Block: 4096 regular file Device: fd00h/64768d Inode: 1962242 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2016-04-24 22:21:38.363999825 +0800 Modify: 2016-04-24 22:21:39.114999827 +0800 Change: 2019-04-18 16:40:09.464725644 +0800 ## 将install.log文件的时间修改成和anaconda-ks.cfg ## 另外touch 还支持像date 命令一样的参数修改文件时间: [root@linux-node1 ~]# stat install.log File: `install.log' Size: 9214 Blocks: 24 IO Block: 4096 regular file Device: fd00h/64768d Inode: 1962242 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2016-04-24 22:21:38.363999825 +0800 Modify: 2016-04-24 22:21:39.114999827 +0800 Change: 2019-04-18 16:40:09.464725644 +0800 [root@linux-node1 ~]# touch -d "3 days ago" install.log;ls -l install.log -rw-r--r--. 1 root root 9214 Apr 15 16:47 install.log 总结一下常用的文件操作与时间的关系: 1、访问时间,读一次这个文件的内容,这个时间就会更新。比如对这个文件使用more命令。ls、stat命令都不会修改文件的访问时间。 2、修改时间,对文件内容修改一次,这个时间就会更新。比如:vim后保存文件。ls -l列出的时间就是这个时间。 3、状态改变时间。通过chmod命令更改一次文件属性,这个时间就会更新。查看文件的详细的状态、准确的修改时间等,可以通过stat命令+文件名。 

touch命令用来更新文件的访问时间和修改时间,在linux中的文件有三个时间属性,分别是:
访问时间(access time,atime):当文件的内容被访问读取,就会更新访问时间。比如对文件使用less命令或者more命令。 ls、stat这样的命令不会修改文件访问时间
修改时间(modification time, mtime):对文件内容被修改便会更新该时间。文件内容指的的是文件中的内容,而不是文件的属性或权限。 例如使用vim等工具更改了文件内容并保存后,文件修改时间发生变化。通过ls –l列出的时间便是这个时间。要想看到文件访问时间可使用ls –ul命令。
状态改变时间(status change time ctime):更改文件的属性便会更新该时间,比如使用chmod命令更改文件属性, 或者执行其他命令时隐式的附带更改了文件的属性若文件大小等。
更多关于文件时间的解析
默认情况下,如果touch后面接有文件参数,则该文件的三个时间都会更新到目前的系统时间。
如果touch命令后接的文件不存在,那么就创建一个空文件,所以touch命令经常被用来创建新的空白文件
可以用-d或者-t选项将文件的时间修改到指定的时间,使用-a选项只修改atime,使用-m选项只修改mtime

命令常用示例:
如果当前工作目录中没有test.txt,创建一个新文件,否则将test.txt的时间更新到当前时间:

touch test.txt

1. 同时变更文件的修改时间和访问时间

peter@peter-VirtualBox:~/Desktop/test$ stat adtest.png 
  File: adtest.png
  Size: 20752     	Blocks: 48         IO Block: 4096   regular file
Device: 801h/2049d	Inode: 4876769     Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/   peter)   Gid: ( 1000/   peter)
Access: 2019-10-10 16:22:10.470963925 +0800
Modify: 2019-10-10 16:22:10.470963925 +0800
Change: 2019-10-10 16:22:10.470963925 +0800
 Birth: -

peter@peter-VirtualBox:~/Desktop/test$ date
Thu Oct 10 16:22:43 CST 2019

peter@peter-VirtualBox:~/Desktop/test$ touch adtest.png 

peter@peter-VirtualBox:~/Desktop/test$ stat adtest.png 
  File: adtest.png
  Size: 20752     	Blocks: 48         IO Block: 4096   regular file
Device: 801h/2049d	Inode: 4876769     Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/   peter)   Gid: ( 1000/   peter)
Access: 2019-10-10 16:22:45.690963925 +0800
Modify: 2019-10-10 16:22:45.690963925 +0800
Change: 2019-10-10 16:22:45.690963925 +0800
 Birth: -

2. 只变更文件的修改时间

peter@peter-VirtualBox:~/Desktop/test$ stat adtest.png 
  File: adtest.png
  Size: 20752     	Blocks: 48         IO Block: 4096   regular file
Device: 801h/2049d	Inode: 4876769     Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/   peter)   Gid: ( 1000/   peter)
Access: 2019-10-10 16:22:45.690963925 +0800
Modify: 2019-10-10 16:58:57.334963925 +0800
Change: 2019-10-10 16:58:57.334963925 +0800
 Birth: -

peter@peter-VirtualBox:~/Desktop/test$ touch -m adtest.png

peter@peter-VirtualBox:~/Desktop/test$ stat adtest.png 
  File: adtest.png
  Size: 20752     	Blocks: 48         IO Block: 4096   regular file
Device: 801h/2049d	Inode: 4876769     Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/   peter)   Gid: ( 1000/   peter)
Access: 2019-10-10 16:22:45.690963925 +0800
Modify: 2019-10-10 16:59:48.834963925 +0800
Change: 2019-10-10 16:59:48.834963925 +0800
 Birth: -

3. 只变更文件的访问时间

peter@peter-VirtualBox:~/Desktop/test$ stat adtest.png 
  File: adtest.png
  Size: 20752     	Blocks: 48         IO Block: 4096   regular file
Device: 801h/2049d	Inode: 4876769     Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/   peter)   Gid: ( 1000/   peter)
Access: 2019-10-10 16:22:45.690963925 +0800
Modify: 2019-10-10 16:58:57.334963925 +0800
Change: 2019-10-10 16:58:57.334963925 +0800
 Birth: -

peter@peter-VirtualBox:~/Desktop/test$ touch -m adtest.png

peter@peter-VirtualBox:~/Desktop/test$ stat adtest.png 
  File: adtest.png
  Size: 20752     	Blocks: 48         IO Block: 4096   regular file
Device: 801h/2049d	Inode: 4876769     Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/   peter)   Gid: ( 1000/   peter)
Access: 2019-10-10 16:22:45.690963925 +0800
Modify: 2019-10-10 16:59:48.834963925 +0800
Change: 2019-10-10 16:59:48.834963925 +0800
 Birth: -

Usage:
touch [OPTION]... FILE...
Update the access and modification times of each FILE to the current time.
A FILE argument that does not exist is created empty, unless -c or -h
is supplied.
A FILE argument string of - is handled specially and causes touch to
change the times of the file associated with standard output.
Mandatory arguments to long options are mandatory for short options too.
-a change only the access time
-c, --no-create do not create any files
-d, --date=STRING parse STRING and use it instead of current time
-f (ignored)
-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
-r, --reference=FILE use this file's times instead of current time
-t STAMP use [[CC]YY]MMDDhhmm[.ss] instead of current time
--time=WORD change the specified time:
WORD is access, atime, or use: equivalent to -a
WORD is modify or mtime: equivalent to -m
--help display this help and exit
--version output version information and exit
Note that the -d and -t options accept different time-date formats.
GNU coreutils online help: http://www.gnu.org/software/coreutils/ Full documentation at:
http://www.gnu.org/software/coreutils/touch or available locally via:
info '(coreutils) touch invocation'

initroot编辑整理,转载请注明www.initroot.com

100次点赞 100次阅读