3.每日一個Linux命令----touch

 

描述:建立一個新的文件,當被建立的文件存在時,則改變文件的時間戳。node

用法:touch [-acmdtr] 文件名vim

經常使用選項:code

-a:修改文件的訪問時間;文檔

-c:僅用來修改文件的時間,若是目標文件不存在,不會建立新的文件。與--no-create效果同樣;cmd

-m:修改文件的修改時間;object

-d:後面接日期,也能夠使用--date="日期或時間」;date

-t:後面接日期,格式爲[YYMMDDhhmm]。file

-r:把指定文檔或目錄的日期時間,通通設成和參考文檔或目錄的日期時間相同。im

示例:時間戳

1.建立三個空文件 a b c

[root@share21 ~]# touch a b c

2.建立三個空文件 file001 file002 file003

[root@share21 ~]# touch file{001,002,003}

3.若是file004文件不存在,則不建立文件,僅用來修改時間

[root@share21 ~]# touch -c file004
[root@share21 ~]# ll
total 0
-rw-r--r--. 1 root root 0 May 18 11:05 file001
-rw-r--r--. 1 root root 0 May 18 11:05 file002
-rw-r--r--. 1 root root 0 May 18 11:05 file00

4.建立file004,且將file004的時間戳設置的和file001同樣

[root@share21 ~]# touch file004
[root@share21 ~]# ll
total 0
-rw-r--r--. 1 root root 0 May 18 11:05 file001
-rw-r--r--. 1 root root 0 May 18 11:05 file002
-rw-r--r--. 1 root root 0 May 18 11:05 file003
-rw-r--r--. 1 root root 0 May 18 11:12 file004
[root@share21 ~]# touch -r file001 file004
[root@share21 ~]# ll
total 0
-rw-r--r--. 1 root root 0 May 18 11:05 file001
-rw-r--r--. 1 root root 0 May 18 11:05 file002
-rw-r--r--. 1 root root 0 May 18 11:05 file003
-rw-r--r--. 1 root root 0 May 18 11:05 file004
[root@share21 ~]#

5.同時修改文件的修改時間和訪問時間

[root@share21 ~]# touch -t 201211251230 file004
[root@share21 ~]# ll
total 0
-rw-r--r--. 1 root root 0 May 18 11:05 file001
-rw-r--r--. 1 root root 0 May 18 11:05 file002
-rw-r--r--. 1 root root 0 May 18 11:05 file003
-rw-r--r--. 1 root root 0 Nov 25  2012 file004

 

[root@share21 ~]# touch -d "2012-11-25 12:01" file004
[root@share21 ~]# stat file004
  File: ‘file004’
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fd00h/64768d    Inode: 67655814    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2012-11-25 12:01:00.000000000 +0800
Modify: 2012-11-25 12:01:00.000000000 +0800
Change: 2016-05-18 11:28:01.466228047 +0800
 Birth: -

6.只修改文件的訪問時間

[root@share21 ~]# touch -d -a "2012-11-25 12:01" file004

7.只修改文件的修改時間

[root@share21 ~]# touch -d -m "2012-11-25 12:01" file004

最後總結下經常使用的文件操做與時間的關係:

一、訪問時間,讀一次這個文件的內容,這個時間就會更新。好比對這個文件使用more命令。ls、stat命令都不會修改文件的訪問時間。

二、修改時間,對文件內容修改一次,這個時間就會更新。好比:vim後保存文件。ls -l列出的時間就是這個時間。

三、狀態改變時間。經過chmod命令更改一次文件屬性,這個時間就會更新。查看文件的詳細的狀態、準確的修改時間等,能夠經過stat命令文件名。

相關文章
相關標籤/搜索