Home > Operation and Maintenance > Linux Operation and Maintenance > How to use Linux touch command

How to use Linux touch command

王林
Release: 2023-05-26 23:21:53
forward
5243 people have browsed it

1. Introduction to touch command

This command can be used to change the time attributes of a file or directory, including access time and modification time. If the file does not exist, a new file is created. What we usually use most is to create an empty file through touch. In fact, the atime and mtime of the file can be modified through the touch command, so the real access time and modification time of the file can be modified. We also need to do this when troubleshooting system abnormalities. Make comprehensive decisions based on factors such as logs and historical commands.

2. Usage examples

1. Get command help

[root@s145]~# touch --help

2. Check the command version

[root@s145]~# touch --version
touch (GNU coreutils) 8.22

3. Create an empty file

If the file does not exist, use the touch command to create an empty file by default.

How to use Linux touch command

4. Update atime, mtime, and ctime

Use the touch command. If the file exists, update atime, mtime, and ctime to the current time.

How to use Linux touch command

5. -a updates atime and ctime

Use the -a parameter. When touching the file, if the file exists, only atime and ctime will be updated to the current time.

How to use Linux touch command

6. -m parameter to modify mtime

Use the -m parameter. When touching a file, only mtime and ctime will be updated to the current time if the file exists.

How to use Linux touch command

7. The -d parameter will modify the atime, mtime, and ctime of the file according to the settings.

Use the option -d "3 years ago" of the touch command. Set the three timestamps of the file to 3 years ago.

How to use Linux touch command

How to use Linux touch command

8. -t parameter to modify atime and mtime

Use touch -t "[[CC]YY] MMDDhhmm[.ss]"Modify the atime and mtime of the file

How to use Linux touch command

9. Use the -at parameter to only modify the atime

If you only want to update the atime, you can use - at combination parameters.

How to use Linux touch command

10. Use the -mt parameter to only modify mtime

If you only want to update mtime, you can use the -mt combination parameter.

How to use Linux touch command

11. The -r parameter modifies the file time attribute to another file time attribute.

Use the touch command and specify the file1 file as a reference. You can change the file2 file The time attribute value of is updated to the same value as file1. The specific command is: touch -r file1 file2.

How to use Linux touch command

12. -h updates the time attribute of the soft link

The touch command updates the time attribute of the real file by default. If you need to update the time of the soft link, Attribute, you need to use the -h parameter.

How to use Linux touch command

3. Usage syntax and parameter description

1. Usage syntax

Usage: touch [OPTION]… FILE&hellip ;

2. Parameter description

Parameter Parameter description
- a Change the file's read time record.
-c If the destination file does not exist, a new file will not be created. Same effect as --no-create.
-d Set the time and date in various formats.
-f is not used and is reserved for compatibility with other unix systems.
-h Affects each symlink, not any referenced file (only useful on systems where symlink timestamps can be changed)
-m Change the modification time record of the file.
-r Use the time record of the reference file, which has the same effect as --file.
-t Set the time record of the file, the format is the same as the date command.
–help Lists the command format.
–version List version information.

The above is the detailed content of How to use Linux touch command. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template