Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the touch command in Linux system

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Linux system touch command how to use, I believe that many inexperienced people are helpless about this, this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

1. Command Format:

touch [Options]... Documents...

2. Command parameters:

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

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

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

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

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

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

-t Use the specified datetime 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. Examples of use:

Example 1: Creating a non-existent file

Command:

touch log2012.log log2013.log

Output:

The code is as follows:

[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

The code is as follows:

[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 log2012.log timestamp

Command:

touch -r log.log log2012.log

Output:

The code is as follows:

[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

Command:

touch -t 201211142234.50 log.log

Output:

The code is as follows:

[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

Description:

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

[[CC]YY]MMDDhhmm[.SS]

Here CC is the first two digits of the year, i.e."century number";YY is the last two digits of the year, i.e. the year in a century. If no CC value is given, touch will limit the year CCYY to 1969--2068. MM is the month, DD is the day will limit the year CCYY to 1969--2068. MM is the month, DD is the day, hh is the hour, mm is the minute, SS is the second. Here the second setting range is 0--61, so you can handle leap seconds. The time of these numbers is a time in the time zone specified by the environment variable TZ. Due to system limitations, dates earlier than January 1, 1970 are incorrect.

Command Name:

touch(1) -Update the time record of the file

Description:

With this command, we can change two kinds of time records of the file simultaneously or separately, access time and modification time.

Options:

-a

Change file access time

-c, --no-create

No new files created

-m

Change file modification time

-t STAMP

Set "specific time" instead of "current time," format [[CC]YY]MMDDHhmm[.ss]

--help

Display program usage information

--version

Display version information for the program itself

Example:

open case

The code is as follows:

# touch file-touch2

# ls -l

total 0

-rw-r--r-- 1 root root 0 Jul 9 13:53 file-touch2

#

Under normal conditions, use the touch command to change the file time. If the file does not exist, touch will automatically create the specified file (empty file). If you do not want to create any new files as a result of using touch, you must add the option-c

The code is as follows:

# touch -c file-touch3

touch: setting times of `file-touch3 ': No such file or directory

#

The command results in an error message because the specified file does not exist. This is exactly what option-c is for. The Update File Time touch command is usually the computer time at which the following command is used as the time to update the file. But also offers special options-t to

Specify a specific time as the update time. For the convenience of an example, I am originally uncertain, copy a system program that has been documented for a long time- tcsh

The code is as follows:

# cp -a /bin/tcsh .

# ls --full-time

total 288

-rwxr-xr-x 1 root root 288604 Mon Jun 25 03:45:26 2001 tcsh

#

The program was last modified at 3:45:26 on June 25, 2001. Now, I intend to change the file time to July 1, 2002 at 8:00:01 due to monitoring needs. The simplest and most effective way is as follows

The code is as follows:

# touch -t 200207010800.01 tcsh

# ls --full-time

total 288

-rwxr-xr-x 1 root root 288604 Mon Jul 01 08:00:01 2002 tcsh

The syntax of the option is simple, that is, you can type the number of the year, day and hour directly, without any separation. If you want to add seconds, it is more special to use small dots." "To distinguish it from the preceding paragraph. Of course, touch commands also support separate changes to Read Time or Modify Time when needed.

The code is as follows:

# touch -at 200207022300 tcsh

# ls -lu

total 288

-rwxr-xr-x 1 root root 288604 Jul 2 23:00 tcsh

#

The above example changes Read Time individually, and the following example changes Modify Time

The code is as follows:

# touch -mt 200206302300 tcsh

# ls -l

total 288

-rwxr-xr-x 1 root root 288604 June 30 23:00 tcsh

# ls -lu

total 288

-rwxr-xr-x 1 root root 288604 Jul 2 23:00 tcsh

#

After reading the above, do you know how to use touch command in Linux system? If you still want to learn more skills or want to know more related content, welcome to pay attention to the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report