Touch: Command to create a file

Source: Internet
Author: User
Tags touch command

Touch: creates an empty file or changes the timestamp attribute of the file.

[Function description]

The touch command has two functions: one is to create a new empty file, and the other is to change the timestamp attribute of an existing file.

[Syntax format]

Touch [Option] [file]

Touch [Option] [file]

Tip:

      1. Touch command and the following options and files. Each element must have at least one space.
      2. Note that the functions of the touch and mkdir commands are distinguished. The mkdir command creates an empty directory while the touch command creates an empty file.
      3. In Linux, everything is a file. Although the touch command cannot create a directory, you can modify the time stamp of the directory.

[Option description]

Parameter description Description
- Only change the last access time of the specified file
-D Use the time represented by string as the template to set the time attribute of the specified file
-M Only change the last modification time of the specified file
-R File Set the time attribute of the specified file to the same as the time attribute of the template file.
-T stamp Use the time in the [[CC] YY] mmddhhmm [. SS] format to set the time attribute of the file. The meaning of the format is from left to right: century, year, month, day, hour, minute, second

[Example]

Basic example

Case 1: File Creation example (the file does not exist beforehand)

[[Email protected] ~] # Mkdir/test # create a test directory under the root
[[Email protected] ~] # Cd/test/# Switch to the/Text directory
[[Email protected] Test] # Touch aa.txt creating an empty file aa.txt
[[Email protected] Test] # ls successfully uploaded to the aa.txt file.
Aa.txt
[[Email protected] Test] # Touch a.txt B .txt # create multiple files at the same time, similar to mkdir to create multiple directories
[[Email protected] Test] # ls
Aa.txt a.txt B .txt
[[Email protected] Test] # Touch SCC {1 .. 5} # You can use braces "{}" to output character sequences to create files in batches.
[[Email protected] Test] # ls
Aa.txt a.txt B .txt scc1 scc2 scc3 scc4 scc5

Case 2: Change the timestamp attribute of a file

[[Email protected] Test] # stat aa.txt # The STAT command can be used to view the timestamp attribute of the file. For detailed usage, see the subsequent STAT command
File: "aa.txt"
Size: 0 blocks: 0 Io blocks: 4096 common Empty files
Device: fd00h/64768 dinode: 35313153 hard link: 1
Permission: (0644/-RW-r --) uid :( 0/root) gid :( 0/root)
Recent access: 23:11:37. 065471839 + 0800
Recent Changes: 23:11:37. 065471839 + 0800
Last modification: 23:11:37. 065471839 + 0800
Creation Time :-

Tip:

Note: The timestamp attribute of a file can be divided into access time, modification time, and state change time.


[[Email protected] Test] # Touch-A aa.txt #-a parameter to change the last access time
[[Email protected] Test] # stat aa.txt
File: "aa.txt"
Size: 0 blocks: 0 Io blocks: 4096 common Empty files
Device: fd00h/64768 dinode: 35313153 hard link: 1
Permission: (0644/-RW-r --) uid :( 0/root) gid :( 0/root)
Recent access: 23:17:06. 329316946 + 0800
Recent Changes: 23:11:37. 065471839 + 0800
Last modification: 23:17:06. 329316946 + 0800
Creation Time :-

 

[[Email protected] Test] # Touch-M aa.txt #-M parameter change the last modification time
[[Email protected] Test] # stat aa.txt
File: "aa.txt"
Size: 0 blocks: 0 Io blocks: 4096 common Empty files
Device: fd00h/64768 dinode: 35313153 hard link: 1
Permission: (0644/-RW-r --) uid :( 0/root) gid :( 0/root)
Recent access: 23:17:06. 329316946 + 0800
Recent Changes: 23:20:26. 211938858 + 0800
Last modification: 23:20:26. 211938858 + 0800
Creation Time :-

Case 3: Create/modify a file at a specified time

You can use option-D to specify the file modification time after the file is created:

[[Email protected] Test] # ls-lH aa.txt # file modification time before modification: January 1, October 30
-RW-r -- 1 Root 0 October 30 23:20 aa.txt
[[Email protected] Test] # Touch-D 20181129 aa.txt # specify that the file created after the file is modified on January 1, November 29, 2018.
[[Email protected] Test] # ls-lH aa.txt # The modified time is January 1, November 29, 2018.
-RW-r -- 1 Root 0 November 29 2018 aa.txt

You can also modify the time limit of aa.txt to make it consistent with the time attribute of a.txt:

[[Email protected] Test] # ls-lH a.txt when modified a.txt
-RW-r -- 1 Root 0 October 30 23:11 a.txt
[[Email protected] Test] # Touch-r a.txt aa.txt uses -r to make the.txt time consistent with a.txt.
[[Email protected] Test] # ls-lH aa.txt
-RW-r -- 1 Root 0 October 30 23:11 aa.txt modified aa.txt file time is consistent with a.txt

You can also use option-t to set the file to the 201810312333.50 time format.

[[Email protected] Test] # Touch-T 201810312333.50 aa.txt
[[Email protected] Test] # ls-lH -- full-time aa.txt
-RW-r -- 1 Root 0 23:33:50. 000000000 + 0800 aa.txt

View Set Properties

[Extended knowledge]

Here we will extend some knowledge about timestamp attributes.

There are three types of timestamps for GNU/Linux Files:

Access: 23:33:50. 000000000 + 0800 # Last file access time
Modify: 23:33:50. 000000000 + 0800 # Last file modification time
Change: 23:35:05. 120111040 + 0800 # Time for last changing the File status

Corresponding to the LS command, the options for viewing the preceding timestamp are as follows:

Mtime: The last modification time (LS-lt) # modifies the file content, and the modification time (modify time) of the file changes.

Ctime: State Change Time (LS-LC) # modify the file content, move the file, or change the file attributes. The change time of the file changes.

Atime: Last access time (LS-Lu) # When you view the file content, the access time of the file changes

Touch: Command to create a file

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.