linux命令--touch

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/smithereensman/article/details/51364864

touch


在linux中,touch的主要作用是创建不存在的文件或者改变已有文件的时间戳。
每个文件都有自己的时间戳,包括访问时间/存取时间(access time)修改时间(modify time),touch命令可以用来修改文件时间戳。让自己的文件更加整齐规范

  • 创建文件
[root@localhost test]# touch file{1..10}.txt
[root@localhost test]# ls
file10.txt  file2.txt  file4.txt  file6.txt  file8.txt
file1.txt   file3.txt  file5.txt  file7.txt  file9.txt
  • 修改时间戳
[root@localhost test]# ls -l file1.txt
-rw-r--r--. 1 root root 0 May 10 11:45 file1.txt
[root@localhost test]# echo "123"> file1.txt
[root@localhost test]# cat file1.txt
123
[root@localhost test]# touch file1.txt
[root@localhost test]# cat file1.txt
123
[root@localhost test]# ls -l file1.txt
-rw-r--r--. 1 root root 0 May 10 11:47 file1.txt
[root@localhost test]# stat file1.txt
  File: `file1.txt'
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: 802h/2050d  Inode: 12845069    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2016-05-10 11:47:37.313572609 -0400
Modify: 2016-05-10 11:47:37.313572609 -0400
Change: 2016-05-10 11:47:37.313572609 -0400

提示:touch一个已经存在的文件,会修改文件的时间戳,将原来文件的时间戳11:45改为了11:47(包括访问时间和修改时间以及状态改变时间(change time)),但是不会改变文件内容。

  • touch参数

-a 或–time=atime或–time=access或–time=use  只更改存取时间/访问时间。
-c 或–no-create  不建立任何文档。
-d  使用指定的日期时间,而非现在的时间。
-f  此参数将忽略不予处理,仅负责解决BSD版本touch指令的兼容性问题。
-m 或–time=mtime或–time=modify  只更改修改时间。
-r  把指定文档或目录的日期时间,统统设成和参考文档或目录的日期时间相同。
-t  使用指定的日期时间,而非现在的时间。

  • 更新访问时间(参数-a)
[root@localhost test]# touch -a file1.txt
[root@localhost test]# stat file1.txt
  File: `file1.txt'
  Size: 4           Blocks: 8          IO Block: 4096   regular file
Device: 802h/2050d  Inode: 12845069    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2016-05-10 12:42:52.415574001 -0400
Modify: 2016-05-10 12:31:40.145571990 -0400
Change: 2016-05-10 12:42:52.415574001 -0400
  • 更新修改时间(参数-m)
[root@localhost test]# touch -m file1.txt
[root@localhost test]# stat file1.txt
  File: `file1.txt'
  Size: 4           Blocks: 8          IO Block: 4096   regular file
Device: 802h/2050d  Inode: 12845069    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2016-05-10 12:42:52.415574001 -0400
Modify: 2016-05-10 12:46:10.966574061 -0400
Change: 2016-05-10 12:46:10.966574061 -0400

提示:状态改变时间(change time)会随着访问时间(access time)或修改时间(modify time)更新

  • 更细时间戳为指定时间(参数-t或者-d)
[root@localhost test]# touch -t 201605101645.33 file1.txt
[root@localhost test]# stat file1.txt
  File: `file1.txt'
  Size: 4           Blocks: 8          IO Block: 4096   regular file
Device: 802h/2050d  Inode: 12845069    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2016-05-10 16:45:33.000000000 -0400
Modify: 2016-05-10 16:45:33.000000000 -0400
Change: 2016-05-10 12:51:41.637573079 -0400
[root@localhost test]# touch -d '10-MAY-2013 17:00:11' file1.txt
[root@localhost test]# stat file1.txt
  File: `file1.txt'
  Size: 4           Blocks: 8          IO Block: 4096   regular file
Device: 802h/2050d  Inode: 12845069    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2013-05-10 17:00:11.000000000 -0400
Modify: 2013-05-10 17:00:11.000000000 -0400
Change: 2016-05-10 13:08:08.639572248 -0400
[root@localhost test]# touch -d 'a year ago 19:00:1' file1.txt
[root@localhost test]# stat file1.txt
  File: `file1.txt'
  Size: 4           Blocks: 8          IO Block: 4096   regular file
Device: 802h/2050d  Inode: 12845069    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2015-05-10 16:00:01.000000000 -0400
Modify: 2015-05-10 16:00:01.000000000 -0400
Change: 2016-05-10 13:09:41.633570947 -0400

[root@localhost test]# touch -d CST15 file1.txt
[root@localhost test]# stat file1.txt
  File: `file1.txt'
  Size: 4           Blocks: 8          IO Block: 4096   regular file
Device: 802h/2050d  Inode: 12845069    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2016-05-10 17:00:00.000000000 -0400
Modify: 2016-05-10 17:00:00.000000000 -0400
Change: 2016-05-10 12:59:52.207573643 -0400
[root@localhost test]# touch -d 'next Monday CST15' file1.txt
[root@localhost test]# stat file1.txt
  File: `file1.txt'
  Size: 4           Blocks: 8          IO Block: 4096   regular file
Device: 802h/2050d  Inode: 12845069    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2016-05-16 17:00:00.000000000 -0400
Modify: 2016-05-16 17:00:00.000000000 -0400
Change: 2016-05-10 13:01:15.396571448 -0400

-t参数的格式[[CC]YY]MMDDhhmm .[SS]。两个参数都不会更新状态改变时间戳

  • 将两个文件的时间戳同步(参数-r)
[root@localhost test]# stat file1.txt
  File: `file1.txt'
  Size: 4           Blocks: 8          IO Block: 4096   regular file
Device: 802h/2050d  Inode: 12845069    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2015-05-10 16:00:01.000000000 -0400
Modify: 2015-05-10 16:00:01.000000000 -0400
Change: 2016-05-10 13:09:41.633570947 -0400
[root@localhost test]# stat file2.txt
  File: `file2.txt'
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: 802h/2050d  Inode: 12845070    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2016-05-10 11:45:30.052578051 -0400
Modify: 2016-05-10 11:45:30.052578051 -0400
Change: 2016-05-10 11:45:30.052578051 -0400
[root@localhost test]# touch -r file1.txt file2.txt
[root@localhost test]# stat file2.txt
  File: `file2.txt'
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: 802h/2050d  Inode: 12845070    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2015-05-10 16:00:01.000000000 -0400
Modify: 2015-05-10 16:00:01.000000000 -0400
Change: 2016-05-10 13:11:21.579573112 -0400

猜你喜欢

转载自blog.csdn.net/smithereensman/article/details/51364864
今日推荐