touch:命令建立文件

touch:建立空文件或改變文件的時間戳屬性node

  【功能說明】linux

    touch命令有兩個功能:一是建立新的空文件;二是改變已有文件的時間戳屬性spa

  【語法格式】ci

touch  [option]  [file]字符串

touch  [選項]     [文件]io

      提示:table

      1. touch命令以及後面的選項和文件,每一個元素之間都至少要有一個空格
      2. 注意區分touch和mkdir命令的功能,mkdir命令是建立空目錄,而touch是建立空文件。
      3. 在linux中,一切皆文件。雖然touch命令不能建立目錄,可是能夠修改目錄的時間戳。

  【選項說明】模板

參數說明 解釋說明
-a 只更改指定文件的最後訪問時間
-d 使用字符串STRING表明的時間做爲模板設置指定文件的時間屬性
-m 只更改指定文件的最後修改時間
-r file 將指定文件的時間屬性設置爲與模板文件file的時間屬性相同
-t STAMP 使用[[CC]YY]MMDDhhmm[.ss]格式的時間設置文件的時間屬性。格式的含義從左到右依次爲:世紀、年、月、日、時、分、秒

  【使用範例】test

    基礎範例基礎

      案例1:建立文件示例(文件事先不存在的狀況)

[root@1-230 ~]# mkdir /test      #在根下新建一個test目錄
[root@1-230 ~]# cd /test/        #切換到/text目錄
[root@1-230 test]# touch aa.txt      #建立空文件aa.txt
[root@1-230 test]# ls           #查看到aa.txt文件建立成功
aa.txt
[root@1-230 test]# touch a.txt b.txt    #同時建立多個文件,相似mkdir建立多個目錄
[root@1-230 test]# ls
aa.txt a.txt b.txt
[root@1-230 test]# touch scc{1..5}    #能夠利用大括號「{ }」輸出的字符序列批量建立文件
[root@1-230 test]# ls
aa.txt a.txt b.txt scc1 scc2 scc3 scc4 scc5

      案例2:更改文件的時間戳屬性

[root@1-230 test]# stat aa.txt         #stat命令能夠查看文件的時間戳屬性,具體用法見後面stat命令
文件:"aa.txt"
大小:0 塊:0 IO 塊:4096 普通空文件
設備:fd00h/64768d Inode:35313153 硬連接:1
權限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
最近訪問:2018-10-30 23:11:37.065471839 +0800
最近更改:2018-10-30 23:11:37.065471839 +0800
最近改動:2018-10-30 23:11:37.065471839 +0800
建立時間:-

提示:

  說明:文件的時間戳屬性分爲訪問時間、修改時間、狀態改變時間


[root@1-230 test]# touch -a aa.txt       #-a 參數更改最後訪問的時間
[root@1-230 test]# stat aa.txt
文件:"aa.txt"
大小:0 塊:0 IO 塊:4096 普通空文件
設備:fd00h/64768d Inode:35313153 硬連接:1
權限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
最近訪問:2018-10-30 23:17:06.329316946 +0800
最近更改:2018-10-30 23:11:37.065471839 +0800
最近改動:2018-10-30 23:17:06.329316946 +0800
建立時間:-

 

[root@1-230 test]# touch -m aa.txt     #-m參數更改最後修改的時間
[root@1-230 test]# stat aa.txt
文件:"aa.txt"
大小:0 塊:0 IO 塊:4096 普通空文件
設備:fd00h/64768d Inode:35313153 硬連接:1
權限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
最近訪問:2018-10-30 23:17:06.329316946 +0800
最近更改:2018-10-30 23:20:26.211938858 +0800
最近改動:2018-10-30 23:20:26.211938858 +0800
建立時間:-

      案例3:指定時間屬性建立/修改文件

      可利用選項-d指定建立文件後的文件修改時間:

[root@1-230 test]# ls -lh aa.txt             #修改前的文件修改時間10月30日
-rw-r--r-- 1 root root 0 10月 30 23:20 aa.txt
[root@1-230 test]# touch -d 20181129 aa.txt      #指定建立文件後的文件修改時間爲2018年11月29日
[root@1-230 test]# ls -lh aa.txt             #修改後時間爲2018年11月29日
-rw-r--r-- 1 root root 0 11月 29 2018 aa.txt

      也能夠利用選項-r,修改aa.txt的時間屬性,使其和a.txt的時間屬性一致:

[root@1-230 test]# ls -lh a.txt         #查看a.txt的修改時間
-rw-r--r-- 1 root root 0 10月 30 23:11 a.txt
[root@1-230 test]# touch -r a.txt aa.txt     #使用-r參數讓a.txt的時間屬性和a.txt一致
[root@1-230 test]# ls -lh aa.txt
-rw-r--r-- 1 root root 0 10月 30 23:11 aa.txt    #aa.txt文件的修改時間和a.txt一致

      還能夠利用選項-t,將文件設置爲201810312333.50時間格式

[root@1-230 test]# touch -t 201810312333.50 aa.txt
[root@1-230 test]# ls -lh --full-time aa.txt
-rw-r--r-- 1 root root 0 2018-10-31 23:33:50.000000000 +0800 aa.txt

查看設置的屬性

  【擴展知識】

    這裏擴展一點有關時間戳屬性的知識

    GNU/Linux的文件有三種類型的時間戳:

Access: 2018-10-31 23:33:50.000000000 +0800    #最後訪問文件的時間
Modify: 2018-10-31 23:33:50.000000000 +0800    #最後修改文件的時間
Change: 2018-10-30 23:35:05.120111040 +0800    #最後改變文件狀態的時間

    對應ls命令,查看上述時間戳的選項以下:

mtime:最後修改時間(ls   -lt)  #修改文件內容,文件的修改時間(modify  time)會改變

ctime:狀態改變時間(ls     -lc)#修改文件內容,移動文件或改變文件屬性等,文件的change時間會改變

atime:最後訪問時間(ls    -lu) #查看文件內容時,文件的訪問時間(access   time)會改變

相關文章
相關標籤/搜索