## touch命令
[TOC]
>[info] touch命令有兩個功能:
一是用于把已存在文件的時間標簽更新為系統當前的時間(默認方式),它們的數據將原封不動地保留下來;
二是用來創建新的空文件。
### 語法
`touch (選項) (參數)`
### 選項
>[success] -a:或--time=atime或--time=access或--time=use 只更改存取時間;
> -c:或--no-create 不建立任何文件;
> -d:<時間日期> 使用指定的日期時間,而非現在的時間;
> -f:此參數將忽略不予處理,僅負責解決BSD版本touch指令的兼容性問題;
> -m:或--time=mtime或--time=modify 只更該變動時間;
> -r:<參考文件或目錄> 把指定文件或目錄的日期時間,統統設成和參考文件或目錄的日期時間相同;
> -t:<日期時間> 使用指定的日期時間,而非現在的時間;
### 參數
文件:指定要設置時間屬性的文件列表
## 實例
### 在當前目錄下建立一個空文件ex2
然后,利用ls -l命令可以發現文件ex2的大小為0,表示它是空文件。
命令:touch ex2
輸出:
~~~
[root@localhost test]# touch ex2
[root@localhost test]# ls -l
總計 0
-rw-r--r-- 1 root root 0 09-14 14:19 ex2
# 如果文件存在則刷新文件創建時間 不新建
[root@localhost test]# cat ex2
1111
[root@localhost test]# ls -l
總計 4
-rw-r--r-- 1 root root 5 09-14 14:21 ex2
[root@localhost test]# date
2015年 09月 14日 星期一 14:23:36 CST
[root@localhost test]# touch ex2
[root@localhost test]# ls -l
總計 4
-rw-r--r-- 1 root root 5 09-14 14:23 ex2 # 文件創建時間被修改
[root@localhost test]# cat ex2
1111 # 文件內容不變
~~~
### 更新ex2的時間和ex1時間戳相同
命令:`touch -r ex1 ex2` *(將后面的時間改成前面的時間)*
輸出:
~~~
[root@localhost test]# ls -l
-rw-r--r-- 1 root root 0 2011-09-14 ex1
-rw-r--r-- 1 root root 5 09-14 14:23 ex2
[root@localhost test]# touch -r ex1 ex2
[root@localhost test]# ls -l
-rw-r--r-- 1 root root 0 2011-09-14 ex1
-rw-r--r-- 1 root root 5 2011-09-14 ex2
~~~
### 設定文件的時間戳
命令:`touch -t 2015091414631.55 ext1`
輸出:
~~~
[root@localhost test]# ls -l
-rw-r--r-- 1 root root 0 09-14 14:27 ex1
-rw-r--r-- 1 root root 5 09-14 14:23 ex2
[root@localhost test]# ls -l
-rw-r--r-- 1 root root 0 2011-09-14 ex1
-rw-r--r-- 1 root root 5 09-14 14:23 ex2
~~~
**說明**
> `-t time`
> 使用指定的時間值time作為指定文件相應時間戳記的新值.此處的time規定為如下形式的十進制數:[[CC]YY]MMDDhhmm[.SS]
> 這里,CC為年數中的前兩位,即”世紀數”;
> YY為年數的后兩位,即某世紀中的年數。如果不給出CC的值,則touch 將把年數CCYY限定在1969--2068之內。MM為月數,DD為天將把年數CCYY限定在1969--2068之內.MM為月數,DD為天數,hh為小時數(幾點),mm為分鐘數,SS為秒數。
> 此處秒的設定范圍是0--61,這樣可以處理閏秒.這些數字組成的時間是環境變量TZ指定的時區中的一個時間。由于系統的限制,早于1970年1月1日的時間是錯誤的。
- Linux的安裝
- Linux的軟件安裝管理
- Linux零碎的命令
- 了解ssh
- 系統調優及安全設置
- Linux系統中的硬鏈接和軟連接
- Linux文件和目錄的屬性及權限
- 命令總結
- 文件目錄管理命令
- cat
- cd
- cp
- ls
- mkdir
- mv
- rm
- touch
- vi
- 硬件檢測內核shell命令
- echo
- alias
- dd
- diff
- date
- grep(重要)
- head
- rpm
- sed(重要)
- tree
- unalias
- yum
- seq
- su
- less
- more
- tail
- awk(重要)
- pwd
- history
- whoami
- find(重要)
- which
- vimdiff
- sudo
- wget
- ln
- chmod
- chown
- vim常用技巧
- scp
- 工作場景應用總結
- 自動刪除n天前日志
- 刪除一個目錄下的所有文件但保留一個文件
- Linux軟件安裝
- php安裝(apache版本)
- vsftpd安裝
- git安裝
- python安裝
- LNMP安裝
- LAMP安裝I
- LAMP安裝II
- svn安裝
- svn在Linux下的命令操作
- svn鉤子簡介
- svn代碼上傳流程
- Crond介紹
- sersync應用指南
- 其他
- 小結一
- 系統調優重新整理tmp
- linux禁止root用戶直接登錄sshd并修改默認端口