溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Linux下touch命令怎么用

發(fā)布時(shí)間:2021-10-27 11:50:11 來源:億速云 閱讀:193 作者:小新 欄目:系統(tǒng)運(yùn)維

這篇文章給大家分享的是有關(guān)Linux下touch命令怎么用的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

touch 命令基本用法

提起 touch 命令,大家想到的肯定是它的兩個(gè)用法:

  • 改變時(shí)間戳

  • 創(chuàng)建新文件

這兩種用法大家在工作中早已用膩了,良許就不再贅述了。

防止創(chuàng)建文件

如果在 touch  后面直接跟上一個(gè)文件名,該文件如果不存在的話,將創(chuàng)建一個(gè)相應(yīng)名字的文件。那么如果我們只想改變文件的時(shí)間戳,如果文件不存在時(shí)不進(jìn)行文件創(chuàng)建該怎么做?這里需要加上  -c 選項(xiàng)。

[alvin@VM_0_16_centos test]$ touch -c alvin [alvin@VM_0_16_centos test]$ ll alvin ls: cannot access alvin: No such file or directory

僅改變文件訪問時(shí)間

我們知道,如果不帶任何選項(xiàng)執(zhí)行 touch 命令時(shí),文件的訪問時(shí)間及修改時(shí)間都是同時(shí)被改變成當(dāng)前系統(tǒng)時(shí)間。如下所示:

[alvin@VM_0_16_centos test]$ stat file   File: ‘file’   Size: 10              Blocks: 8          IO Block: 4096   regular file Device: fd01h/64769d    Inode: 371115      Links: 1 Access: (0664/-rw-rw-r--)  Uid: ( 1000/   alvin)   Gid: ( 1000/   alvin) Access: 2019-02-20 14:20:21.154819675 +0800 Modify: 2019-02-20 14:20:21.154819675 +0800 Change: 2019-02-20 14:20:21.191819649 +0800  Birth: - [alvin@VM_0_16_centos test]$ touch file        # 在這里使用 touch 命令 [alvin@VM_0_16_centos test]$ stat file   File: ‘file’   Size: 10              Blocks: 8          IO Block: 4096   regular file Device: fd01h/64769d    Inode: 371115      Links: 1 Access: (0664/-rw-rw-r--)  Uid: ( 1000/   alvin)   Gid: ( 1000/   alvin) Access: 2019-02-20 21:51:24.848774158 +0800        # 文件的訪問時(shí)間/修改時(shí)間均已改成當(dāng)前系統(tǒng)時(shí)間 Modify: 2019-02-20 21:51:24.848774158 +0800 Change: 2019-02-20 21:51:24.848774158 +0800  Birth: -

這里使用到 stat 命令,可以查看文件更詳細(xì)的信息。

如果我們只想改變文件的訪問時(shí)間,只需加上 -a 選項(xiàng)即可, a 即是單詞 access 的縮寫。

[alvin@VM_0_16_centos test]$ touch -a file [alvin@VM_0_16_centos test]$ stat file   File: ‘file’   Size: 10              Blocks: 8          IO Block: 4096   regular file Device: fd01h/64769d    Inode: 371115      Links: 1 Access: (0664/-rw-rw-r--)  Uid: ( 1000/   alvin)   Gid: ( 1000/   alvin) Access: 2019-02-20 21:56:40.858021859 +0800        # 只有訪問時(shí)間的時(shí)間戳被改變了 Modify: 2019-02-20 21:51:24.848774158 +0800        # 修改時(shí)間保持不變 Change: 2019-02-20 21:56:40.858021859 +0800  Birth: -

僅改變修改時(shí)間

如果我們只想改變文件的修改時(shí)間,只需加上 -m 選項(xiàng)即可, m 即是單詞 modify 的縮寫。

[alvin@VM_0_16_centos test]$ touch -m file [alvin@VM_0_16_centos test]$ stat file   File: ‘file’   Size: 10              Blocks: 8          IO Block: 4096   regular file Device: fd01h/64769d    Inode: 371115      Links: 1 Access: (0664/-rw-rw-r--)  Uid: ( 1000/   alvin)   Gid: ( 1000/   alvin) Access: 2019-02-20 21:56:40.858021859 +0800 Modify: 2019-02-20 22:07:39.138701655 +0800 Change: 2019-02-20 22:07:39.138701655 +0800  Birth: -

更改為自定義時(shí)間戳

不管是不帶選項(xiàng),還是帶上 -a 或 -m  選項(xiàng),都會(huì)將文件相應(yīng)的時(shí)間改為當(dāng)前系統(tǒng)時(shí)間戳。那如果我們想改為自定義的時(shí)間戳呢?要怎么處理?否則怎么算得上時(shí)光穿梭?

我們有兩種方法來更改為自定義時(shí)間戳。

1. 加上 -t 選項(xiàng)

比如我們將文件的時(shí)間戳改為一個(gè)將來時(shí)間:

[alvin@VM_0_16_centos test]$ touch -t 202001012020.20 file [alvin@VM_0_16_centos test]$ stat file   File: ‘file’   Size: 10              Blocks: 8          IO Block: 4096   regular file Device: fd01h/64769d    Inode: 371115      Links: 1 Access: (0664/-rw-rw-r--)  Uid: ( 1000/   alvin)   Gid: ( 1000/   alvin) Access: 2020-01-01 20:20:20.000000000 +0800 Modify: 2020-01-01 20:20:20.000000000 +0800 Change: 2019-02-20 22:13:01.526965566 +0800  Birth: -

在這里, -t 后面所帶的時(shí)間戳的格式為:

[[CC]YY]MMDDhhmm [.SS]

具體來講,是這樣的:

CC - 年份的前兩位  YY - 年份的后兩位  MM - 月份 [01-12] DD - 日期 [01-31] hh - 時(shí) [00-23] mm - 分 [00-59] SS - 秒 [00-61]

2. 加上 -d 選項(xiàng)

我們再用新方法將文件的時(shí)間戳改成一個(gè)過去的時(shí)間(2008年奧運(yùn)會(huì)開幕式):

[alvin@VM_0_16_centos test]$ touch -d '08-August-2008' file [alvin@VM_0_16_centos test]$ stat file   File: ‘file’   Size: 10              Blocks: 8          IO Block: 4096   regular file Device: fd01h/64769d    Inode: 371115      Links: 1 Access: (0664/-rw-rw-r--)  Uid: ( 1000/   alvin)   Gid: ( 1000/   alvin) Access: 2008-08-08 00:00:00.000000000 +0800 Modify: 2008-08-08 00:00:00.000000000 +0800 Change: 2019-02-20 22:25:47.808490725 +0800  Birth: -

在這里,時(shí)間的格式為:日-月-年 。但是,這里的時(shí)間可以相當(dāng)靈活,比如也支持 yesterday 、 1 year ago 等等模糊時(shí)間:

[alvin@VM_0_16_centos test]$ touch -d 'yesterday 08-August-2008' file [alvin@VM_0_16_centos test]$ stat file   File: ‘file’   Size: 10              Blocks: 8          IO Block: 4096   regular file Device: fd01h/64769d    Inode: 371115      Links: 1 Access: (0664/-rw-rw-r--)  Uid: ( 1000/   alvin)   Gid: ( 1000/   alvin) Access: 2008-08-07 00:00:00.000000000 +0800 Modify: 2008-08-07 00:00:00.000000000 +0800 Change: 2019-02-20 22:31:57.564725604 +0800  Birth: -

除了更改時(shí)間,它還可以改時(shí)區(qū)。

更改時(shí)區(qū),只需在 -d 后面跟上對應(yīng)的時(shí)區(qū)就可以。

感謝各位的閱讀!關(guān)于“Linux下touch命令怎么用”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI