Linux touch command instance graphic explanation

Source: Internet
Author: User

Linux touch command instance graphic explanation

The touch command can be used to modify the file access/modification timestamp. More often, it is used to quickly create an empty file.

A good feature of touch is that compared with commands such as cp (used to copy files and directories) and mv (used to move or rename files and directories, it does not automatically overwrite (that is, erase) the content of an existing file with the same name. Instead, it only changes the last access time of these files to the current time.

In this article, we will explain some simple and quick examples of using the touch command to modify the timestamp or create a file.

1. How to Create a blank File

To use the touch command to create a simple blank file, use the following syntax. If the selected file name already exists, you only need to change the access time

Bkjia @ bkjia :~ $ Touch bkjia.txt

2. How to create multiple files

If you want to use the touch command to create multiple files, you only need to enter a file name with a space extension. You can check the following syntax.

Bkjia @ bkjia :~ $ Touch bkjia_com.txt linuxmu.txt bkjia_net.txt

3. How to use a command to create batch files

If you want to create a batch file, such as 20 or 30 files, instead of the extension. The example below will certainly help you. You can run the ls or ll command to check whether the file is created. Because I use a graphical interface, open the folder to see more intuitive.

1. Create A file named A to Z

Bkjia @ bkjia :~ $ Touch {A. Z}

2. Create a file from 1 to 100

Bkjia @ bkjia :~ $ Touch {1 .. 100}

3. Create batch files with extensions

Bkjia @ bkjia :~ $ Touch 255.2..2002.16.txt

4. Create 10000 files with extensions

Bkjia @ bkjia :~ $ Touch 255.1..102.161..10002.16.txt

For example:

Open the folder to see:

4. How to avoid creating new files

You just want to update the access time of the existing file? Yes, of course you can do this without creating a new file. Use Option "-c ". If the file exists, touch updates the access time, otherwise nothing will be done.

Bkjia @ bkjia :~ $ Touch-c bkjia.txt

5. How to change the file access time

To only change the file access time, use the "-a" option and file name. You can check the following example.

Bkjia @ bkjia :~ $ Touch-a bkjia.txt

After you complete the preceding command, check whether the command is changed? Try the following command.

Bkjia @ bkjia :~ $ Stat bkjia.txt

The output is as follows.

File: bkjia.txt
Size: 0 blocks: 0 IO blocks: 4096 common Empty files
Device: 804 h/2052d Inode: 1451165 hard link: 1
Permission: (0644/-rw-r --) Uid :( 1000/bkjia) Gid :( 1000/bkjia)
Recent access: 20:17:54. 730291896 + 0800
Recent Changes: 20:17:26. 434083956 + 0800
Last modified: 20:17:54. 730291896 + 0800
Creation Time :-

For example:

6. How to change the modification time

Use the '-m' option to change the file modification time. You can use the stat command to check the following time changes.

Bkjia @ bkjia :~ $ Touch-m bkjia.txt

After you complete the preceding command, check whether the command is changed? Try the following command.

Bkjia @ bkjia :~ $ Stat bkjia.txt

The output is as follows.

File: bkjia.txt
Size: 0 blocks: 0 IO blocks: 4096 common Empty files
Device: 804 h/2052d Inode: 1451165 hard link: 1
Permission: (0644/-rw-r --) Uid :( 1000/bkjia) Gid :( 1000/bkjia)
Recent access: 20:17:54. 730291896 + 0800
Recent Changes: 20:19:51. 999315295 + 0800
Last modified: 20:19:51. 999315295 + 0800
Creation Time :-

For example:

7. How to change the modification time of multiple files

Use wildcards to change the modification time of multiple files.

Bkjia @ bkjia :~ $ Touch-m *. txt

8. How to Use the access and modification time options

We use the "a" and "m" options together to modify the access and modification time. You can also use the "stat" command to check the changed timestamp.

Bkjia @ bkjia :~ $ Touch-am bkjia.txt

After you complete the preceding command, check whether the command is changed? Try the following command.

Bkjia @ bkjia :~ $ Stat bkjia.txt

The output is as follows.

File: bkjia.txt
Size: 0 blocks: 0 IO blocks: 4096 common Empty files
Device: 804 h/2052d Inode: 1451165 hard link: 1
Permission: (0644/-rw-r --) Uid :( 1000/bkjia) Gid :( 1000/bkjia)
Recent access: 20:24:56. 038676993 + 0800
Recent Changes: 20:24:56. 038676993 + 0800
Last modified: 20:24:56. 038676993 + 0800
Creation Time :-

9. How to set the specific access/modification time

To set the access/modification time to a specific date time, use the t option and specify the date time (CC) YY) MMDDhhmm. (ss) in this format)

Touch Command Option List

  • -A-change the file access time
  • -C-do not create the specified file if it does not exist.
  • -M-changed the file modification time.
  • -R -- ref_file: the time when the file named by ref_file is used instead of the current time.
  • -T-time: Use the specified time instead of the current time. The time will be the decimal number of the table.
  • MM-month in a year [01-12]
  • DD-one day per month [01-31]
  • Hh-hour in a day [00-23]
  • Mm-hour minutes [00-59]
  • CC-the first two digits of the year
  • YY-the last two digits of the year
  • SS-second minute [00-61]

Bkjia @ bkjia :~ $ Touch-c-t 201802062036.18 bkjia.txt

 

Note: If the c option is omitted, if it does not exist, a new file will be created using the given date and time.

10. How to Use the timestamp of another file as a reference

Set the access/modification time of linuxmi.txt to bkjia.txt with the following command:

Bkjia @ bkjia :~ $ Touch-r bkjia.txt linuxmi.txt

11. How to specify a date and time as a string

In addition to the option "t", there is another option "-d", which accepts the date time in general human readable format.

The following example only provides the date. The time is automatically set.

Bkjia @ bkjia :~ $ Touch-c-d '18 aug' bkjia.txt

For example:

 

Summary

In this article, we will introduce the basic touch commands that should be used more frequently. You can search for more Command Options on its manual page.

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.