Linux系统下touch命令如何使用

软件发布|下载排行|最新软件

当前位置:首页操作系统Centos/RedHat

Linux系统下touch命令如何使用

  2020-12-11 我要评论

1.命令格式:

touch [选项]... 文件...

2.命令参数:

-a   或--time=atime或--time=access或--time=use  只更改存取时间。
-c   或--no-create  不建立任何文档。
-d  使用指定的日期时间,而非现在的时间。
-f  此参数将忽略不予处理,仅负责解决BSD版本touch指令的兼容性问题。
-m   或--time=mtime或--time=modify  只更改变动时间。
-r  把指定文档或目录的日期时间,统统设成和参考文档或目录的日期时间相同。
-t  使用指定的日期时间,而非现在的时间。

3.命令功能:

touch命令参数可更改文档或目录的日期时间,包括存取时间和更改时间。

4.使用范例:

实例一:创建不存在的文件

命令:

touch log2012.log log2013.log

输出:



[root@localhost test]# touch log2012.log log2013.log
[root@localhost test]# ll
-rw-r--r-- 1 root root 0 10-28 16:01 log2012.log
-rw-r--r-- 1 root root 0 10-28 16:01 log2013.log

如果log2014.log不存在,则不创建文件


[root@localhost test]# touch -c log2014.log
[root@localhost test]# ll
-rw-r--r-- 1 root root 0 10-28 16:01 log2012.log
-rw-r--r-- 1 root root 0 10-28 16:01 log2013.log

实例二:更新log.log的时间和log2012.log时间戳相同

命令:

touch -r log.log log2012.log

输出:



[root@localhost test]# ll
-rw-r--r-- 1 root root 0 10-28 16:01 log2012.log
-rw-r--r-- 1 root root 0 10-28 16:01 log2013.log
-rw-r--r-- 1 root root 0 10-28 14:48 log.log
[root@localhost test]# touch -r log.log log2012.log
[root@localhost test]# ll
-rw-r--r-- 1 root root 0 10-28 14:48 log2012.log
-rw-r--r-- 1 root root 0 10-28 16:01 log2013.log
-rw-r--r-- 1 root root 0 10-28 14:48 log.log

实例三:设定文件的时间戳

命令:

touch -t 201211142234.50 log.log

输出:



[root@localhost test]# ll
-rw-r--r-- 1 root root 0 10-28 14:48 log2012.log
-rw-r--r-- 1 root root 0 10-28 16:01 log2013.log
-rw-r--r-- 1 root root 0 10-28 14:48 log.log
[root@localhost test]# touch -t 201211142234.50 log.log
[root@localhost test]# ll
-rw-r--r-- 1 root root 0 10-28 14:48 log2012.log
-rw-r--r-- 1 root root 0 10-28 16:01 log2013.log
-rw-r--r-- 1 root root 0 2012-11-14 log.log

说明:

-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日的时间是错误的。

指令名稱:
touch(1) - 更新檔案的時間紀錄
說明:
藉由這個指令,我們可以同時或單獨變更檔案的兩種時間紀錄,讀取時間(access time)以及修改時間(modification time)。
選項:
-a
更改文件的讀取時間(access time)
-c, --no-create
不建立任何新檔案
-m
更改文件的修改時間(modification time)
-t STAMP
設定"特定時間"來代替"目前時間",格式 [[CC]YY]MMDDhhmm[.ss]
--help
顯示程式用法資訊
--version
顯示程式本身的版本資訊
實例說明:
空檔案



# touch file-touch1
# ls -l
total 0
-rw-r--r-- 1 root root 0 7月 9 13:53 file-touch1
#

一般狀況下使用 touch 指令變更檔案時間,如果檔案不存在,touch 會自動建立指定的檔案(空檔案)。如果不想因使用 touch 而建立任何新檔案,必須加上選項 -c



# touch -c file-touch2
touch: setting times of `file-touch2': 沒有此一檔案或目錄
#

指令的執行結果是出現一段錯誤訊息,因為指定的檔案不存在。這正是加上選項 -c 的目的。 更新檔案時間 touch 指令一般是以下指令當時的電腦時間來作為更新檔案的時間。但也提供特殊的選項 -t 來
指定"特定的時間"作為更新時間。為了舉例的方便,我原封不定地,拷貝一隻已建檔許久的系統程式 -- tcsh



# cp -a /bin/tcsh .
# ls --full-time
total 288
-rwxr-xr-x 1 root root 288604 週一 6月 25 03:45:26 2001 tcsh
#

這隻程式的最後修改時間是 -- 2001 年 6 月 25 日 3 點 45 分 26 秒。現在,我因監控上的需要,打算將檔案時間變更為 2002 年 7 月 1 日 8 點 00 分 01 秒。最簡單有效的作法如下



# touch -t 200207010800.01 tcsh
# ls --full-time
total 288
-rwxr-xr-x 1 root root 288604 週一 7月 01 08:00:01 2002 tcsh

選項的語法很單純,就是直接鍵入西元的年月日時分數字即可,其間不需任何分隔。如果要加上秒數,做法比較特別,需使用小點"."來與前段區隔。當然,在有需要時,touch 指令也支援"讀取時間"或"修改時間"的單獨變更。



# touch -at 200207022300 tcsh
# ls -lu
total 288
-rwxr-xr-x 1 root root 288604 7月 2 23:00 tcsh
#

上例是單獨變更"讀取時間", 下例則是變更"修改時間"



# touch -mt 200206302300 tcsh
# ls -l
total 288
-rwxr-xr-x 1 root root 288604 6月 30 23:00 tcsh
# ls -lu
total 288
-rwxr-xr-x 1 root root 288604 7月 2 23:00 tcsh
#

以上這些例子,即是大體上 touch 的運用。這篇就暫時先寫到這裡好了。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们