在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