Linux command learning (9): touch

 

The touch command of linux is not commonly used. Generally, it may be used when using make, to modify the file timestamp, or to create a new file that does not exist .

 

----------------------------------------------------------------------------------------

 

 

1. Command format:

touch [options]... file...

2. Command parameters:

-a or --time=atime or --time=access or --time=use only changes the access time.

-c or --no-create do not create any documentation.

-d Use the specified datetime instead of the current time.

-f This parameter will be ignored and not processed, and it is only responsible for solving the compatibility problem of the touch command of the BSD version.

-m or --time=mtime or --time=modify only change the change time.

-r Set the date and time of the specified document or directory to be the same as the date and time of the referenced document or directory.

-t Use the specified datetime instead of the current time.

3. Command function:

The touch command parameters can change the date and time of the document or directory, including access time and change time. 

4. Usage example:

 

-------------------------------------------------------------------------------------

 

 

Example 1: Create a file that doesn't exist

Order:

touch log2012.log log2013.log

output:

[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

 

If log2014.log does not exist, no file is created

[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

 

 

 

 

Example 2: Update log2012.log  timestamp to  be the same as log.log

Order:

touch -r log.log log2012.log

output:

[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

 

 

 

 

Example 3: Setting the timestamp of a file

Order:

touch -t 201211142234.50 log.log

output:

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

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327041052&siteId=291194637