In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article is to share with you about the use of the touch command in Linux. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
The touch command can be used to modify the access / modification timestamp of a file. More often it will be used to quickly create an empty file.
The touch command can be used to modify the access / modification timestamp of a file. More often it will be used to quickly create an empty file.
A good feature of touch is that, compared with commands such as cp (for copying files and directories) and mv (for moving or renaming files and directories), it does not automatically overwrite (that is, erase) the contents of existing files with the same name. Instead, it simply 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 a timestamp or create a file.
1. How to create a blank file
To create a simple blank file using the touch command, use the following syntax. If the file name you selected already exists, just change the access time
Linuxidc@linuxidc:~$ touch linuxidc.txt
two。 How to create multiple files
If you want to create multiple files using the touch command, simply enter the file name with a space extension. You can check the grammar below.
Linuxidc@linuxidc:~$ touch linuxidc_com.txt linuxmu.txt linuxidc_net.txt
3. How to create batch files with one command
If you want to create batch files, such as 20 or 30 files, instead of an extension. The following example will certainly help you. You can use the command ls or ll to check whether the file has been created. Since I am using a graphical interface, it is more intuitive to open the folder directly.
1)。 Create a file named A to Z
Linuxidc@linuxidc:~$ touch {A..Z}
2)。 Create a file from 1 to 100
Linuxidc@linuxidc:~$ touch {1..100}
3)。 Create a batch file with an extension
Linuxidc@linuxidc:~$ touch {2..200} .txt
4)。 Create 10000 files with an extension
Linuxidc@linuxidc:~$ touch {1..10} {1..1000} .txt
As shown below:
Open the folder and have a look:
4. How to avoid creating new files
Do you just want to update the access time of existing files? Yes, of course you can do this without creating a new file. Use the option "- c". If the file exists, touch will update the access time, otherwise nothing will be done.
Linuxidc@linuxidc:~$ touch-c linuxidc.txt
5. How to change the access time of a file
To change only the access time of the file, use the "- a" option and the file name. You can check the following example.
Linuxidc@linuxidc:~$ touch-a linuxidc.txt
Once you have completed the above command, check to see if it has been changed? Try the following command.
Linuxidc@linuxidc:~$ stat linuxidc.txt
The output is shown below.
File: linuxidc.txt
Size: 0 block: 0 IO block: 4096 ordinary empty file
Device: 804h/2052d Inode:1451165 hard link: 1
Permission: (0644) Uid: (1000/linuxidc) Gid: (1000/linuxidc)
Last visit: 2018-02-06 20 17 Vera 54.730291896 + 0800
Last modified: 2018-02-06 20 17 Vera 26.434083956 + 0800
Last modified: 2018-02-06 20 17 54.730291896 + 0800
Creation time:-
As shown below:
6. How to change the modification time
Use the'- m 'option to change the modification time of the file. You can check the following time changes with the stat command.
Linuxidc@linuxidc:~$ touch-m linuxidc.txt
Once you have completed the above command, check to see if it has been changed? Try the following command.
Linuxidc@linuxidc:~$ stat linuxidc.txt
The output is shown below.
File: linuxidc.txt
Size: 0 block: 0 IO block: 4096 ordinary empty file
Device: 804h/2052d Inode:1451165 hard link: 1
Permission: (0644) Uid: (1000/linuxidc) Gid: (1000/linuxidc)
Last visit: 2018-02-06 20 17 Vera 54.730291896 + 0800
Last modified: 2018-02-06 20 19pura 51.999315295 + 0800
Last modified: 2018-02-06 20 19VR 51.999315295 + 0800
Creation time:-
As shown below:
7. How to change the modification time of multiple files
Use wildcards to change the modification time of multiple files.
Linuxidc@linuxidc:~$ touch-m * .txt
8. How to use the access and modification time options
Together, we use the "a" and "m" options to modify the access and modification time. You can also use the "stat" command to check for changed timestamps.
Linuxidc@linuxidc:~$ touch-am linuxidc.txt
Once you have completed the above command, check to see if it has been changed? Try the following command.
Linuxidc@linuxidc:~$ stat linuxidc.txt
The output is shown below.
File: linuxidc.txt
Size: 0 block: 0 IO block: 4096 ordinary empty file
Device: 804h/2052d Inode:1451165 hard link: 1
Permission: (0644) Uid: (1000/linuxidc) Gid: (1000/linuxidc)
Last visit: 2018-02-06 20 24 Vera 56.038676993 + 0800
Last revision date: 2018-02-06 20: 24 Vera 56.038676993 + 0800
Last modified: 2018-02-06 20 24 56.038676993 + 0800
Creation time:-
9. How to set a specific time for access / modification
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
List of options for the touch command
-a-change the access time of the file
-c-if it does not exist, do not create the specified file.
-m-change the modification time of the file.
-r-- ref_file uses the corresponding time of the file named by ref_file instead of the current time.
-t-time uses the specified time instead of the current time. The time will be the decimal number of the table
MM-month of the year [01-12]
DD-one day of each month [01-31]
Hh-hours of the day [00-23]
Mm-minutes of the hour [00-59]
CC-the first two digits of the year
YY-the last two digits of the year
SS-second minute [00-61]
Linuxidc@linuxidc:~$ touch-c-t 201802062036.18 linuxidc.txt
Note: if the c option is omitted, if it does not exist, a new file is created with the given date and time.
10. How to use the timestamp of another file as a reference
The following command sets the access / modification time of linuxmi.txt to the access / modification time of linuxidc.txt
Linuxidc@linuxidc:~$ touch-r linuxidc.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 a date and time in a general human-readable format.
The following example provides only dates. The time will be automatically set to 00:00
Linuxidc@linuxidc:~$ touch-c-d'18 Aug' linuxidc.txt
As shown below:
Thank you for reading! This is the end of this article on "what is the use of touch commands in Linux?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.