A Linux command a day (9): touch command

post thumb
Devops
by Admin/ on 21 Sep 2021

A Linux command a day (9): touch command


The touch command in linux is not commonly used, but 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 [option ]… File…

2. Command parameters.

-a or –time=atime or –time=access or –time=use Change only the access time.

-c or –no-create Does not create any documents.

-d Use the specified datetime, not the present time.

-f This parameter will be ignored and not processed, and is only responsible for resolving compatibility issues with the BSD version of the touch command.

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

-r Sets the date and time of the specified document or directory to the same date and time as the reference document or directory.

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

3. Command Function.

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

4. Usage examples.

Example 1: Create a non-existent file

Command:

touch log2012.log log2013.log

output:

[root@localhost test]# touch log2012.log log2013.log  
[root@localhost test]# 11  
-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, then do not create the file  
[root@localhost test]# touch -c log2014.log  
[root@localhost test]# 11  
-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 log.log with the same timestamp as log2012.log

Command:

touch -r log.log log2012.log

output:

[root@localhost test]# 11  
-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]# 11  
-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

Commands:

touch -t 201211142234.50 log.log

output:

[root@localhost test]# 11  
-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]# 11  
-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  

Description:

-t time uses the specified time value time as the new value for the corresponding timestamp of the specified file. The time here is specified as a decimal number of the following form: [[CC]YY]MMDDhhmm[.SS]

Here, CC is the first two digits of the year, i.e., the “century number”, and YY is the last two digits of the year, i.e., the number of years in a century. If the value of CC is not given, touch will limit the number of years CCYY to 1969-2068. MM is the number of months, DD is the number of days will limit the number of years CCYY to 1969-2068. MM is the number of months, DD is the number of days, hh is the number of hours (points), mm is the number of minutes, SS is the number of seconds. Here the seconds are set from 0 to 61, so that leap seconds can be handled. The time that these numbers form is one of the times in the time zone specified by the environment variable TZ. Due to system limitations, times earlier than January 1, 1970 are incorrect.

Reference:

https://www.cnblogs.com/peida/archive/2012/10/30/2745714.htm

comments powered by Disqus