命令介紹
命令:touch 將每個文件的訪問和修改時間更新爲當前時間;文件不存在則創建
用法: touch [OPTION]… FILE… touch 選項 文件
命令選項
[root@fp-21 ~]# touch --help
-a # 只更改訪問時間( atime )
-c, --no-create # 不創建任何文件
-d, --date=STRING # 設置時間和日期,可以使用各種不同的格式
-f # 不使用(戶忽略)
-m # 只更改修修改時間( mtime )
-r, --reference=FILE # 使用參考文檔的時間記錄
-t STAMP # 使用"[[CC]YY]MMDDhhmm[.ss]"格式的時間替代當前時間
--help # 幫助文檔
--version # 版本信息
命令實例
# 只更改訪問時間( atime )
[root@fp-21 tmp]# ll a.txt
-rw-r--r--. 1 tom jack 385 Feb 21 15:29 a.txt
[root@fp-21 tmp]# touch a.txt
[root@fp-21 tmp]# ll a.txt
-rw-r--r--. 1 tom jack 385 Feb 23 13:48 a.txt
# 不創建任何文件
[root@fp-21 tmp]# touch -c test_file
[root@fp-21 tmp]# ll test_file
ls: cannot access test_file: No such file or directory
# 使用"[[CC]YY]MMDDhhmm[.ss]"格式的時間替代當前時間
[root@fp-21 tmp]# touch -t "202001231351" test_file
[root@fp-21 tmp]# ll test_file
-rw-r--r--. 1 root root 0 Jan 23 13:51 test_file
# 創建多個文件
[root@fp-21 opt]# touch test{1..3}.txt
[root@fp-21 opt]# ll
-rw-r--r--. 1 root root 0 Feb 23 15:00 test1.txt
-rw-r--r--. 1 root root 0 Feb 23 15:00 test2.txt
-rw-r--r--. 1 root root 0 Feb 23 15:00 test3.txt
# 混合創建多個文件
[root@fp-21 opt]# touch {a,b}.{txt,sql}
[root@fp-21 opt]# ll
-rw-r--r--. 1 root root 0 Feb 23 15:01 a.sql
-rw-r--r--. 1 root root 0 Feb 23 15:01 a.txt
-rw-r--r--. 1 root root 0 Feb 23 15:01 b.sql
-rw-r--r--. 1 root root 0 Feb 23 15:01 b.txt
注
文件的時間戳:
- atime:access time,上一次的訪問時間
- mtime:modify time,文件的修改時間
- ctime:change time,修改元數據時間( 重命名文件,更改權限,更改屬組、屬主,移動文件,修改文件內容 )
atime 只在文件被打開訪問時才改變,若不是打開文件編輯內容( 如重定向內容到文件中 ),則 ctime 和 mtime 的改變不會引起 atime 的改變。
mtime 的改變一定引起 ctime 的改變,而訪問文件時( cat、less、more ),atime 不一定會改變,所以 atime “改變” 不一定會影響ctime。
如何查看一個文件的時間戳
[root@fp-21 tmp]# stat test_file |sed -n 6,8p
Access: 2020-01-23 13:51:00.000000000 +0800
Modify: 2020-01-23 13:51:00.000000000 +0800
Change: 2020-02-23 13:52:08.086979833 +0800