歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> 每天一個Linux命令(4)touch命令

每天一個Linux命令(4)touch命令

日期:2017/3/3 14:03:57   编辑:Linux技術

touch命令有兩個功能:一是用於把已存在文件的時間標簽更新為系統當前的時間(默認方式),它們的數據將原封不動地保留下來;二是用來創建新的空文件。[b] (1)用法[/b]

用法:touch [選項]... 文件...[b] (2)功能[/b]

1)將每個文件的訪問時間和修改時間改為當前時間; 2)不存在的文件將會被創建為空文件,除非使用-c 或-h 選項;

[b] (3)選項參數[/b] 1) -f, --force 忽略不存在的文件,從不給出提示

2) -i, --interactive 進行交互式刪除 3) -r, -R, --recursive 指示rm將參數中列出的全部目錄和子目錄均遞歸地刪除

4) -v, --verbose 詳細顯示進行的步驟 5) --help 顯示此幫助信息並退出

6) --version 輸出版本信息並退出 7) -a 只更改訪問時間

8) -d, --date=字符串 使用指定字符串表示時間替代當前時間 9) -t STAMP 使用[[CC]YY]MMDDhhmm[.ss] 格式的時間替代當前時間

[b] (4)實例: [/b] 1)[root@localhost /]# touch /home/sunjimeng/Document/touch_test_file 在指定目下創建文件,如果沒有指定,則默認在當前文件夾下(若文件已經存在,則可以看作重新更改文件檔案時間)

[root@localhost /]# ls -l /home/sunjimeng/Document
總用量 0
drwxrwxr-x. 3 sunjimeng sunjimeng 17 5月   1 03:13 bin
drwxrwxr-x. 3 sunjimeng sunjimeng 18 5月   1 03:21 Father
[root@localhost /]# touch /home/sunjimeng/Document/touch_test_file
[root@localhost /]# cd home/sunjimeng/Document
[root@localhost Document]# ll
總用量 0
drwxrwxr-x. 3 sunjimeng sunjimeng 17 5月   1 03:13 bin
drwxrwxr-x. 3 sunjimeng sunjimeng 18 5月   1 03:21 Father
-rw-r--r--. 1 root      root       0 5月   1 18:30 touch_test_file    //第一次創建的時間
[root@localhost Document]#
[root@localhost Document]# touch touch_test_file

[root@localhost Document]# ll

總用量 0

drwxrwxr-x. 3 sunjimeng sunjimeng 17 5月 1 03:13 bin

drwxrwxr-x. 3 sunjimeng sunjimeng 18 5月 1 03:21 Father

-rw-r--r--. 1 root root 0 5月 1 18:35 touch_test_file //第二次更改文件的時間為當前時間,若文件中存有內容,則內容不變

2)[root@localhost Document]# touch -t 06061806 touch_test_file 不創建文件,只是更改文檔時間(這裡加不加-c操作是一樣的)

[root@localhost Document]# touch -t 06061806 touch_test_file
[root@localhost Document]# ll
總用量 0
drwxrwxr-x. 3 sunjimeng sunjimeng 17 5月   1 03:13 bin
drwxrwxr-x. 3 sunjimeng sunjimeng 18 5月   1 03:21 Father
-rw-r--r--. 1 root      root       0 6月   6 2016 touch_test_file
3)[root@localhost Document]# touch touch_test_file1 touch_test_file2 將touch_test_file2的時間更改為與touch_test_file1的時間相同

[root@localhost Document]# touch touch_test_file2
[root@localhost Document]# ll
總用量 0
drwxrwxr-x. 3 sunjimeng sunjimeng 17 5月   1 03:13 bin
drwxrwxr-x. 3 sunjimeng sunjimeng 18 5月   1 03:21 Father
-rw-r--r--. 1 root      root       0 6月   6 2016 touch_test_file      //兩個文件時間不相同
-rw-r--r--. 1 root      root       0 5月   1 18:42 touch_test_file2    
[root@localhost Document]# touch -r touch_test_file touch_test_file2
[root@localhost Document]# ll
總用量 0
drwxrwxr-x. 3 sunjimeng sunjimeng 17 5月   1 03:13 bin
drwxrwxr-x. 3 sunjimeng sunjimeng 18 5月   1 03:21 Father
-rw-r--r--. 1 root      root       0 6月   6 2016 touch_test_file     //兩個文件時間相同
-rw-r--r--. 1 root      root       0 6月   6 2016 touch_test_file2
4)[root@localhost Document]# touch -d "10 days ago" touch_test_file2 將當前指定文件時間更改為相對現在的過去或未來時間

[root@localhost Document]# touch -d "10 days ago" touch_test_file2
[root@localhost Document]# ll
總用量 0
drwxrwxr-x. 3 sunjimeng sunjimeng 17 5月   1 03:13 bin
drwxrwxr-x. 3 sunjimeng sunjimeng 18 5月   1 03:21 Father
-rw-r--r--. 1 root      root       0 6月   6 2016 touch_test_file
-rw-r--r--. 1 root      root       0 4月  21 18:48 touch_test_file2           //不是6月6號的10天前,而是當前時間的10天前
5)[root@localhost Document]# touch --help 與touch --version

[root@localhost Document]# touch --help
用法:touch [選項]... 文件...
Update the access and modification times of each FILE to the current time.

A FILE argument that does not exist is created empty, unless -c or -h
is supplied.

A FILE argument string of - is handled specially and causes touch to
change the times of the file associated with standard output.

Mandatory arguments to long options are mandatory for short options too.
  -a            只更改訪問時間
  -c, --no-create    不創建任何文件
  -d, --date=字符串    使用指定字符串表示時間而非當前時間
  -f            (忽略)
  -h, --no-dereference        會影響符號鏈接本身,而非符號鏈接所指示的目的地
                (當系統支持更改符號鏈接的所有者時,此選項才有用)
  -m            只更改修改時間
  -r, --reference=FILE   use this file's times instead of current time
  -t STAMP               use [[CC]YY]MMDDhhmm[.ss] instead of current time
      --time=WORD        change the specified time:
                           WORD is access, atime, or use: equivalent to -a
                           WORD is modify or mtime: equivalent to -m
      --help        顯示此幫助信息並退出
      --version        顯示版本信息並退出

請注意,-d 和-t 選項可接受不同的時間/日期格式。

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
請向<http://translationproject.org/team/zh_CN.html> 報告touch 的翻譯錯誤
要獲取完整文檔,請運行:info coreutils 'touch invocation'
[root@localhost Document]# touch --version
touch (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
許可證:GPLv3+:GNU 通用公共許可證第3 版或更新版本<http://gnu.org/licenses/gpl.html>。
本軟件是自由軟件:您可以自由修改和重新發布它。
在法律范圍內沒有其他保證。

由Paul Rubin、Arnold Robbins、Jim Kingdon、
David MacKenzie 和 Randy Smith 編寫。
[b] (5)其他[/b]

說明: -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日的時間是錯誤的。

Copyright © Linux教程網 All Rights Reserved