新增文件最常见的是用touch命令
用法 : touch [OPTION]… FILE…
把每个文件的访问时间和修改时间更新为当前时间
如果文件不存在,且没有用 -c 或-h 选项,则创建一个空文件,
[root@chiji51 ~]# ls
anaconda-ks.cfg Desktop Downloads Music Public Videos
a.txt Documents initial-setup-ks.cfg Pictures Templates
[root@chiji51 ~]# touch b.txt
[root@chiji51 ~]# ls -l b.txt
-rw-r--r--. 1 root root 0 May 12 19:43 b.txt
-a 把访问时间改成当前时间
[root@chiji51 ~]# stat a.txt
Access: 2017-05-02 00:00:00.000000000 +0800
Modify: 2017-05-02 00:00:00.000000000 +0800
Change: 2019-05-12 18:24:02.604285661 +0800
[root@chiji51 ~]# touch -a a.txt
[root@chiji51 ~]# stat a.txt
Access: 2019-05-12 19:46:58.199585895 +0800 此处时间已经修改为当前时间
Modify: 2017-05-02 00:00:00.000000000 +0800
Change: 2019-05-12 19:46:58.199585895 +0800
-c, --no-create 不会创建任何文件
[root@chiji51 ~]# ls
anaconda-ks.cfg b.txt Documents initial-setup-ks.cfg Pictures Templates
a.txt Desktop Downloads Music Public Videos
[root@chiji51 ~]# touch -c c.txt
[root@chiji51 ~]# ls
anaconda-ks.cfg b.txt Documents initial-setup-ks.cfg Pictures Templates
a.txt Desktop Downloads Music Public Videos
当前目录没有c.txt
-d, --date=STRING 把atime 和mtime 时间改成STRING对应的时间,而不是当前时间
stat a.txt
Access: 2019-05-12 19:46:58.199585895 +0800
Modify: 2017-05-02 00:00:00.000000000 +0800
Change: 2019-05-12 19:46:58.199585895 +0800
[root@chiji51 ~]# touch -d "20170512" a.txt
[root@chiji51 ~]# stat a.txt
Access: 2017-05-12 00:00:00.000000000 +0800 此时时间已经变成修改的时间
Modify: 2017-05-12 00:00:00.000000000 +0800 此时时间已经变成修改的时间
Change: 2019-05-12 19:52:00.054604109 +0800
-h, --no-dereference 将改变符号链接文件而不是实际的文件,不加-h将会改变符号链接文件和实际文件
[root@chiji51 ~]# ln -s Desktop/ desktop
[root@chiji51 ~]# ls -ld Desktop desktop
lrwxrwxrwx. 1 root root 8 May 12 19:57 desktop -> Desktop/
drwxr-xr-x. 2 root root 6 May 12 19:33 Desktop
[root@chiji51 ~]# stat desktop
Access: 2019-05-12 19:57:33.674624240 +0800
Modify: 2019-05-12 19:57:32.662624179 +0800
Change: 2019-05-12 19:57:32.662624179 +0800
[root@chiji51 ~]# stat Desktop
Access: 2019-05-12 19:42:31.991569831 +0800
Modify: 2019-05-12 19:33:09.247535875 +0800
Change: 2019-05-12 19:33:09.247535875 +0800
[root@chiji51 ~]# touch -h desktop
[root@chiji51 ~]# stat desktop
Access: 2019-05-12 19:58:46.791628652 +0800
Modify: 2019-05-12 19:58:45.774628591 +0800
Change: 2019-05-12 19:58:45.774628591 +0800
此时链接文件的时间已经更改了
[root@chiji51 ~]# stat Desktop
Access: 2019-05-12 19:42:31.991569831 +0800
Modify: 2019-05-12 19:33:09.247535875 +0800
Change: 2019-05-12 19:33:09.247535875 +0800
-m 把修改时间改成当前时间
[root@chiji51 ~]# stat a.txt
Access: 2017-05-12 00:00:00.000000000 +0800
Modify: 2017-05-12 00:00:00.000000000 +0800
Change: 2019-05-12 19:52:00.054604109 +0800
[root@chiji51 ~]# touch -m a.txt
[root@chiji51 ~]# stat a.txt
Access: 2017-05-12 00:00:00.000000000 +0800
Modify: 2019-05-12 20:09:21.341666942 +0800 此时时间已经变成当前的时间
Change: 2019-05-12 20:09:21.341666942 +0800
-r, --reference=FILE 对照该文件的时间,把另外一个文件的时间改成该文件的时间
[root@chiji51 ~]# stat a.txt
Access: 2017-05-12 00:00:00.000000000 +0800
Modify: 2019-05-12 20:09:21.341666942 +0800
Change: 2019-05-12 20:09:21.341666942 +0800
[root@chiji51 ~]# stat b.txt
Access: 2019-05-12 19:43:33.903573567 +0800
Modify: 2019-05-12 19:43:33.903573567 +0800
Change: 2019-05-12 19:43:33.903573567 +0800
Birth: -
[ root@chiji51 ~]# touch -r a.txt b.txt
[root@chiji51 ~]# stat b.txt
Access: 2017-05-12 00:00:00.000000000 +0800
Modify: 2019-05-12 20:09:21.341666942 +0800
Change: 2019-05-12 20:12:17.453677568 +0800
Birth: -
-t STAMP 把[[CC]YY]MMDDhhmm[.ss] instead of current time
这个与-d参数的不同是年份和秒钟可以省略
[root@chiji51 ~]# stat b.txt
Access: 1805-02-04 05:00:00.000000000 +0805
Modify: 1805-02-04 05:00:00.000000000 +0805
Change: 2019-05-12 20:21:01.117709167 +0800
[root@chiji51 ~]# touch -t "05020405" b.txt
[root@chiji51 ~]# stat b.txt
Access: 2019-05-02 04:05:00.000000000 +0800
Modify: 2019-05-02 04:05:00.000000000 +0800
Change: 2019-05-12 20:22:15.637713664 +0800
–time=WORD 改变文件的时间
WORD的值有 - ‘atime’, ‘access’, ‘use’
- ‘mtime’, ‘modify’
如果WORD等于a,则相当于运行 -a参数的命令
[root@chiji51 ~]# touch --time=a b.txt
[root@chiji51 ~]# stat b.txt
Access: 2019-05-12 20:31:13.835746139 +0800
Modify: 2019-05-02 04:05:00.000000000 +0800
Change: 2019-05-12 20:31:13.835746139 +0800
如果WORD等于m,则相当于运行 -m参数的命令
[root@chiji51 ~]# touch --time=m b.txt
[root@chiji51 ~]# stat b.txt
Access: 2019-05-12 20:31:13.835746139 +0800
Modify: 2019-05-12 20:34:33.277758174 +0800
Change: 2019-05-12 20:34:33.277758174 +080