当前位置:首页 > linux教程 > 列表

Linux文件目录时间更新touch命令用法

发布:smiling 来源: PHP粉丝网  添加日期:2015-04-29 11:15:11 浏览: 评论:0 

touch命令开始看成了权限命令后来发现touch命令是对目录或文件进行时间日期的一个更新操作了,下面来看一些简单的例子.

名称:touch

使用权限:所有使用者

使用方式:

  1. touch [-acfm] 
  2. [-r reference-file] [--file=reference-file] 
  3. [-t MMDDhhmm[[CC]YY][.ss]] 
  4. [-d time] [--date=time] [--time={atime,access,use,mtime,modify}] 
  5. [--no-create] [--help] [--version] 
  6. file1 [file2 ...] 

说明:touch 指令改变档案的时间记录,ls -l 可以显示档案的时间记录.

参数:

  1. a 改变档案的读取时间记录。 
  2. m 改变档案的修改时间记录。 
  3. c 假如目的档案不存在,不会建立新的档案。与 --no-create 的效果一样。 
  4. f 不使用,是为了与其他 unix 系统的相容性而保留。 
  5. r 使用参考档的时间记录,与 --file 的效果一样。 
  6. d 设定时间与日期,可以使用各种不同的格式。 
  7. t 设定档案的时间记录,格式与 date 指令相同。 
  8. --no-create 不会建立新档案。 
  9. --help 列出指令格式。 
  10. --version 列出版本讯息。 

使用范例:

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

命令:touch log2012.log log2013.log

输出:

  1. [root@localhost test]# touch log2012.log log2013.log 
  2. [root@localhost test]# ll  //phpfensi.com 
  3. -rw-r--r-- 1 root root    0 10-28 16:01 log2012.log 
  4. -rw-r--r-- 1 root root    0 10-28 16:01 log2013.log 

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

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

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

命令:touch -r log.log log2012.log

输出:

  1. [root@localhost test]# ll 
  2. -rw-r--r-- 1 root root    0 10-28 16:01 log2012.log 
  3. -rw-r--r-- 1 root root    0 10-28 16:01 log2013.log 
  4. -rw-r--r-- 1 root root    0 10-28 14:48 log.log 
  5. [root@localhost test]# touch -r log.log log2012.log  
  6. [root@localhost test]# ll 
  7. -rw-r--r-- 1 root root    0 10-28 14:48 log2012.log 
  8. -rw-r--r-- 1 root root    0 10-28 16:01 log2013.log 
  9. -rw-r--r-- 1 root root    0 10-28 14:48 log.log 

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

命令:touch -t 201211142234.50 log.log

输出:

  1. [root@localhost test]# ll 
  2. -rw-r--r-- 1 root root    0 10-28 14:48 log2012.log 
  3. -rw-r--r-- 1 root root    0 10-28 16:01 log2013.log 
  4. -rw-r--r-- 1 root root    0 10-28 14:48 log.log 
  5. [root@localhost test]# touch -t 201211142234.50 log.log 
  6. [root@localhost test]# ll 
  7. -rw-r--r-- 1 root root    0 10-28 14:48 log2012.log 
  8. -rw-r--r-- 1 root root    0 10-28 16:01 log2013.log 
  9. -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日的时间是错误的.

Tags: Linux文件目录 touch命令

分享到: