In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces how to use the touch command in Linux, the content is very detailed, interested friends can use for reference, I hope it can be helpful to you.
1. Create an empty file using touch
The most basic use of touch is to create a simple file that contains nothing. It is useful in many cases, such as when trying file operations or other terminal commands. To do this, simply pass the file name after the touch command.
Linuxidc@ubuntu:~/linuxidc.com$ touch linuxidc
This creates a simple empty file called linuxidc in the current working directory. You can use the ls command for verification. You can also use touch to create an empty file on any other directory by simply passing the correct path before the file name.
Linuxidc@ubuntu:~/linuxidc.com$ touch / path/to/filename
2. Use touch to create multiple empty files
You can also use the touch command to create multiple files from the terminal at the same time. All you need to do is pass all the file names you want to create in turn. Skim through the following commands to understand how they work.
Linuxidc@ubuntu:~/linuxidc.com$ rm linuxidc linuxidc@ubuntu:~/linuxidc.com$ touch linuxidc1 linuxidc2 linuxidc3 linuxidc4 linuxidc5
The above command creates all five files at the same time. You can use ls-l to verify the command in the terminal. You can also use the parenthesis extension feature to create multiple files through touch at a time, as shown in the following example.
Linuxidc@ubuntu:~/linuxidc.com$ rm linuxidc1 linuxidc2 linuxidc3 linuxidc4 linuxidc5 # first delete the file linuxidc@ubuntu:~/linuxidc.com$ touch linuxidc {1 ~ 2 ~ 2 ~ 3 ~ 4 ~ 5}
The above command will perform the same work, but in a more concise manner.
The above command is shown in the following figure:
3. Create a file and populate it
The following example shows you how to create a simple file using touch and populate it with some content. We use the Linux seq command to do this. But you can also use other methods if you want.
Linuxidc@ubuntu:~/linuxidc.com$ rm linuxidc1 linuxidc2 linuxidc3 linuxidc4 linuxidc5 # first delete the file linuxidc@ubuntu:~/linuxidc.com$ seq 10 > linuxidc linuxidc@ubuntu:~/linuxidc.com$ cat linuxidc
Now the linuxidc file is not empty, but contains numbers from 1 to 10. You can use the Linux cat command for verification.
4. Copy file access time
The touch command allows users to copy file access times between multiple files. You will need to use the-r option to enable this feature. Instead of using the current time, we will create a new file called linux, but copy the time value from the previously created test file.
Linuxidc@ubuntu:~/linuxidc.com$ touch-r linuxidc linux linuxidc@ubuntu:~/linuxidc.com$ ls-l
You can use the ls command for verification. You should see that the timestamp of the file linux is earlier than the current time and is the same as the file linuxidc. This option also has a long form, called-reference.
5. Use touch to change the access time of files
Linux administrators usually use the touch command to modify the access time of previously created files. The next command shows us how to do this. To do this, we need to use the-an option. First, check the timestamp of the file test using the following command.
Linuxidc@ubuntu:~/linuxidc.com$ ls-l linuxidc-- time=atime-rw-r--r-- 1 linuxidc linuxidc 21 4-thanks 15 18:28 linuxidc
Now, use the-an option to change this timestamp through touch. Note that the-- time=atime option of ls provides us with the access time of the file.
Linuxidc@ubuntu:~/linuxidc.com$ touch-a linuxidc linuxidc@ubuntu:~/linuxidc.com$ ls-l linuxidc-- time=atime-rw-r--r-- 1 linuxidc linuxidc 21 4-15 19:14 linuxidc
Now, if you check the timestamp of linuxidc again, you will notice that it has changed after executing the above command. It is one of the most common uses of touch in Linux.
6. Use touch to change the modification time of a file
You can also use touch to change the modification time of a file in Linux. You will need to use the-m option to do this. Take a closer look at the following example to see how it works.
Linuxidc@ubuntu:~/linuxidc.com$ ls-l linuxidc-rw-r--r-- 1 linuxidc linuxidc 21 4-thanks 15 18:28 linuxidc linuxidc@ubuntu:~/linuxidc.com$ touch-m linuxidc linuxidc@ubuntu:~/linuxidc.com$ ls-l linuxidc-rw-r--r-- 1 linuxidc linuxidc 21 4-visit 15 19:18 linuxidc
First, we looked at the last modification time of the test using ls. Then, we use the-m option of touch to change this value to the current time. You can verify this by running the ls command again.
Change the date and time to the current time
You can use-an and-m
Linuxidc@ubuntu:~/linuxidc.com$ stat linuxidc
File: linuxidc
Size: 21 blocks: 2 IO blocks: 512 ordinary files
Device: 32h/50d Inode:1466 hard link: 1
Permission: (0644) Uid: (1000/linuxidc) Gid: (1000/linuxidc)
Last visit: 2020-04-15 1919 14 Vera 52.595674642 + 0800
Last modified: 2020-04-15 1919 Vera 1815 53.189468825 + 0800
Last modified: 2020-04-15 1919 Vera 1815 53.189468825 + 0800
Creation time:-
You can use the stat command to view all the time-related information about the old file. It displays the last access time, the modification time, and the time when the file was changed. Now, use the following command to change the date and time to the current date and time.
Linuxidc@ubuntu:~/linuxidc.com$ touch-am linuxidc
Verify the date and time change by using the stat command again.
Linuxidc@ubuntu:~/linuxidc.com$ stat linuxidc
File: linuxidc
Size: 21 blocks: 2 IO blocks: 512 ordinary files
Device: 32h/50d Inode:1466 hard link: 1
Permission: (0644) Uid: (1000/linuxidc) Gid: (1000/linuxidc)
Last visit: 2020-04-15 1919 Fraser 23 57.654700585 + 0800
Last modified: 2020-04-15 1919 Vol 237.654700585 + 0800
Last modified: 2020-04-15 1919 Vol 23 57.654700585 + 0800
Creation time:-
8. Ignore the creation of files
The-c or-no create touch option allows us to ignore file creation. Although you may think this is useless, many users use it to check for the existence of files.
If the file does not exist, the file is not created (the lamp will not light when there is light, and it will not light without light. )
Linuxidc@ubuntu:~/linuxidc.com$ touch-c ubuntu linuxidc@ubuntu:~/linuxidc.com$ touch-- no-create ubuntu
When you run the above command, there will be no changes in your working directory. You can run the ls command again to verify this.
9. Change the file date
We can also change the date of the file manually using the touch command. You will need to use the-d or-date options to do this. The following command shows how to use touch to change the date of a file to an earlier date.
Linuxidc@ubuntu:~/linuxidc.com$ ls-l linuxidc-rw-r--r-- 1 linuxidc linuxidc 21 4-thanks 15 19:32 linuxidc linuxidc@ubuntu:~/linuxidc.com$ touch-d'28 April' linuxidc linuxidc@ubuntu:~/linuxidc.com$ ls-l linuxidc-rw-r--r-- 1 linuxidc linuxidc 21 4-thanks 28 2020 linuxidc
Now, if you run the ls command again, you will see that it displays the file on April 28th. But you created this file today, right? If you look closely, you will see that ls displays only the date, not the time. You can even use this command to set the date to a future date.
10. Change the date and time to custom values
The-t option of touch enables us to change the date and time of the file to individualized values. In this way, we can change several parameters, including year, month, date, hour, minute and second. Take a closer look at the following example to see how it works.
Linuxidc@ubuntu:~/linuxidc.com$ ls-l linuxidc # shows the current time and date-rw-r--r-- 1 linuxidc linuxidc 21 3-questions 28 00:00 linuxidc linuxidc@ubuntu:~/linuxidc.com$ touch-t 202802190529 linuxidc linuxidc@ubuntu:~/linuxidc.com$ ls-l linuxidc-rw-r--r-- 1 linuxidc linuxidc 21 2-minutes 19 2028 linuxidc
Now run the ls command again to check the date and time parameters. The parameter format of the-t option is YYMMDDhhmm. Because we set the value to the future, ls will not be able to display the time parameter.
11. Detailed date of change
You can also use touch to change the date of the file in detail. Simply pass the argument to the-date option as a string. The following command shows how to change the file date to the previous day.
Linuxidc@ubuntu:~/linuxidc.com$ ls-l linuxidc.com-rw-r--r-- 1 linuxidc linuxidc 0 4-thanks 15 19:56 linuxidc.com linuxidc@ubuntu:~/linuxidc.com$ touch-- date= "yesterday" linuxidc.com linuxidc@ubuntu:~/linuxidc.com$ ls-l linuxidc.com-rw-r--r-- 1 linuxidc linuxidc 0 4-thanks 14 19:57 linuxidc.com
Use the following command to change the date to the next day.
Linuxidc@ubuntu:~/linuxidc.com$ touch-date= "tomorrow" linuxidc.com linuxidc@ubuntu:~/linuxidc.com$ ls-l linuxidc.com-rw-r--r-- 1 linuxidc linuxidc 0 4-16 2020 linuxidc.com
Use the ls command to verify the dates and see if they work as expected.
12. Copy the file access time of the symbolic link
The-h or-no-dereference options of touch allow users to copy file access times across symbolic links. However, this option works only on systems that allow you to change the symbolic link time.
Touch-h link
This command does not work in many Linux distributions due to various security issues.
13. View the help page
The help page contains summary information for all possible options for the command. This is useful because users can browse the page and remember what an option should do.
Touch-help
This command displays the help page for the Linux touch command on your terminal. It frees you from having to remember the use of each option.
14. Check the manual page
The man page or manual contains details about all available options for Linux terminal commands. You can query the page at any time by issuing the following simple command.
Man touch
This will bring you in-depth documentation on the touch approach. If you are confused about the use of options, please refer to this page.
15. Display version information
You can use the following command to see which version of touch is installed on the system. It can help you determine a variety of information about installed packages.
Touch-version on how to use the touch command in Linux to share here, I hope the above content can be of some help to 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.