0?前言
在csdn技能樹Linux入門的練習題中,touch是最常見的一條命令。這次我們就來研究它的用法。
1 touch命令的功能、格式和選項說明
我們可以使用touch --help命令查看touch命令的幫助信息。
[purpleendurer @ bash ~ ]touch --help
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 referencedfile (useful only on systems that can change thetimestamps 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 -aWORD is modify or mtime: equivalent to -m--help display this help and exit--version output version information and exitNote that the -d and -t options accept different time-date formats.GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Report touch translation bugs to <http://translationproject.org/team/>
For complete documentation, run: info coreutils 'touch invocation'
[purpleendurer @ bash ~ ]
1.1 touch命令的功能
touch命令可以將指定文件的訪問時間或修改時間更新為當前時間,
如果指定的文件不存在,touch命令也可以創建出新文件。
1.2?touch命令的格式
touch [選項] 文件1 [[文件2] ……]
1.3??touch命令的選項
選項 | 功能 |
---|---|
-a | 只更改訪問時間 |
-c --no-create | 不創建文件 |
-d=STRING --date=STRING | 解析 STRING 并使用它來代替當前時間 |
-f | 可以忽略不使用,是為了與其他 unix 系統的相容性而保留 |
-h --no-dereference | 影響每個符號鏈接,而不是任何引用的文件(僅在可以更改符號鏈接時間戳的系統上有用) |
-m | 僅更改修改時間 |
-r=FILE --reference=FILE | 使用指定文件的時間而不是當前時間 |
-t STAMP | 使用 [[CC]YY]MMDDhhmm[.ss] 代替當前時間 其中: CC 為年份前兩位數字 YY 為年份后兩位數字 MM 為月份 DD 為日 hh 為小時 mm 為分鐘 ss 為秒數 |
--time=WORD | 更改指定時間: WORD 是 access、atime 或 use:等價于 -a WORD 是 modify 或 mtime:相當于 -m |
--help | 顯示幫助信息并退出 |
--version | 輸出版本信息并退出 |
?2 touch命令實例
2.1 touch test1.txt : 創建文件test1.txt
[purpleendurer @ bash ~ ]ls -l
total 4
drwxr-xr-x 2 csdn csdn 4096 8月 3 2021 Code
[purpleendurer @ bash ~ ]touch test1.txt
[purpleendurer @ bash ~ ]ls -l
total 4
drwxr-xr-x 2 csdn csdn 4096 8月 3 2021 Code
-rw-rw-r-- 1 csdn csdn 0 6月 29 18:25 test1.txt
[purpleendurer @ bash ~ ]
我們先使用命令 ls -l 查看當前目錄的內容,當前目錄中沒有文件test1.txt。
所以我們執行命令touch test1.txt時,會自動創建這個文件。
2.2?touch -t 198001020304.05 test1.txt :將文件test1.txt的訪問或修改時間改為 1980年1月2日3點4分05秒
[purpleendurer @ bash ~ ]ls -l
total 4
drwxr-xr-x 2 csdn csdn 4096 8月 3 2021 Code
-rw-rw-r-- 1 csdn csdn 0 6月 29 18:34 test1.txt
[purpleendurer @ bash ~ ]touch -t 198001020304.05 test1.txt
[purpleendurer @ bash ~ ]ls -l
total 4
drwxr-xr-x 2 csdn csdn 4096 8月 3 2021 Code
-rw-rw-r-- 1 csdn csdn 0 1月 2 1980 test1.txt
[purpleendurer @ bash ~ ]
我們先使用命令 ls -l 查看當前目錄的內容,其中test1.txt的日期是6月29日 18:34。
接著我們執行命令touch -t 198001020304.05 test1.txt
再使用命令 ls -l 查看當前目錄的內容,其中test1.txt的日期是已變成1980年1月2日 。
2.3?touch --r=test1.txt test2.txt :將文件test2.txt的訪問或修改時間改為 test1.txt的時間
[purpleendurer @ bash ~ ]ls -l
total 4
drwxr-xr-x 2 csdn csdn 4096 8月 3 2021 Code
-rw-rw-r-- 1 csdn csdn 0 1月 2 1980 test1.txt
[purpleendurer @ bash ~ ]ls -l
total 4
drwxr-xr-x 2 csdn csdn 4096 8月 3 2021 Code
-rw-rw-r-- 1 csdn csdn 0 1月 2 1980 test1.txt
[purpleendurer @ bash ~ ]touch test2.txt
[purpleendurer @ bash ~ ]ls -l
total 4
drwxr-xr-x 2 csdn csdn 4096 8月 3 2021 Code
-rw-rw-r-- 1 csdn csdn 0 1月 2 1980 test1.txt
-rw-rw-r-- 1 csdn csdn 0 6月 29 18:38 test2.txt
[purpleendurer @ bash ~ ]touch --r=test1.txt test2.txt
[purpleendurer @ bash ~ ]ls -l
total 4
drwxr-xr-x 2 csdn csdn 4096 8月 3 2021 Code
-rw-rw-r-- 1 csdn csdn 0 1月 2 1980 test1.txt
-rw-rw-r-- 1 csdn csdn 0 1月 2 1980 test2.txt
[purpleendurer @ bash ~ ]
我們先使用命令 ls -l 查看當前目錄的內容,其中文件test1.txt的日期是1980年1月2日,文件test2.txt的日期是6月29日 18:38。
接著我們執行命令touch --r=test1.txt test2.txt
再使用命令 ls -l 查看當前目錄的內容,其中test2.txt的日期已變成1980年1月2日,跟test1.txt一樣。
2.4?touch --date="2022-01-01 12:00:00" test2.txt :將文件test2.txt的時間改為 2022年1月1日
[purpleendurer @ bash ~ ]ls -l
total 4
drwxr-xr-x 2 csdn csdn 4096 8月 3 2021 Code
-rw-rw-r-- 1 csdn csdn 0 6月 29 18:55 test1.txt
-rw-rw-r-- 1 csdn csdn 0 6月 29 18:55 test2.txt
[purpleendurer @ bash ~ ]touch -d="2022-01-01 12:00:00" test2.txt
touch: invalid date format ‘=2022-01-01 12:00:00’
[purpleendurer @ bash ~ ]touch --date="2022-01-01 12:00:00" test2.txt
[purpleendurer @ bash ~ ]ls -l
total 4
drwxr-xr-x 2 csdn csdn 4096 8月 3 2021 Code
-rw-rw-r-- 1 csdn csdn 0 6月 29 18:55 test1.txt
-rw-rw-r-- 1 csdn csdn 0 1月 1 2022 test2.txt
[purpleendurer @ bash ~ ]
我們先使用命令 ls -l 查看當前目錄的內容,其中文件test2.txt的日期是6月29日 18:55。
接著我們執行命令touch --date="2022-01-01 12:00:00"
再使用命令 ls -l 查看當前目錄的內容,其中test2.txt的日期已變成2022年1月1日。