How to use the touch command under Linux?

  

In Linux commands, we will often use cd, ls and other commands, but commands like touch are less useful, so what does the touch command do? Let's take a look at the usage of the touch command under Linux with the small series.

linux in touch command is not commonly used, in general may be useful when using the make, the time stamp is used to modify the file or create a 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 change the access time.

-c or --no-create does not create any documents.

-d Use the specified date and time instead of the current time.

-f This parameter will be ignored and will only be used to resolve compatibility issues with the BSD version of the touch directive.

-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 and time instead of the current time.

3. Command Function:

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

4. Example of use:

Example 1: Creating a file that does not exist

Command:

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, then Do not create a file

[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

Instance 2 : Update log.log time is the same as log2012.log timestamp

Command:

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

Instance 3: Setting the timestamp of the file

Command:

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 ro Ot root 0 2012-11-14 log.log

Description:

-t time Use the specified time value time as the new value for the corresponding timestamp of the specified file. The time here is defined as a decimal number in the following form:

[[CC]YY]MMDDhhmm[.SS]

Here, CC is the first two digits of the year, ie, ” The number rd is the last two digits of the year, that is, the number of years in a century. If you do not give the value of CC, touch will limit the number of years CCYY to 1969--2068. MM is the number of months, DD is the day 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 (several), mm is the number of minutes, and SS is the number of seconds. Here the setting range of seconds is 0--61, which can handle leap seconds. The time formed by these numbers is one of the time zones specified by the environment variable TZ. Due to system limitations, the time earlier than January 1, 1970 was wrong.

The above is the usage of the touch command under Linux. The touch command can be used to modify the file timestamp. The modified date is the specified date and time instead of the current time. Have you learned it?

Copyright © Windows knowledge All Rights Reserved