One linux command per day (9): touch command

The touch of linux is generally used when using make, to modify the file timestamp, or to create a new file that does not exist.

Also note the difference with mkdir. mkdir creates a folder by default, mkdir w2; at this time, a w2 folder is created; file w2 can view the properties of the folder w2, and the real directory at this time means that a folder is indeed built

            touch creates a blank document by default, touch w; at this time, a blank document of w is created; file w can view the properties of document w, and empty is displayed at this time, indicating that it is indeed a blank document

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 does not 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: The time to update log.log is the same as the timestamp of log2012.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 the 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

illustrate:

-t time Use the specified time value time as the new value for the corresponding timestamp of the specified file. Here time is specified as a decimal number of the form:      

  [[CC]YY]MMDDhhmm[.SS]     

  Here, CC is the first two digits of the number of years, that is, the "century number"; YY is the last two digits of the number of years, that is, the number of years in a century. If no CC value is given, touch will limit the years CCYY to 1969--2068. MM is the number of months, DD is the number of days will limit the number of years CCYY within 1969--2068. MM is the number of months, DD is the number of days, hh is the number of hours (time), mm is the number of minutes, and SS is the number of seconds. The setting range of seconds here is 0--61, so that leap seconds can be handled. The time composed of these numbers is a time in the time zone specified by the environment variable TZ. Times earlier than January 1, 1970 are wrong due to system limitations.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324652801&siteId=291194637