touch 命令用于創(chuàng)建空文件,也可以更改 Unix 和 Linux 系統(tǒng)上現(xiàn)有文件時(shí)間戳。這里所說的更改時(shí)間戳意味著更新文件和目錄的訪問以及修改時(shí)間。
讓我們來看看 touch 命令的語法和選項(xiàng):
語法:
# touch {選項(xiàng)} {文件}
touch命令中使用的選項(xiàng):
在這篇文章中,我們將介紹 Linux 中 9 個(gè)有用的touch命令示例。
示例:1 使用 touch 創(chuàng)建一個(gè)空文件
要在 Linux 系統(tǒng)上使用touch命令創(chuàng)建空文件,鍵入touch,然后輸入文件名。如下所示:
[root@linuxtechi ~]# touch devops.txt
[root@linuxtechi ~]# ls -l devops.txt
-rw-r--r--.1root root0Mar2922:39devops.txt
示例:2 使用 touch 創(chuàng)建批量空文件
可能會出現(xiàn)一些情況,我們必須為某些測試創(chuàng)建大量空文件,這可以使用 touch 命令輕松實(shí)現(xiàn):
[root@linuxtechi ~]# touch sysadm-{1..20}.txt
在上面的例子中,我們創(chuàng)建了 20 個(gè)名為 sysadm-1.txt 到 sysadm-20.txt 的空文件,你可以根據(jù)需要更改名稱和數(shù)字。
示例:3 改變/更新文件和目錄的訪問時(shí)間
假設(shè)我們想要改變名為 devops.txt 文件的訪問時(shí)間,在 touch 命令中使用 -a 選項(xiàng),然后輸入文件名。如下所示:
[root@linuxtechi ~]# touch -a devops.txt
現(xiàn)在使用 stat 命令驗(yàn)證文件的訪問時(shí)間是否已更新:
[root@linuxtechi ~]# stat devops.txt
File: 'devops.txt'
Size: 0 Blocks: 0IO Block: 4096 regular empty file
Device: fd00h/64768dInode: 67324178Links: 1
Access: (0644/-rw-r--r--)Uid: (0/root) Gid: (0/root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2018-03-2923:03:10.902000000 -0400
Modify: 2018-03-2922:39:29.365000000 -0400
Change: 2018-03-2923:03:10.902000000 -0400
Birth: -
改變目錄的訪問時(shí)間:
假設(shè)我們在 /mnt 目錄下有一個(gè) nfsshare 文件夾,讓我們用下面的命令改變這個(gè)文件夾的訪問時(shí)間:
[root@linuxtechi ~]# touch -m /mnt/nfsshare/
[root@linuxtechi ~]# stat /mnt/nfsshare/
File: '/mnt/nfsshare/'
Size: 6Blocks: 0IO Block: 4096directory
Device: fd00h/64768dInode: 2258Links: 2
Access: (0755/drwxr-xr-x)Uid: (0/root)Gid: (0/root)
Context: unconfined_u:object_r:mnt_t:s0
Access: 2018-03-2923:34:38.095000000 -0400
Modify: 2018-03-0310:42:45.194000000 -0500
Change: 2018-03-2923:34:38.095000000 -0400
Birth: -
示例:4 更改訪問時(shí)間而不用創(chuàng)建新文件
在某些情況下,如果文件存在,我們希望更改文件的訪問時(shí)間,并避免創(chuàng)建文件。在 touch 命令中使用 -c 選項(xiàng)即可,如果文件存在,那么我們可以改變文件的訪問時(shí)間,如果不存在,我們不會創(chuàng)建它。
[root@linuxtechi ~]# touch -c sysadm-20.txt
[root@linuxtechi ~]# touch -c winadm-20.txt
[root@linuxtechi ~]# ls -l winadm-20.txt
ls: cannot access winadm-20.txt: No such file ordirectory
示例:5 更改文件和目錄的修改時(shí)間
在 touch 命令中使用 -m 選項(xiàng),我們可以更改文件和目錄的修改時(shí)間。
讓我們更改名為 devops.txt 文件的更改時(shí)間:
[root@linuxtechi ~]# touch -m devops.txt
現(xiàn)在使用 stat 命令來驗(yàn)證修改時(shí)間是否改變:
[root@linuxtechi ~]# stat devops.txt
File: 'devops.txt'
Size: 0Blocks: 0IO Block: 4096regular empty file
Device: fd00h/64768dInode: 67324178Links: 1
Access: (0644/-rw-r--r--)Uid: (0/root)Gid: (0/root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2018-03-2923:03:10.902000000 -0400
Modify: 2018-03-2923:59:49.106000000 -0400
Change: 2018-03-2923:59:49.106000000 -0400
Birth: -
同樣的,我們可以改變一個(gè)目錄的修改時(shí)間:
[root@linuxtechi ~]# touch -m /mnt/nfsshare/
使用 stat 交叉驗(yàn)證訪問和修改時(shí)間:
[root@linuxtechi ~]# stat devops.txt
File: 'devops.txt'
Size: 0Blocks: 0IO Block: 4096regular empty file
Device: fd00h/64768dInode: 67324178Links: 1
Access: (0644/-rw-r--r--)Uid: (0/root)Gid: (0/root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2018-03-3000:06:20.145000000 -0400
Modify: 2018-03-3000:06:20.145000000 -0400
Change: 2018-03-3000:06:20.145000000 -0400
Birth: -
示例:7 將訪問和修改時(shí)間設(shè)置為特定的日期和時(shí)間
每當(dāng)我們使用 touch 命令更改文件和目錄的訪問和修改時(shí)間時(shí),它將當(dāng)前時(shí)間設(shè)置為該文件或目錄的訪問和修改時(shí)間。
假設(shè)我們想要將特定的日期和時(shí)間設(shè)置為文件的訪問和修改時(shí)間,這可以使用 touch 命令中的 -c 和 -t 選項(xiàng)來實(shí)現(xiàn)。
日期和時(shí)間可以使用以下格式指定:
{CCYY}MMDDhhmm.ss
其中:
CC– 年份的前兩位數(shù)字
YY– 年份的后兩位數(shù)字
MM– 月份 (01-12)
DD– 天 (01-31)
hh– 小時(shí) (00-23)
mm– 分鐘 (00-59)
讓我們將devops.txt文件的訪問和修改時(shí)間設(shè)置為未來的一個(gè)時(shí)間(2025 年 10 月 19 日 18 時(shí) 20 分)。
[root@linuxtechi ~]# touch -c -t 202510191820 devops.txt
使用stat命令查看更新訪問和修改時(shí)間:
根據(jù)日期字符串設(shè)置訪問和修改時(shí)間,在touch命令中使用-d選項(xiàng),然后指定日期字符串,后面跟文件名。如下所示:
[root@linuxtechi ~]# touch -c -d "2010-02-07 20:15:12.000000000 +0530" sysadm-29.txt
使用 stat 命令驗(yàn)證文件的狀態(tài):
[root@linuxtechi ~]# stat sysadm-20.txt
File: ‘sysadm-20.txt’
Size: 0Blocks: 0IO Block: 4096regular empty file
Device: fd00h/64768dInode: 67324189Links: 1
Access: (0644/-rw-r--r--)Uid: (0/root)Gid: (0/root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2010-02-0720:15:12.000000000 +0530
Modify: 2010-02-0720:15:12.000000000 +0530
Change: 2018-03-3010:23:31.584000000 +0530
Birth: -
注意:在上述命令中,如果我們不指定 -c,如果系統(tǒng)中不存在該文件那么 touch 命令將創(chuàng)建一個(gè)新文件,并將時(shí)間戳設(shè)置為命令中給出的。
示例:8 使用參考文件設(shè)置時(shí)間戳(-r)
在 touch 命令中,我們可以使用參考文件來設(shè)置文件或目錄的時(shí)間戳。假設(shè)我想在 devops.txt 文件上設(shè)置與文件 sysadm-20.txt 文件相同的時(shí)間戳,touch 命令中使用 -r 選項(xiàng)可以輕松實(shí)現(xiàn)。
語法:
# touch -r {參考文件} 真正文件
[root@linuxtechi ~]# touch -r sysadm-20.txt devops.txt
示例:9 在符號鏈接文件上更改訪問和修改時(shí)間
默認(rèn)情況下,每當(dāng)我們嘗試使用 touch 命令更改符號鏈接文件的時(shí)間戳?xí)r,它只會更改原始文件的時(shí)間戳。如果你想更改符號鏈接文件的時(shí)間戳,則可以使用 touch 命令中的 -h 選項(xiàng)來實(shí)現(xiàn)。
語法:
# touch -h {符號鏈接文件}
[root@linuxtechi opt]# ls -l /root/linuxgeeks.txt
lrwxrwxrwx.1root root15Mar3010:56 /root/linuxgeeks.txt -> linuxadmins.txt
[root@linuxtechi ~]# touch -t 203010191820 -h linuxgeeks.txt
[root@linuxtechi ~]# ls -l linuxgeeks.txt
lrwxrwxrwx.1root root15Oct192030linuxgeeks.txt -> linuxadmins.txt
-
Linux
+關(guān)注
關(guān)注
87文章
11350瀏覽量
210459 -
touch
+關(guān)注
關(guān)注
0文章
68瀏覽量
34485
原文標(biāo)題:在 Linux 下 9 個(gè)有用的 touch 命令示例
文章出處:【微信號:LinuxHub,微信公眾號:Linux愛好者】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
相關(guān)推薦
評論